algorithm - STL C++

includes()

Declaration

template <class InputIterator1, class InputIterator2>
bool includes(
	InputIterator1 xFirst1,
	InputIterator1 xLast1,
	InputIterator2 xFirst2,
	InputIterator2 xLast2
);

Description

This function return true if the range of items starting at "xFirst1" going up to, but not including "xLast1" contains all of the items in the range starting at "xFirst2" going up to, but not including "xLast2". The function requires that the ranges are sorted according to a comparison function, which by default is the less than function. In case of the overloaded version, the comparison function "xComp" is passed in as an addition argument. So, the ranges must be sorted according "xComp."

Header Include

#include <algorithm>

Overloads

template <class InputIterator1, class InputIterator2, class BinaryPred>
bool includes(
	InputIterator1 xFirst1,
	InputIterator1 xLast1,
	InputIterator2 xFirst2,
	InputIterator2 xLast2,
	BinaryPred xComp
);

Example

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
	using namespace std;

	// Create two vector instances
	vector<int> qV1;
	qV1.push_back(-3);
	qV1.push_back(-1);
	qV1.push_back(0);
	qV1.push_back(1);

	vector<int> qV2;
	qV2.push_back(-3);
	qV2.push_back(0);
	qV2.push_back(1);

	vector<int>::iterator qIter;
	cout << "(  ";
	for (qIter = qV1.begin(); qIter != qV1.end(); ++qIter) {
		cout << *qIter << "  ";
	}
	cout << ") ";

	// Test first inclusion
	bool bInc = includes(qV1.begin(), qV1.end(), qV2.begin(), qV2.end());
	cout << ((bInc) ? "includes" : "does not include");

	cout << " (  ";
	for (qIter = qV2.begin(); qIter != qV2.end(); ++qIter) {
		cout << *qIter << "  ";
	}
	cout << ")" << endl;

	cout << "(  ";
	for (qIter = qV2.begin(); qIter != qV2.end(); ++qIter) {
		cout << *qIter << "  ";
	}
	cout << ") ";

	// Test opposite inclusion
	bInc = includes(qV2.begin(), qV2.end(), qV1.begin(), qV1.end());
	cout << ((bInc) ? "includes" : "does not include");

	cout << " (  ";
	for (qIter = qV1.begin(); qIter != qV1.end(); ++qIter) {
		cout << *qIter << "  ";
	}
	cout << ")" << endl;

	// Keep the window open
	cin.get();
	return 0;
}

Output

includes() Output
 

© 2007–2024 XoaX.net LLC. All rights reserved.