24 Temmuz 2019 Çarşamba

std::any_of

Giriş
Şu satırı dahil ederiz
#include <algorithm> // std::any_of
Birinci imza şöyle.
template <class InputIterator, class Predicate>
bool any_of(InputIterator first, InputIterator last, Predicate pred);
İkinci imza şöyle.
template <class ExecutionPolicy, class ForwardIterator, class Predicate>
bool any_of(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last,
  Predicate pred);
Açıklaması şöyle. Muhtemelen short circuit yaparak ilk true veren elemanı görünce hemen döner.
Returns: false if [first, last) is empty or if there is no iterator i in the range [first, last) such that pred(*i) is true, and true otherwise.
Complexity: At most last - first applications of the predicate.
Örnek
Şöyle yaparız.
bool found = std::any_of(vector.begin(),
                         vector.begin(),
                         [](auto item) -> bool { return item == <xxx>; });
Örnek
Şöyle yaparız. Dizideki karakterlerden herhangi birisi ise isValid() true döner.
#include <array>     // std::array
#include <algorithm> // std::any_of

const std::array<char, 4> options{ '+', '-', '*', '/' };
const auto tester = [&temp](char c) { return temp->left->oper == c ||
                                             temp->right->oper == c; };
const bool isValid = std::any_of(options.cbegin(), options.cend(), tester);

while(isValid) // now the while-loop is simplified to
{
    // do something
}



Hiç yorum yok:

Yorum Gönder