wpp
wpp

Reputation: 7303

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. Cheers!

Upvotes: 0

Views: 6349

Answers (2)

Ahmed
Ahmed

Reputation: 7238

Your code should print "match" on the intersected strings between the two vectors, it should work fine for the intersection, you may need to check the string value itself, it may contain spaces or so

Upvotes: 0

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143071

For what you're trying with your code you might want to use 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.

For anything else you want to achieve you better be more specific.

Upvotes: 2

Related Questions