Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# Another vector question

Posted on 2004-10-18
Medium Priority
236 Views
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
Question by:jewee
• 3
• 2

LVL 86

Accepted Solution

jkr earned 2000 total points
ID: 12342193
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 Comment

ID: 12342732
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

LVL 86

Expert Comment

ID: 12343403
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 Comment

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

LVL 86

Expert Comment

ID: 12343443
>>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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
###### Suggested Courses
Course of the Month9 days, 18 hours left to enroll

#### 927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.