Advertisement
Caminhoneiro

Iterators

Jul 5th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | None | 0 0
  1. // Iterators.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <vector>
  6. #include <algorithm>
  7. #include <numeric>
  8. #include <iterator>
  9. #include <deque>
  10. #include <array>
  11.  
  12. using namespace std;
  13.  
  14.  
  15. int main()
  16. {
  17.     vector<int> v1(10); //Create a vector and give the value of 10
  18.     fill(begin(v1), end(v1), 1); //Fill the vector with the value of 1
  19.     fill_n(begin(v1), 6, 2); //Fill the six first values with value of two
  20.     iota(begin(v1), end(v1), 1);//Iota makes count till 10
  21.  
  22.     vector<int> v2;//Create the vector
  23.     fill_n(back_inserter(v2), 6, 2);//Put the size of six on this vector and fill with the value of two
  24.     generate_n(back_inserter(v2), 10, [n = 0]()mutable{return n++; });//Increase the size of Vector to put values from 0 till 10
  25.  
  26.     //Vector dont support fron inserter but deque support, to use front_inserter we create a deque<int> besides a vecor<int>
  27.     deque<int> q3;
  28.     fill_n(front_inserter(q3), 6, 2);
  29.     generate_n(front_inserter(q3), 10, [n = 0]()mutable{return n++; });
  30.  
  31.     //fill_n(front_inserter(v1), 6, 2); //you cant use front_inserter on vector
  32.  
  33.     v2.clear(); //Clean the values inside vector
  34.     transform(begin(v1), end(v1), back_inserter(v2),
  35.         [](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
  36.  
  37.     vector<int> v3{ 3, 6, 1, 0, -2, 5 };
  38.     vector<int> v5;
  39.     copy_if(begin(v3), end(v3), back_inserter(v5),
  40.         [](int elem) {return elem % 2 == 0; }); //The lambda return true for even numbers and false for odd numbers
  41.  
  42.     v3[3] = -2;
  43.     vector<int> v6;
  44.     unique_copy(begin(v3), end(v3), back_inserter(v6));
  45.  
  46.     string sentence = "Hello, world!";
  47.     string reverse;
  48.     reverse_copy(begin(sentence), end(sentence), back_inserter(reverse));
  49.  
  50.     //At this point v1 has values from 0,1 till 10
  51.     v2.clear();//v2 is now empty
  52.     copy(rbegin(v1), rend(v1), back_inserter(v2)); //Insert values from v1 to v2 in reverse order
  53.  
  54.     v1[5] = 2; //Change an expecific element
  55.  
  56.     //Using front interator (begin) (end)
  57.     auto two = find(begin(v1), end(v1), 2); //points to value of 2 on the list v1
  58.     auto distance = two - begin(v1); //Calculate the distance from the value searched from the beginning of the list
  59.     two++;//Increments
  60.  
  61.     //Using reverse interator (rbegin) (rend)
  62.     auto rtwo = find(rbegin(v1), rend(v1), 2);
  63.     distance = rtwo - rbegin(v1);
  64.     rtwo++;
  65.  
  66.     //distance = rtwo - two; //It will give error because they are different operators
  67.  
  68.     //is "3" in the first or second half?
  69.     auto three = find(begin(v1), end(v1), 3);
  70.     bool firsthalf = (three - begin(v1) < end(v1) - three);
  71.     //What about "9" ?
  72.     auto nine = find(begin(v1), end(v1), 9);
  73.     firsthalf = (nine - begin(v1) < end(v1) - nine);
  74.  
  75.     array<const int, 5> ca = { 3,1,6,0,2 };
  76.     auto it = begin(ca);
  77.     it++;
  78.     //*it = 7;
  79.     int i = *it; //Run till second value of the array and points to it
  80.  
  81.     //vector<const int> vc;
  82.  
  83.     it = find(begin(ca), end(ca), 1);
  84.     it++; //Now the int i above still with the value with gets previously but the it pointer is now 6
  85.     //*it = 7;
  86.     //it = cbegin(ca);
  87.  
  88.     auto it2 = begin(v2);
  89.     it2++;
  90.     *it2 = 7;
  91.  
  92.     auto cit = cbegin(v2);
  93.     cit++;
  94.     //*cit = 7;
  95.  
  96.  
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement