x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 246

# Another vector question

I am  comparing a string from one vector to a string in another vector.  How would I go about comparing if the string from one vector to a string from the other vector if only the substring needs to match up with the other vector?
0
jewee
• 3
• 2
1 Solution

Commented:
That depends on whether you are using strings or char*. In the letter case, 'strstr()' will find if a substring matches, in the 1st case, 'string::find()' will do that.
0

Author Commented:
So, would i use iterators for both?

I need to compare strings from one vector to strings in another vector.  The size of the vectors are different.
0

Commented:
Yes, if you have a

string str = "substring";
vector<string> vs;

for ( vector<string>::iterator i = vs.begin(); i != vs.end(); ++i) {

if ( -1 != i->find(str)) {

cout << "Found " << str->c_str() " in in " i->c_str() << endl;
}
}
0

Author Commented:
But I have 2 vectors.  I'm comparing each element to elements in the other vector.
0

Commented:
>>I'm comparing each element to elements in the other vector

Oh, then it might work without iterators - assuming that both vectors are of the same size:

vector<string> vs1;
vector<string> vs2;

for ( inti = 0; i != vs1.size(); ++i) {

if ( -1 != vs1[i].find(vs2[i])) {

cout << "Found " << vs2[i].c_str() " in in " vs1[i].c_str() << endl;
}
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.