Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include<deque>
- #include<numeric>
- #include<algorithm>
- #include<functional>
- #include<iostream>
- #include<random>
- #include<array>
- #include<forward_list>
- using std::deque;
- using std::cout;
- using std::endl;
- template <class FowardItr>
- void print(FowardItr begin, FowardItr end) {
- cout << "\nOutput (" << std::distance(begin, end) << "):\n";
- std::for_each(begin, end, [](auto i) { cout << i << " "; });
- cout << endl;
- }
- void SequenceContainerAlgorithms()
- {
- // create a sequence container of 100 integers (it must have random iterators!)
- deque<int> d(100);
- cout << "\n\nHERE:\n";
- // fill the vector in the following way: 50, 49, ... 2, 1, 49, 48, ... 1, 0
- std::iota(d.rbegin() + 50, d.rend(), 1);
- std::iota(d.rbegin(), d.rbegin() + 50, 0);
- print(d.begin(), d.end());
- // iota(first_iterator, last_iterator, start_value) - Fills the range [first_iterator, last_iterator) with sequentially increasing values
- // next(forward_iterator, val) - Increment forward_iterator, val times
- // reverse(first_iterator, last_iterator) - To reverse elements in range [first_iterator, last_iterator).
- // reorder the elements in a pseudo-random order
- // shuffle(first_iterator, last_iterator, generator) - Reorders the elements using the given random number generator
- // std::mt19937 is generator, which you can initialize with a hard-coded seed (eg. 0), to always have the same results
- std::mt19937 g(0);
- std::shuffle(d.begin(), d.end(), g);
- print(d.begin(), d.end());
- // create a second sequence container of 50 integers (of different type than the first)
- std::array<int, 50> arr;
- // extract 50 random numbers from the first sequence to the second one
- // 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
- std::sample(d.begin(), d.end(), arr.begin(), std::distance(arr.begin(), arr.end()), g);
- print(arr.begin(), arr.end());
- // using the second container
- // find the maximum and the minimum values and their position in the container
- // max_element(first_iterator, last_iterator) - To find the maximum element in range [first_iterator, last_iterator).
- // min_element(first_iterator, last_iterator) - To find the minimum element in range [first_iterator, last_iterator).
- // minmax_element(first_iterator, last_iterator) - To find the minimum and the maximum elements in range [first_iterator, last_iterator).
- // distance(first_iterator, last_iterator) - Distance in elements between first_iterator and last_iterator
- auto [minItr, maxItr] = std::minmax_element(arr.begin(), arr.end());
- cout << "min: " << *minItr << " at [" << std::distance(arr.begin(), minItr) << "]\n";
- cout << "max: " << *maxItr << " at [" << std::distance(arr.begin(), maxItr) << "]\n";
- // find the values which appear more than once
- // count(first_iterator, last_iterator, x) - To count the occurrences of x in vector.
- std::sort(arr.begin(), arr.end());
- int prev = arr[0]; bool recurring = false;
- std::for_each(arr.begin() + 1, arr.end(), [&prev, &recurring](int i) {
- if (prev == i) {
- if (recurring);
- }
- });
- // see if 0 appears in the container
- // find(first_iterator, last_iterator, x) - Points to the end of the container if element is not present
- // how much is the sum of all the elements?
- // accumulate(first_iterator, last_iterator, initial value of sum) - Does the summation of the elements
- // how about the product of the elements?
- // accumulate(first_iterator, last_iterator, initial value, binary_function) - Fold (aggregate) elements using binary_function
- // Optimize search:
- // copy the elements to a container that has random iterators, than sort the container
- // sort(first_iterator, last_iterator) - To sort the given container using operator <
- // sort(first_iterator, last_iterator, binary_function) - To sort the given container using a binary_function
- // search for element 25 in the container, in an optimal way
- // 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'.
- // 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'.
- // try doing a permutation on the container, and see which search algorithm gives you a better result
- // next_permutation(first_iterator, last_iterator) - This modified the container to its next permutation.
- // prev_permutation(first_iterator, last_iterator) - This modified the container to its previous permutation.
- cout << "\n\nHERE:\n";
- }
- void SequenceContainerAlgorithmsExtended()
- {
- // implement iota using a generate
- // generate(first_iterator, last_iterator, generator) - Assigns each element in range [first_iterator, last_iterator) a value generated by generator
- // implement sample using transform
- // 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.
- // see if any of the tasks from SequenceContainerAlgorithms(), could have been implemented easier using one of the following:
- // count_if(first_iterator, last_iterator, unary_predicate) - Counts elements for which unary_predicate returns true
- // find_if(first_iterator, last_iterator, unary_predicate) - Finds the first element for which the unary_predicate is true
- // any_of(first_iterator, last_iterator, unary_predicate) - Checks if unary_predicate returns true for at least one element in the range
- // none_of(first_iterator, last_iterator, unary_predicate) - Checks if unary_predicate returns true for no elements in the range
- // all_of(first_iterator, last_iterator, unary_predicate) - Checks if unary_predicate returns true for all elements in the range
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement