Solved

Another vector question

Posted on 2004-10-18
5
225 Views
Last Modified: 2010-04-01
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
Comment
Question by:jewee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 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

by:jewee
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

by:jkr
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

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

Expert Comment

by:jkr
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

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

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

Join & Ask a Question