Solved

Searching a vector for a vector.

Posted on 2008-06-16
4
203 Views
Last Modified: 2010-04-21
Hi,

Is there some STL method that lets me search a vector of values from a certain index for another vector of values and telling me the returned index?

Thanks,
Uni
0
Comment
Question by:Unimatrix_001
[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
4 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 21792108
You mean you want to find the first occurrence of the entire second vector in the first ? If so, take a look at the search algorithm for example :

        http://www.cplusplus.com/reference/algorithm/search.html
std::vector<int> vec;       // <--- the vector
std::vector<int> pattern;   // <--- the pattern we want to find
 
std::vector<int>::iterator it = search(vec.begin(), vec.end(), pattern.begin(), pattern.end());
 
if (it != vec.end()) {
  // found pattern at iterator it
}

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21792115
Or do you want to find any value from the second vector in the first ? If so, take a look at the find_first_of algorithm :

        http://www.cplusplus.com/reference/algorithm/find_first_of.html

std::vector<int> vec;       // <--- the vector
std::vector<int> match;     // <--- the values we want to find
 
std::vector<int>::iterator it = find_first_of(vec.begin(), vec.end(), pattern.begin(), pattern.end());
 
if (it != vec.end()) {
  // found a match at iterator it
}

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21792122
Obviously in the second code sample, pattern should have been replaced by match :
std::vector<int> vec;       // <--- the vector
std::vector<int> match;     // <--- the values we want to find
 
std::vector<int>::iterator it = find_first_of(vec.begin(), vec.end(), match.begin(), match.end());
 
if (it != vec.end()) {
  // found a match at iterator it
}

Open in new window

0
 
LVL 3

Author Closing Comment

by:Unimatrix_001
ID: 31467532
That's the one. :)
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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 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…

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