Advertisement
kaenan

c++

Nov 14th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include<deque>
  4. #include<numeric>
  5. #include<algorithm>
  6. #include<functional>
  7. #include<iostream>
  8. #include<random>
  9. #include<array>
  10. #include<forward_list>
  11.  
  12. using std::deque;
  13. using std::cout;
  14. using std::endl;
  15.  
  16. template <class FowardItr>
  17. void print(FowardItr begin, FowardItr end) {
  18. cout << "\nOutput (" << std::distance(begin, end) << "):\n";
  19. std::for_each(begin, end, [](auto i) { cout << i << " "; });
  20. cout << endl;
  21. }
  22.  
  23. void SequenceContainerAlgorithms()
  24. {
  25. // create a sequence container of 100 integers (it must have random iterators!)
  26. deque<int> d(100);
  27.  
  28. cout << "\n\nHERE:\n";
  29.  
  30. // fill the vector in the following way: 50, 49, ... 2, 1, 49, 48, ... 1, 0
  31. std::iota(d.rbegin() + 50, d.rend(), 1);
  32. std::iota(d.rbegin(), d.rbegin() + 50, 0);
  33. print(d.begin(), d.end());
  34.  
  35. // iota(first_iterator, last_iterator, start_value) - Fills the range [first_iterator, last_iterator) with sequentially increasing values
  36. // next(forward_iterator, val) - Increment forward_iterator, val times
  37. // reverse(first_iterator, last_iterator) - To reverse elements in range [first_iterator, last_iterator).
  38.  
  39. // reorder the elements in a pseudo-random order
  40. // shuffle(first_iterator, last_iterator, generator) - Reorders the elements using the given random number generator
  41. // std::mt19937 is generator, which you can initialize with a hard-coded seed (eg. 0), to always have the same results
  42. std::mt19937 g(0);
  43. std::shuffle(d.begin(), d.end(), g);
  44. print(d.begin(), d.end());
  45.  
  46. // create a second sequence container of 50 integers (of different type than the first)
  47. std::array<int, 50> arr;
  48.  
  49. // extract 50 random numbers from the first sequence to the second one
  50. // sample(first_iterator, last_iterator, output_iterator, n, generator) - Selects n elements from the sequence [first_iterator; last_iterator) and writes those selected elements into the output_iterator
  51. std::sample(d.begin(), d.end(), arr.begin(), std::distance(arr.begin(), arr.end()), g);
  52. print(arr.begin(), arr.end());
  53.  
  54. // using the second container
  55. // find the maximum and the minimum values and their position in the container
  56. // max_element(first_iterator, last_iterator) - To find the maximum element in range [first_iterator, last_iterator).
  57. // min_element(first_iterator, last_iterator) - To find the minimum element in range [first_iterator, last_iterator).
  58. // minmax_element(first_iterator, last_iterator) - To find the minimum and the maximum elements in range [first_iterator, last_iterator).
  59. // distance(first_iterator, last_iterator) - Distance in elements between first_iterator and last_iterator
  60. auto [minItr, maxItr] = std::minmax_element(arr.begin(), arr.end());
  61. cout << "min: " << *minItr << " at [" << std::distance(arr.begin(), minItr) << "]\n";
  62. cout << "max: " << *maxItr << " at [" << std::distance(arr.begin(), maxItr) << "]\n";
  63.  
  64. // find the values which appear more than once
  65. // count(first_iterator, last_iterator, x) - To count the occurrences of x in vector.
  66. std::sort(arr.begin(), arr.end());
  67. int prev = arr[0]; bool recurring = false;
  68. std::for_each(arr.begin() + 1, arr.end(), [&prev, &recurring](int i) {
  69. if (prev == i) {
  70. if (recurring);
  71. }
  72. });
  73.  
  74. // see if 0 appears in the container
  75. // find(first_iterator, last_iterator, x) - Points to the end of the container if element is not present
  76.  
  77. // how much is the sum of all the elements?
  78. // accumulate(first_iterator, last_iterator, initial value of sum) - Does the summation of the elements
  79.  
  80. // how about the product of the elements?
  81. // accumulate(first_iterator, last_iterator, initial value, binary_function) - Fold (aggregate) elements using binary_function
  82.  
  83.  
  84. // Optimize search:
  85. // copy the elements to a container that has random iterators, than sort the container
  86. // sort(first_iterator, last_iterator) - To sort the given container using operator <
  87. // sort(first_iterator, last_iterator, binary_function) - To sort the given container using a binary_function
  88.  
  89. // search for element 25 in the container, in an optimal way
  90. // lower_bound(first_iterator, last_iterator, x) - returns an iterator pointing to the first element in the range[first, last) which has a value not less than 'x'.
  91. // upper_bound(first_iterator, last_iterator, x) - returns an iterator pointing to the first element in the range[first, last) which has a value greater than 'x'.
  92.  
  93. // try doing a permutation on the container, and see which search algorithm gives you a better result
  94. // next_permutation(first_iterator, last_iterator) - This modified the container to its next permutation.
  95. // prev_permutation(first_iterator, last_iterator) - This modified the container to its previous permutation.
  96.  
  97. cout << "\n\nHERE:\n";
  98. }
  99.  
  100. void SequenceContainerAlgorithmsExtended()
  101. {
  102. // implement iota using a generate
  103. // generate(first_iterator, last_iterator, generator) - Assigns each element in range [first_iterator, last_iterator) a value generated by generator
  104.  
  105. // implement sample using transform
  106. // transform(first_iterator, last_iterator, output_iterator, unary_op) - The unary_op is applied to the range defined by [first_iterator, last_iterator) and stores the result in another range, beginning at output_iterator.
  107.  
  108. // see if any of the tasks from SequenceContainerAlgorithms(), could have been implemented easier using one of the following:
  109. // count_if(first_iterator, last_iterator, unary_predicate) - Counts elements for which unary_predicate returns true
  110. // find_if(first_iterator, last_iterator, unary_predicate) - Finds the first element for which the unary_predicate is true
  111. // any_of(first_iterator, last_iterator, unary_predicate) - Checks if unary_predicate returns true for at least one element in the range
  112. // none_of(first_iterator, last_iterator, unary_predicate) - Checks if unary_predicate returns true for no elements in the range
  113. // all_of(first_iterator, last_iterator, unary_predicate) - Checks if unary_predicate returns true for all elements in the range
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement