以下のコードは、単語のグループを に格納std::vector
し、ユーザーが指定した特定の単語がベクトルに格納されているすべての単語と比較して、ベクトルに出現する回数をカウントするためのものです。
std::cin >>
以下のプログラムでは、コンソールは 2 番目に入力を求めません。
#include <iostream>
#include <ios>
#include <iomanip>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, const char * argv[])
{
cout<<"enter your words followed ny EOF"<<endl;
vector<string> words;
string x;
typedef vector<string>::size_type vec_size;
vec_size size;
while (cin>>x)
{
words.push_back(x);
}
size=words.size();
cin.clear();
//now compare
cout<<"enter your word:"<<endl;
string my_word;
int count=0;
cin>>my_word; //didn't get any prompt in the console for this 'cin'
for (vec_size i=0; i<size; ++i)
{
my_word==words[i]?(++count):(count=count);
}
cout<<"Your word appeared "<<count<<" times"<<endl;
return 0;
}
私が得る最終的な出力は、「あなたの言葉は0回現れました」です。コードの問題は何ですか。どんな助けでも素晴らしいでしょう。