Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CPlusPlusNotesForProfessionals.pdf
Скачиваний:
47
Добавлен:
20.05.2023
Размер:
5.11 Mб
Скачать

//create vector

vector<int> intVec{4,6,8,9,10,30,55,100,45,2,4,7,9,43,48};

//count occurrences of 9, 55, and 101

size_t count_9 = count(intVec.begin(), intVec.end(), 9); //occurs twice size_t count_55 = count(intVec.begin(), intVec.end(), 55); //occurs once size_t count_101 = count(intVec.begin(), intVec.end(), 101); //occurs once

//print

result

 

 

cout <<

"There are " << count_9

<<

" 9s"<< endl;

cout

<<

"There is " << count_55

<<

" 55"<< endl;

cout

<<

"There is " << count_101

<< " 101"<< ends;

//find the first element == 4 in the vector

vector<int>::iterator itr_4 = find(intVec.begin(), intVec.end(), 4);

//count its occurrences in the vector starting from the first one size_t count_4 = count(itr_4, intVec.end(), *itr_4); // should be 2

cout << "There are " << count_4 << " " << *itr_4 << endl;

return 0;

}

Output

There are 2 9s

There is 1 55

There is 0 101

There are 2 4

Section 62.9: std::count_if

template <class InputIterator, class UnaryPredicate> typename iterator_traits<InputIterator>::difference_type

count_if (InputIterator first, InputIterator last, UnaryPredicate red);

E ects

Counts the number of elements in a range for which a specified predicate function is true

Parameters

first => iterator pointing to the beginning of the range last => iterator pointing to the end of the range red => predicate function(returns true or false)

Return

The number of elements within the specified range for which the predicate function returned true.

Example

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

using namespace std;

GoalKicker.com – C++ Notes for Professionals

337

/*

Define a few functions to use as predicates

*/

//return true if number is odd bool isOdd(int i){

return i%2 == 1;

}

//functor that returns true if number is greater than the value of the constructor parameter provided

class Greater { int _than;

public:

Greater(int th): _than(th){} bool operator()(int i){

return i > _than;

}

};

int main(int argc, const char * argv[]) {

//create a vector

vector<int> myvec = {1,5,8,0,7,6,4,5,2,1,5,0,6,9,7};

//using a lambda function to count even numbers

size_t evenCount = count_if(myvec.begin(), myvec.end(), [](int i){return i % 2 == 0;}); // >= C++11

//using function pointer to count odd number in the first half of the vector size_t oddCount = count_if(myvec.begin(), myvec.end()- myvec.size()/2, isOdd);

//using a functor to count numbers greater than 5

size_t greaterCount = count_if(myvec.begin(), myvec.end(), Greater(5));

cout << "vector size: " << myvec.size() << endl;

cout << "even numbers: " << evenCount << " found" << endl; cout << "odd numbers: " << oddCount << " found" << endl; cout << "numbers > 5: " << greaterCount << " found"<< endl;

return 0;

}

Output

vector size: 15

even numbers: 7 found odd numbers: 4 found numbers > 5: 6 found

GoalKicker.com – C++ Notes for Professionals

338