Solved

Another vector question

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

Independent Software Vendors: 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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

628 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