作为一个C++的初学者,我对这一点困惑了很久,程序就是告诉字符串中每个单词出现的次数。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string x;
vector<string> str;
vector<int> t;
while (cin >> x)
{
int k = 0;
for (int j = 0; j != str.size(); j++)
{
if (strcmp(x,str[j]) == 0)
t[j]++;
k = 1;
}
if (k == 0)
{
str.push_back(x);
t.push_back(1);
}
}
for (int i = 0; i != str.size(); i++ )
{
cout << str[i] << " " << t[i] << endl;
}
return 0;
}
这是错误:
C++\code\3.3.cpp(17) : error C2664: 'strcmp' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
经过长时间的搜索,我在互联网上没有找到任何结果。我怎样才能解决这个问题?