Solved

Searching a vector for a vector.

Posted on 2008-06-16
4
192 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
  • 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

809 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