Skip to content

Instantly share code, notes, and snippets.

@ksvbka
Last active July 31, 2021 02:01
Show Gist options
  • Save ksvbka/4d0570874b0ba6329e982c6b97236427 to your computer and use it in GitHub Desktop.
Save ksvbka/4d0570874b0ba6329e982c6b97236427 to your computer and use it in GitHub Desktop.
Demo algorithm in STL
#include <vector>
#include <utility> // std::pair
#include <iostream>
#include <algorithm>
using namespace std;
int main(int argc, char const *argv[])
{
vector<int> v = {1, 2, 3, 4, 5, 5, 6, 7, 8, 9};
/* for_each(): Perform an operation for each element*/
auto print = [](auto const x){ cout << x << endl; };
for_each(v.begin(), v.end(), print);
/* count(): count the number of elements have value equal argument */
cout << "elements equal 5: " << count(v.begin(), v.end(), 5) << endl;
/* count_if(): return the number of element that match criterion */
auto is_odd = [](int x) { return x % 2 != 0;};
cout << "odd element:" << count_if(v.begin(), v.end(), is_odd) << endl;
/* min_element(): Return the element with the smallest value */
auto min = min_element(v.begin(), v.end());
cout << "min element: " << *min << endl;
/* max_element(): Return the element with the largest value */
auto max = max_element(v.begin(), v.end());
cout << "max element: " << *max << endl;
/* minmax_element(): Return the elements with smallest and largest value C++11 */
auto minmax = minmax_element(v.begin(), v.end());
cout << "min-max element: " << *minmax.first << " & " << *minmax.second << endl;
/* find(): searches for the first element with passed value */
auto ret = find(v.begin(), v.end(), 5);
cout << "index of first value 5: " << ret - v.begin() << endl;
/* find_if(): searches for the first element that matches a criterion */
auto first_odd = find_if(v.begin(), v.end(), is_odd);
cout << "index of first odd element: " << first_odd - v.begin() << endl;
/* find_if_not(): search for the first element that match a crierion not */
auto first_even = find_if_not(v.begin(), v.end(), is_odd);
cout << "index of first odd element: " << first_even - v.begin() << endl;
/* search_n(): searchs for the first n consecutive element with sertain properties */
auto it_n = search_n(v.begin(), v.end(), 2, 10);
auto msg = (it_n != v.end()) ? to_string(it_n - v.begin()) : "match not found";
cout << "two value 5 found at: " << msg << endl;
/* search(): searches for the first occurrence of a subrange*/
string text = "why waste time learning, when ignorance is instantaneous?";
string pattern = "learning";
auto it = search(text.begin(), text.end(), pattern.begin(), pattern.end());
cout << "Is pattern in text: " << (it != text.end() ? "True" : "Faild") << endl;
/* find_end(): Searches for the last occurrence of subrange*/
/* find_first_of(): Search for the first of several possible elements*/
/* adjacent_find(): Searches for two adjacent elements that are equal*/
/* equal(): return two ranges are equal*/
/* is_permutation() */
/* mismatch(): */
/* is_sorted(): */
cout << "Is vector sorted: " << (is_sorted(v.begin(), v.end()) ? "True": "False") << endl;
/* is_sorted_until(): return first element in range not sorted*/
/* is_partitioned(): */
/* .... */
/* all_of(): */
cout << "All vector is odd: " << (all_of(v.begin(), v.end(), is_odd) ? "True" : "False") << endl;
/* any_of(): */
cout << "Vector has odd element: " << (any_of(v.begin(), v.end(), is_odd) ? "True" : "False") << endl;
/* none_of(): */
cout << "All vector is even: " << (none_of(v.begin(), v.end(), is_odd) ? "True" : "False") << endl;
/* for_each(): Perform operation for element */
auto s = v;
for_each(s.begin(), s.end(), [](auto& x){ x *= x; });
for_each(s.begin(), s.end(), [](auto x) { cout << x << " "; });
/* copy(): Copies a range starting with the first element */
/* copy_if(): Copies element that match a criterion */
/* copy_n(): Copies n elements */
/* copy_backward(): Copies a range starting with the last element */
/* move(): Moves elements of a range starting with the first element */
/* move_backward(): Moves elements of a range starting with the first element */
/* transform(): Modifies (and copies) elements; combines elements of two ranges*/
/* merge(): merge two ranges */
/* fill(): */
/* fill_n(): */
/* generate(): */
/* generate_n(): */
/* iota(): */
/* replace(): */
/* replace_if(): */
/* replace_copy(): */
/* replace_copy_if() */
/* remove(): */
/* remove_if(): */
/* remove_copy_if(): */
/* unique(): */
/* unique_copy(): */
vector<int> ret;
unique_copy (s.begin(), s.end(), std :: back_inserter(res));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment