Link to home
Create AccountLog in
Avatar of Unimatrix_001
Unimatrix_001Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Searching a vector for a vector.

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
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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

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

Avatar of Unimatrix_001

ASKER

That's the one. :)