In [1]:
#include <vector>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <functional>
In [2]:
std::vector<int> v(10, 2);
std::partial_sum(v.cbegin(), v.cend(), v.begin());
std::cout << "Among the numbers: ";
std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
Among the numbers: 2 4 6 8 10 12 14 16 18 20 
In [3]:
// minden elemre igaz e
if (std::all_of(v.cbegin(), v.cend(), [](int i){ return i % 2 == 0; })) {
    std::cout << "All numbers are even\n";
}
All numbers are even
In [4]:
// egyikre sem igaz (mivel a modulus a remindert szamolja es az mindenhol nulla azaz mindenhol hamissa konvertalodik)
if (std::none_of(v.cbegin(), v.cend(), std::bind(std::modulus<int>(), std::placeholders::_1, 2))) {
    std::cout << "None of them are odd\n";
}
None of them are odd
In [5]:
struct DivisibleBy {
    const int d;
    DivisibleBy(int n) : d(n) {}
    bool operator()(int n) const { return n % d == 0; }
};
 
// barmelyik elemre a range en belul igaz e
if (std::any_of(v.cbegin(), v.cend(), DivisibleBy(7))) {
    std::cout << "At least one number is divisible by 7\n";
}
At least one number is divisible by 7
In [6]:
void goodbye(const std::string& s) {
    std::cout << "Goodbye " << s << '\n';
}
 
class Object {
public:
    void hello(const std::string& s) {
        std::cout << "Hello " << s << '\n';
    }
};

// fvpointer void visszateresi ertekkel es const string& parameterrel
typedef std::function<void(const std::string&)> ExampleFunction;
Object instance;
std::string str("World");
ExampleFunction f = std::bind(&Object::hello, &instance, std::placeholders::_1);
 
// equivalent to instance.hello(str)
// az str a placeholder
f(str);
f = std::bind(&goodbye, std::placeholders::_1);
 
// equivalent to goodbye(str)
f(str);
Hello World
Goodbye World
In [7]:
struct Sum {
    Sum(): sum{0} { }
    void operator()(int n) { sum += n; }
    int sum;
};

std::vector<int> nums{3, 4, 2, 8, 15, 267};
 
auto print = [](const int& n) { std::cout << " " << n; };
 
std::cout << "before:";
std::for_each(nums.begin(), nums.end(), print);
std::cout << '\n';
 
std::for_each(nums.begin(), nums.end(), [](int &n){ n++; });
 
// calls Sum::operator() for each number
Sum s = std::for_each(nums.begin(), nums.end(), Sum());
 
std::cout << "after: ";
std::for_each(nums.begin(), nums.end(), print);
std::cout << '\n';
std::cout << "sum: " << s.sum << '\n';
before: 3 4 2 8 15 267
after:  4 5 3 9 16 268
sum: 305
In [8]:
std::vector<int> v2{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };
 
// determine how many integers in a std::vector match a target value.
int target1 = 3;
int target2 = 5;
int num_items1 = std::count(v2.begin(), v2.end(), target1);
int num_items2 = std::count(v2.begin(), v2.end(), target2);
std::cout << "number: " << target1 << " count: " << num_items1 << '\n';
std::cout << "number: " << target2 << " count: " << num_items2 << '\n';
 
// use a lambda expression to count elements divisible by 3.
int num_items3 = std::count_if(v2.begin(), v2.end(), [](int i){return i % 3 == 0;});
std::cout << "number divisible by three: " << num_items3 << '\n';
number: 3 count: 2
number: 5 count: 0
number divisible by three: 3
In [9]:
bool IsOdd (int i) {
    return ((i%2)==1);
}

int n1 = 3;
int n2 = 5;
 
std::vector<int> v3{0, 1, 2, 3, 4};
 
auto result1 = std::find(std::begin(v3), std::end(v3), n1);
auto result2 = std::find(std::begin(v3), std::end(v3), n2);
 
if (result1 != std::end(v3)) {
    std::cout << "v contains: " << n1 << '\n';
} else {
    std::cout << "v does not contain: " << n1 << '\n';
}
 
if (result2 != std::end(v3)) {
    std::cout << "v contains: " << n2 << '\n';
} else {
    std::cout << "v does not contain: " << n2 << '\n';
}

std::vector<int> myvector;
    
myvector.push_back(10);
myvector.push_back(25);
myvector.push_back(40);
myvector.push_back(55);
    
std::vector<int>::iterator it = std::find_if (myvector.begin(), myvector.end(), IsOdd);
std::cout << "The first odd value is " << *it << '\n';
v contains: 3
v does not contain: 5
The first odd value is 25
In [10]:
//////copy
std::vector<int> from_vector(10);
std::iota(from_vector.begin(), from_vector.end(), 0);

std::copy(from_vector.begin(), from_vector.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';    
 
std::vector<int> to_vector;
std::copy(from_vector.begin(), from_vector.end(), std::back_inserter(to_vector));

std::cout << "to_vector contains: ";
 
std::copy(to_vector.begin(), to_vector.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';

//////copy_n
std::string in = "1234567890";
std::string out;
 
std::copy_n(in.begin(), 4, std::back_inserter(out));
std::cout << out << '\n';

/////////////////////////////////////////////////////////

///////copy_if
std::vector<int> foo = {25,15,5,-5,-15};
std::vector<int> bar (foo.size());
  
// copy only positive numbers:
auto it2 = std::copy_if (foo.begin(), foo.end(), bar.begin(), [](int i){return !(i<0);} );
bar.resize(std::distance(bar.begin(),it2));  // shrink container to new size
  
std::cout << "bar contains:";
for (int& x: bar) std::cout << ' ' << x;
std::cout << '\n';
0 1 2 3 4 5 6 7 8 9 
to_vector contains: 0 1 2 3 4 5 6 7 8 9 
1234
bar contains: 25 15 5