algorithm - STL C++

all_of()

Declaration

template <class InputIterator, class Predicate>
bool all_of(
	InputIterator qFirst,
	InputIterator qLast,
	Predicate qTester
);

Description

This function searches the entries from "qFirst" to the one before "qLast" to test whether each entry satisfies the "qTester" function. If all entries satisfy the test, true is returned. Otherwise, the function returns false.

Header Include

#include <algorithm>

Example

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

bool AllAreX(char cChar) {
    return (cChar == 'X');
}

int main()
{
    using namespace std;

    // Create two vector instances
    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');

    // Test all entries of the vector
    bool bAllAreX = all_of(qV.begin(), qV.end(), AllAreX);
    cout << "All entries " << (bAllAreX ? "are" : "are not") << " X" << endl;

    vector<char>::iterator qIter = qV.begin();
    ++qIter;
    // Test the first entry of the vector
    bAllAreX = all_of(qV.begin(), qIter, AllAreX);
    cout << "All entries " << (bAllAreX ? "are" : "are not") << " X" << endl;

    cin.get();
    return 0;
}

Output

all_of() Output
 

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