#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int a;
vector<int > num_count;
while(cin>>a)
{
num_count.push_back(a);
}
int word_count;
cin>>word_count;
cout<<"please input the number that you want to find"<<endl;
cout<<count(num_count.begin(),num_count.end(),word_count);
system("pause");
return 0;
}
今天编写了这个程序,运行时 发现 cin>>word_count这一行死活不出现 直接跳过,让我产生了很大的迷惑,在c++群里问了别人之后发现时 结束 ctrl+z的那里出错了,当我在输入一串数字之后,用ctrl+z结束while时 产生的文件流仍在缓冲区,导致直接cin>>word_count跳过。经过我查了百度之后。对程序进行了更改。
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int a;
vector<int > num_count;
while(cin>>a,!cin.eof())
{
num_count.push_back(a);
}
cin.clear();
int word_count;
cin>>word_count;
cout<<"please input the number that you want to find"<<endl;
cout<<count(num_count.begin(),num_count.end(),word_count);
system("pause");
return 0;
}
经过这样的改动 程序完美运行 哈哈