Материалы по теме класса map и лямбда-функций

Примеры кода

Re: Примеры кода

by Николай Ольховский -
Number of replies: 0
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int randVec()
{
return rand() % (99 - 10) + 10;
}
bool ifVec(const int & x)
{
if(x % 3 == 0) return true;
else return false;
}
int main(int argc, char** argv) {
vector<int> v (10);

generate(v.begin(), v.end(), [](){
return rand() % 99;
});

int count = 0;
for_each(v.begin(), v.end(), [&count](int &x){
if(x % 3 == 0) count++;
});

cout << count_if(v.begin(), v.end(), [](const int & x){
return x % 3 == 0;
}) << endl;

copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
return 0;
}