如何比较C ++中两个向量的字符串内容

时间:2011-12-04 10:21:57

标签: c++ string vector compare

我有两个向量,想要比较它们的内容(字符串),但这不起作用:

    vector<string>inwords = getInWords();
    vector<string>killwords = getKillWords();


    vector<string>::iterator it;
    vector<string>::iterator ut;

    for(it = inwords.begin(); it < inwords.end(); it++){
     for (ut = killwords.begin(); ut < killwords.end(); ut++) {
      if (*ut == *it){
       cout << "match" << endl;
      }
     }
    } 

我也尝试过比较功能:

    if (killwords[u].compare(inwords[i]) == 0)

我的猜测是我需要重载==运算符,但我不知道该怎么做。如果你可以帮助我,那将是很好的,因为我的谷歌搜索并没有真正让我在任何地方。干杯!

2 个答案:

答案 0 :(得分:3)

对于您尝试使用代码的内容,您可能希望使用std::find_first_of。如果您尝试比较(非)平等的范围,可能需要查看std::mismatchstd::equal

对于你想要实现的任何其他事情,你最好更具体。

答案 1 :(得分:0)

你的代码应该在两个向量之间的相交字符串上打印“匹配”,它应该适用于交集,你可能需要检查字符串值本身,它可能包含空格左右