Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Searching a vector for a vector.

Posted on 2008-06-16
4
Medium Priority
?
204 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 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
}

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

704 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