# STL - an algorithm for finding a collection within a collection?

Discussion in 'C++' started by Dylan, Mar 21, 2005.

1. ### DylanGuest

Is there an STL algorithm that will return true if each element in
coll1 is present in coll2

Dylan, Mar 21, 2005

2. ### Jerry CoffinGuest

Dylan wrote:

> Is there an STL algorithm that will return true if each element in
> coll1 is present in coll2

I don't know of one specifically for this, but if this was the case,
the result from set_difference would be empty (this requires both
collections to be sorted).

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jerry Coffin, Mar 21, 2005

3. ### Ioannis VranosGuest

Dylan wrote:

> Is there an STL algorithm that will return true if each element in
> coll1 is present in coll2

If you want to check if the sequences are exactly the same, use std::equal.

--
Ioannis Vranos

http://www23.brinkster.com/noicys

Ioannis Vranos, Mar 21, 2005
4. ### Paul DubucGuest

Dylan wrote:
> Is there an STL algorithm that will return true if each element in
> coll1 is present in coll2

I think this requires sorted elements. I would use the std::mismatch()
algorithm. With the pair of iterators it returns you can tell if the containers
have equal contents or if one is a subset of the other.

--
Paul M. Dubuc

Paul Dubuc, Mar 21, 2005
5. ### kwijibo28Guest

kwijibo28, Mar 21, 2005
6. ### Daniel T.Guest

In article <>,
Dylan <> wrote:

> Is there an STL algorithm that will return true if each element in
> coll1 is present in coll2

template <class InputIterator1, class InputIterator2>
bool includes(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2);

template <class InputIterator1, class InputIterator2,
class StrictWeakOrdering>
bool includes(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
StrictWeakOrdering comp);

Includes tests whether one sorted range includes another sorted range.
That is, it returns true if and only if, for every element in [first2,
last2), an equivalent element [1] is also present in [first1, last1)
[2]. Both [first1, last1) and [first2, last2) must be sorted in
ascending order.

Daniel T., Mar 22, 2005