x
Solved

# Searching a vector for a vector.

Posted on 2008-06-16
Medium Priority
208 Views
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
Question by:Unimatrix_001
• 3

LVL 53

Accepted Solution

Infinity08 earned 2000 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
}
``````
0

LVL 53

Expert Comment

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
}
``````
0

LVL 53

Expert Comment

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
}
``````
0

LVL 3

Author Closing Comment

ID: 31467532
That's the one. :)
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.