Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Iterators.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <vector>
- #include <algorithm>
- #include <numeric>
- #include <iterator>
- #include <deque>
- #include <array>
- using namespace std;
- int main()
- {
- vector<int> v1(10); //Create a vector and give the value of 10
- fill(begin(v1), end(v1), 1); //Fill the vector with the value of 1
- fill_n(begin(v1), 6, 2); //Fill the six first values with value of two
- iota(begin(v1), end(v1), 1);//Iota makes count till 10
- vector<int> v2;//Create the vector
- fill_n(back_inserter(v2), 6, 2);//Put the size of six on this vector and fill with the value of two
- generate_n(back_inserter(v2), 10, [n = 0]()mutable{return n++; });//Increase the size of Vector to put values from 0 till 10
- //Vector dont support fron inserter but deque support, to use front_inserter we create a deque<int> besides a vecor<int>
- deque<int> q3;
- fill_n(front_inserter(q3), 6, 2);
- generate_n(front_inserter(q3), 10, [n = 0]()mutable{return n++; });
- //fill_n(front_inserter(v1), 6, 2); //you cant use front_inserter on vector
- v2.clear(); //Clean the values inside vector
- transform(begin(v1), end(v1), back_inserter(v2),
- [](int elem) {return elem * 2; }); //Pick up every element on v1 and put on v2 AND the lambda double every value to put on v2
- vector<int> v3{ 3, 6, 1, 0, -2, 5 };
- vector<int> v5;
- copy_if(begin(v3), end(v3), back_inserter(v5),
- [](int elem) {return elem % 2 == 0; }); //The lambda return true for even numbers and false for odd numbers
- v3[3] = -2;
- vector<int> v6;
- unique_copy(begin(v3), end(v3), back_inserter(v6));
- string sentence = "Hello, world!";
- string reverse;
- reverse_copy(begin(sentence), end(sentence), back_inserter(reverse));
- //At this point v1 has values from 0,1 till 10
- v2.clear();//v2 is now empty
- copy(rbegin(v1), rend(v1), back_inserter(v2)); //Insert values from v1 to v2 in reverse order
- v1[5] = 2; //Change an expecific element
- //Using front interator (begin) (end)
- auto two = find(begin(v1), end(v1), 2); //points to value of 2 on the list v1
- auto distance = two - begin(v1); //Calculate the distance from the value searched from the beginning of the list
- two++;//Increments
- //Using reverse interator (rbegin) (rend)
- auto rtwo = find(rbegin(v1), rend(v1), 2);
- distance = rtwo - rbegin(v1);
- rtwo++;
- //distance = rtwo - two; //It will give error because they are different operators
- //is "3" in the first or second half?
- auto three = find(begin(v1), end(v1), 3);
- bool firsthalf = (three - begin(v1) < end(v1) - three);
- //What about "9" ?
- auto nine = find(begin(v1), end(v1), 9);
- firsthalf = (nine - begin(v1) < end(v1) - nine);
- array<const int, 5> ca = { 3,1,6,0,2 };
- auto it = begin(ca);
- it++;
- //*it = 7;
- int i = *it; //Run till second value of the array and points to it
- //vector<const int> vc;
- it = find(begin(ca), end(ca), 1);
- it++; //Now the int i above still with the value with gets previously but the it pointer is now 6
- //*it = 7;
- //it = cbegin(ca);
- auto it2 = begin(v2);
- it2++;
- *it2 = 7;
- auto cit = cbegin(v2);
- cit++;
- //*cit = 7;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement