下面的程序没有像Linux G++或freopen()语句中那样接受输入.为什么
this following program not taking input as it should in linux g++ or with freopen() statement...why?
它与使用 stdin 和 stdout 的代码块一起工作得很好,但是当我使用 freopen 重定向流或使用"g++"在 Linux 中运行代码时,输入/输出显示不稳定的行为!!任何人都可以告诉我可能出现的问题。
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,string> p;
int main()
{
// freopen("11.txt","r+",stdin);
// freopen("out.txt","w",stdout);
int n,t,i,j,k;
string tmp;
cin>>t;
for(i=1;i<=t;i++)
{
cin>>n;
fflush(stdin);
priority_queue<p,vector<p>,less<p> > s;
for(j=0;j<n;j++)
{ getline(cin,tmp);
map<char,int> mp;
int c=0;
for(k=0;k<tmp.length();k++)
{
if(tmp[k]!=' '){
if(mp.find(tmp[k])==mp.end())
{c++;
mp[tmp[k]]++;
}
}
}
if(!s.empty()&&s.top().first<c)
{ s.pop();}
s.push(make_pair(c,tmp));
}
p ans;
if(!s.empty())
ans =s.top();
string qwe = ans.second;
cout<<"Case #"<<i<<": "<<qwe<<endl;
fflush(stdin);
}
return 0;
}
cin>>n;
其次
getline(cin,tmp);
不起作用,因为第一行通常会在输入流中留下换行符。
添加一行代码以忽略输入流中的其余行。
cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
紧接着
cin >> n;
确保添加
#include <limits>
使用std::numeric_limits
.
相关文章:
- 为什么是0;C++中的有效语句
- 为什么布尔开关语句有编译器警告?
- 为什么我的 if else 语句不起作用并从数组中输出正确的索引?
- 为什么我的两个 cin 语句没有在程序结束时运行?
- 为什么在此C++代码中触发此 if 语句?
- 为什么无论输入如何,所有 if-else 语句都会打印?
- 为什么切换 if else 语句的顺序会导致错误?
- 为什么别名声明不是有效的 init 语句(/simple-declaration)?
- 为什么在 C/C++ 中交织 switch/for/if 语句是有效的?
- 为什么这个复合语句作为用大括号和括号括起来的语句序列似乎不是有效的语句表达式
- if-else 语句仅按特定顺序工作,我不知道为什么
- 为什么 if 语句在 Java 中"if(1)"给出错误,但在C++中成立?
- 如果 constexpr语言 - 为什么完全检查丢弃的语句?
- 为什么这个 IF 语句在循环内不打印
- 为什么我们在 Arduino 中有两个 if 语句?
- 为什么我的 if 语句不适用于 c++ 中的字符串?
- 为什么当 while 循环中的 if 条件变为 false 时,我的函数不输出最后一条语句?
- 如果有 constexpr if 语句,为什么没有其他 constexpr 语句呢?
- 为什么当我从语句检查中删除"+ mod"时,以下程序给出错误的答案。问题链接:https://www.codechef.com/problems/FFC219B
- 为什么 cout 语句在传递指向函数的指针时没有执行?