algorithm - STL C++

minmax_element()

Declaration

template <class ForwardIterator>
pair minmax_element(
	ForwardIterator xFirst,
	ForwardIterator xLast
);

Description

This function returns a pair of iterators that point to the smallest and largest of elements, respectively, in range from "xFirst" up through the one before "xLast." The overloaded version uses the function "xComp" as a comparison function.

Header Include

#include <algorithm>

Overloads

template <class ForwardIterator, class BinaryPred>
pair minmax_element(
	ForwardIterator xFirst,
	ForwardIterator xLast,
	BinaryPred xComp
);

Example

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

int main()
{
	using namespace std;

	// Create a vector instance
	vector<char> qV;
	qV.push_back('X');
	qV.push_back('o');
	qV.push_back('a');
	qV.push_back('X');
	qV.push_back('.');
	qV.push_back('n');
	qV.push_back('e');
	qV.push_back('t');

	vector<char>::iterator qIter;
	// Output the vector
	cout << "The Vector: ";
	for (qIter = qV.begin(); qIter != qV.end(); ++qIter) {
		cout << *qIter;
	}
	cout << endl;

	typedef vector<char>::iterator TIter;
	pair<TIter, TIter> qMinMax;
	// Find the smallest and largest values
	qMinMax = minmax_element(qV.begin(), qV.end());

	// Output the smallest and largest values
	cout << "The smallest is: " << *(qMinMax.first) << endl;
	cout << "The largest is: " << *(qMinMax.second) << endl;

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

Output

minmax_element() Output
 

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