gpt4 book ai didi

c++ - 为什么这个 cin 不能正常工作?

转载 作者:太空宇宙 更新时间:2023-11-04 11:27:42 24 4
gpt4 key购买 nike

<分区>

#include <iostream>
#include <map>
#include <string>

using namespace std;

int main (void)
{

int c;
cout<<"enter number of test cases\n";
cin>>c;
while (c!=0)
{
string s;
int t;
cout<<"enter number of strings to be entered\n";
cin>>t;
map <string,int> a;
map <string,int>::iterator it;
while ( t!= 0 )
{
getline(cin,s);
it = a.find(s);
if ( it == a.end() )
{
a.insert(pair<string,int>(s,1));
cout<<"New inserted\n";
}
else
{
a[s]++;
cout<<"Value incremented\n";
}
t--;
}
it = a.begin();
cout<<"Value will print\n";
while ( it != a.end() )
{
cout<<it->first<<" "<<it->second<<"\n";
it++;
}
c--;
}
return 0;
}

所以,我编写了这段代码,首先询问测试用例,然后询问字符串的数量,然后对字符串进行排序并输出它们的频率。现在,在这段代码中,当我在输入字符串数量后按下回车键时,会显示消息 New Inserted,这意味着新行将作为字符串放入 map 中。为什么会这样?

谢谢!

PS:我尝试将 fflush(stdin) 放在 getline 之前,但它也无济于事。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com