Solved

Another vector question

Posted on 2004-10-18
5
222 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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Grammars for C C++ and java 1 138
GUI: DIalog Stacking and Popping in MS C++ 4 87
C++ mouse_event mouse look 7 111
Installshield for Embarcadero EX 10.1 Berlin 4 72
  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 …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

756 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