Ari
Ari

Reputation: 7556

Looking to find a C++ STL vector inside an 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?

Upvotes: 15

Views: 10820

Answers (1)

Blastfurnace
Blastfurnace

Reputation: 18652

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());

Upvotes: 25

Related Questions