algorithm - STL C++

any_of()

Declaration

template <class InputIterator, class Predicate>
bool any_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 any entry satisfies the test, true is returned. Otherwise, the function returns false.

Header Include

#include <algorithm>

Example

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

bool HasAnA(char cChar) {
    return (cChar == 'a');
}

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 bAnyIsA = any_of(qV.begin(), qV.end(), HasAnA);
    cout << (bAnyIsA ? "An" : "No") << " entry is \'a\'" << endl;

    vector<char>::iterator qIter = qV.begin();
    ++qIter;
    // Test the first entry of the vector
    bAnyIsA = any_of(qV.begin(), qIter, HasAnA);
    cout << (bAnyIsA ? "An" : "No") << " entry is \'a\'" << endl;

    cin.get();
    return 0;
}

Output

any_of() Output
 

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