作爲C++的初學者,我很長時間以來對這一點感到困惑,程序是告訴字符串中每個單詞出現的時間。strcmp(x,str)是錯誤的?
#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
我在網上找了沒有結果經過較長時間的搜索之後。我怎樣才能解決這個問題?
什麼是'y'?也是'std :: string'不能與'std :: strcmp'一起使用。 – Nawaz 2012-08-05 09:45:44
一個字:*文檔* ... – 2012-08-05 09:46:59
如果您需要strcmp的行爲,可以使用string :: compare作爲字符串。 – Cubic 2012-08-05 11:55:59