Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking to find a C++ STL vector inside an STL vector

Tags:

c++

find

stl

vector

I am trying to see if vector v1 is contained within vector v2. My vectors are ordered and it is required that the order is preserved.

For example, if v1= (a, b) and v2 = (e, f, a, b), I would like to get an iterator pointing to a in v2.

STL find only finds one object inside a vector. I guess what I want is something similar to string::find.

Is there any function in STL for doing this?

like image 801
Ari Avatar asked May 09 '12 00:05

Ari


People also ask

How do you find an element in vector STL?

How do we find an element using STL? Approach 1: Return index of the element using std::find() Use std::find_if() with std::distance() Use std::count()

How do you check if a vector contains an item?

util. vector. contains() method is used to check whether a specific element is present in the Vector or not. So basically it is used to check if a vector contains any particular element or not.

How do you combine two vectors?

The concatenation of vectors can be done by using combination function c. For example, if we have three vectors x, y, z then the concatenation of these vectors can be done as c(x,y,z). Also, we can concatenate different types of vectors at the same time using the same same function.


1 Answers

It looks like you want to search for a subsequence inside another sequence. You can do that with std::search from the Standard Library.

auto it = std::search(v2.begin(), v2.end(), v1.begin(), v1.end());
like image 180
Blastfurnace Avatar answered Oct 19 '22 19:10

Blastfurnace