代码尝试确定两个字符串是否具有相同的模式。错误:表达式必须具有大小类类型()
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <sstream>
bool findMatch(char * s1, char * s2){
std::map<char, std::string> words;
std::istringstream iss(s1);
std::string word;
//for (std::string::size_t i = 0; i < s2.size(); ++i) //line 1
//for (int i = 0; i < s2.size(); ++i) //line 2
{
if (!(iss >> word))
return false;
std::string& mapping = words[s2[i]];
if (mapping == "")
mapping = word;
else if (mapping != word)
return false;
}
return !(iss >> word);
}
int main(int argc, char * argv[]){
bool b = findMatch("red blue blue red red yellow", "abbaac");
std::cout << b << std::endl;
return 0;
}
问题: 我曾两次尝试,1号线和2号线,他们都没有工作
line 1, Error: class "..." has no member "size_t"
line 2: Error: char * s2 Expression must have class type
任何想法?
's2'是一个'char *',所以它没有'size()'成员函数。你可以使用'std :: strlen()'来确定长度(虽然在字符串长度上会是O(n))。另外,它是'std :: string :: size_type',而不是'std :: string :: size_t'。 – jogojapan 2013-03-07 01:45:21