Advertisement
Caminhoneiro

Swap and others concepts on CPP

Jul 5th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. // Unexpected.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. using namespace std;
  9.  
  10. int main()
  11. {
  12.     int a{ 4 }, b{ 5 };
  13.     swap(a, b);
  14.  
  15.     vector<int> evens{ 2,4,6,8, 10 };
  16.     vector<int> odds{ 1,3,5,7,9 };
  17.  
  18.     auto v1 = evens;
  19.     swap(v1[0], v1[1]);
  20.     iter_swap(begin(v1), begin(v1) + 1);
  21.  
  22.     auto v2 = odds;
  23.     swap(v1[0], v2[0]);
  24.     iter_swap(begin(v1), find(begin(v2), end(v2), 5));
  25.  
  26.     v1 = evens;
  27.     v2 = odds;
  28.  
  29.     swap_ranges(begin(v1), find(begin(v1), end(v1), 6), begin(v2));
  30.  
  31.     vector<int> tasks(6); //Obvious in the real life it will be objects but lets work with int a priori
  32.     iota(begin(tasks), end(tasks), 1); //Make the vector be from 1 to 10
  33.    
  34.     auto two = std::find(begin(tasks), end(tasks), 2);//I'll get by a find but in the real life I'll use a sort of an event handler that the user clicked on item number 4. The user let it go between items 1 and 2
  35.     auto four = std::find(begin(tasks), end(tasks), 4);//Im calling find and finding the 2 and then I'm calling find and finding the 4
  36.     rotate(two, four, four + 1);//Now I call the rotate.
  37.  
  38.     vector<int> numbers(8);
  39.     iota(begin(numbers), end(numbers), 1);
  40.     auto selected = std::stable_partition(begin(numbers), end(numbers), [](int i) {return i % 2 != 0; });
  41.     four = std::find(begin(numbers), end(numbers), 4);
  42.     rotate(begin(numbers), selected, four);
  43.  
  44.  
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement