代碼嘗試確定兩個字符串是否具有相同的模式。錯誤:表達式必須具有大小類類型()
#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