Solved

Searching a vector for a vector.

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
best sources to up-to-date in C++? 8 91
C++ mouse_event mouse look 7 97
C++ help/ Toy problem 19 30
Finding Divisors 5 20
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…
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 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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

832 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