#include "stdafx.h" #include #include #include #include using namespace std; //range-based for void range_based_for() { vector nrid = {1,2,3,4,5}; cout << "c++ 03 range-based for" << endl; for (vector::iterator it = nrid.begin(); it != nrid.end(); ++it) { cout << *it << endl; } cout << "c++ 11 range-based for" << endl; for (auto item : nrid) { cout << item << endl; } } void unique_pointer() { //https://stackoverflow.com/questions/16894400/how-to-declare-stdunique-ptr-and-what-is-the-use-of-it //Smart pointers are supposed to model object ownership, //and automatically take care of destroying the pointed object //when the last (smart, owning) pointer to that object falls out of scope. //This way you do not have to remember doing delete on objects allocated dynamically int arv1 = 1; int *arv2 = &arv1; int arv3 = 2; int *arv4 = &arv3; //c++ 03 auto_ptr ap(arv2); cout << "auto_ptr: " << *ap << endl; ap.release(); //c++ 11 //kordades safem kui auto_ptr unique_ptr up(arv4); cout << "unique_ptr: " << *up << endl; up.release(); //shared_ptr - sama mis unique aga ilma garbage collectionita } void lambdas() { //parameetrites auto on c++ 14 aga lambda ise on c++ 11 auto lambda1 = [](auto first, auto second) {return first + second; }; cout << lambda1(10, 10) << endl; cout << lambda1(10.1, 10.1) << endl; //cout << lambda1("kana", "mana") << endl; cout << lambda1(10.1333, 10.112312) << endl; } void vector_with_inline_list() { //c++ 03 vector v0; v0.push_back(1); v0.push_back(2); v0.push_back(3); // voi teha array ja see initida vectoris //c++ 11 vector v = { 1,2,3 }; } void new_algorithms() { vector v = { -1,1,2,3,4,5,6,7 }; cout << "Kas vahemikus -1 kuni 7 on koik arvud positiivsed: " << all_of(v.begin(), v.end(), [](int a) {return a >= 0; }) << endl; cout << "Kas vahemikus -1 kuni 7 on positiivseid arve: " << any_of(v.begin(), v.end(), [](int a) {return a >= 0; }) << endl; cout << "Kas vahemikus -1 kuni 7 ükski element pole positiivne: " << none_of(v.begin(), v.end(), [](int a) {return a >= 0; }) << endl; int source[5] = { 0,12,34,50,80 }; int target[5]; cout << "Enne copy" << endl; cout << "source" << endl; for (int i : source) { cout << i << endl; } cout << "target" << endl; for (int i : target) { cout << i << endl; } copy_n(source, 3, target); cout << "peale copy" << endl; cout << "source" << endl; for (int i : source) { cout << i << endl; } cout << "target" << endl; for (int i : target) { cout << i << endl; } } int main() { range_based_for(); cout << "--------------------------------------------------------------------------" << endl; unique_pointer(); cout << "--------------------------------------------------------------------------" << endl; lambdas(); cout << "--------------------------------------------------------------------------" << endl; vector_with_inline_list(); cout << "--------------------------------------------------------------------------" << endl; new_algorithms(); return 0; }