简体   繁体   English

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

[英]How to compare string content of two vectors in C++

I have two vectors and want to compare their contents (strings) but this does not work: 我有两个向量,想要比较它们的内容(字符串),但这不起作用:

    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;
      }
     }
    } 

I also tried the compare function: 我也尝试了比较功能:

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

My guess is that I need to overload the == operator, but I am not sure how to do that. 我的猜测是我需要重载==运算符,但是我不确定该怎么做。 Would be nice if you could help me out, as my google searches have not really gotten me anywhere. 如果您能帮帮我,那会很好,因为我的Google搜索并没有真正带我到任何地方。 Cheers! 干杯!

For what you're trying with your code you might want to use std::find_first_of . 对于您尝试使用的代码,您可能需要使用std::find_first_of If you're trying to compare ranges for (un)equality you may want to look at std::mismatch or std::equal . 如果要比较(不)等式的范围,则可能需要查看std::mismatchstd::equal

For anything else you want to achieve you better be more specific. 对于您想要实现的其他目标,最好更加具体。

您的代码应该在两个向量之间的相交字符串上打印“ match”,对于相交应该可以正常工作,您可能需要检查字符串值本身,它可能包含空格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM