Standard Template Library algorithm (all functions)

Keywords: C++ Algorithm data structure STL

Catalog

Adjacent_find

All_of (returns true if all iterator intervals meet criteria, false otherwise)

Any_of (returns true if one of the iterator intervals meets the criteria, false otherwise)

Binary_search (binary search, find return true, otherwise return false)

Copy (copy an interval to the target iterator location)

Copy_backward (copy an interval from the tail to the target iterator location)

Copy_if (copies elements with qualified intervals to the target iterator location, returns the tail iterator of the target)

Copy_n (copy the first n elements from the iterator to the start of the target iterator)

Count (returns the number of elements whose iterator intervals are equal to value)

Count_if (returns the number of elements eligible for an iterator interval)

Equal (the iterator interval is compared with the target iterator location, all matches the criteria and returns true, otherwise returns false)

Equal_range (Iterator interval versus value returns a pair of groups (upper and lower bounds), with the default comparison rule equal to)

Fill (fill target values in iterator intervals)

Fill_n (fill several target values in iterator position, return to the next position pointing to the fill element in C++11)

Find (iterator interval lookup, find val, find returns a matching element, otherwise returns function, ends iterator)

Find_end (find the matching iterator interval, find the first iterator that returns a match, and find no return end iterator) goes back and forth

Find_first_of (looks for any matching element in an interval, finds an iterator that returns the first match, and finds no iterator that returns the end of the function)

Find_if (conditionally, returns the first iterator found, does not find the end of the return interval)

Find_if_not (conditionally, returns the first mismatched iterator, and all matches return the end of the interval)

For_each (traversal)

Generate (return value assigned to iterator interval)

Generate_n (return value assigns n to iterator specified location)

Include (two sorted sequences, comparing intervals, returning true for all matches or false for all matches) is to find a subset when the collation is the same

Inplace_merge (merges two ordered into one ordered) merge and sort

Is_heap (determines whether it is a binary heap, returns true, not false) heap sort

Is_heap_until (returns the first iterator in the iterator interval that destroys the binary heap structure element, all conforming to the return end iterator)

Is_partitioned (true if the condition is satisfied or false if it is not satisfied)

Is_permutation (ratio of direction between interval elements and target elements, all match return true, otherwise return false. independent of location)

Is_sorted (returns true if ordered by rules, false otherwise)

Is_sorted_until (returns the first element iterator that is not sorted by rule, and the end iterator if both are sorted by rule)

Iter_swap (exchanging elements pointed to by two container iterators)

Lexicographical_compare (compares two intervals by rule, returns bool)

Lower_bound (returns >= element, does not match end iterator within return function)

Make_heap (rearrange to make a heap)

Max (returns a larger value)

Max_element (returns maximum value of interval)

Merge (two ordered merges into a new one according to the rules)

Min (returns smaller value)

Minmax (returns a pair of groups, maximum and minimum)

Minmax_element (an iterator that returns a pair of groups pointing to the minimum and maximum value with an iterator interval)

Min_element (returns an iterator pointing to the minimum with an interval parameter)

Mismatch (an iterator that returns a pair of groups with two mismatched numbers)

Move (moves an interval element to a specified position, returns the end iterator of the moving target)

Move_backwrad (move backward and forward)

Next_permutation (returns true if sorted by dictionary, false if sorted by maximum)

None_of (returns true if none of the elements in the interval does not match, otherwise returns false)

Nth_element (re-iterator position begins to split, front and back are ordered regularly (but not sorted)

Partial_sort (partial sort, four for all data)

Partial_sort_copy (copy to a container after partial sorting, top four for sorting)

Partition (divides the interval into regular intervals, returning the end iterator of the division)

Partition_copy (copy results to two containers)

Partition_point (splitting elements, returning the end iterator of the first part of splitting elements)

Pop_heap (after moving the top element to a range, it can be interpreted as a pop-up)

Prev_parmutation (can be arranged in a dictionary to return true, or false if it is already the largest arrangement) prev 321 is arranged 123 next 123 to 321

Push_heap (add heap, make add before call, predicate consistent)

Random_shuffle

Remove (removes an element, but does not actually delete it, returns the last iterator pointing to the deleted element)

Remove_copy (copy can be moved to a new container)

Remove_copy_if (move copies to new containers by rule)

Remove_if (remove by rule)

Replace

Replace_copy (replace copy to new container)

Replace_copy_if (Replace copies to new containers by rules)

Replace_if (replace by rule)

Reverse

Reverse_copy (copy upside down to a new container)

Rotate

Rotate_copy (rotate to copy into a new container)

Search (Find another interval in one interval, find the iterator that returns the found, do not find the iterator that returns the end)

Search_n (Looks for consecutive numbers in an interval, finds an iterator to return to, finds no end iterator to return to)

Set_difference (difference set) (an element that does not exist between one interval and another, assigned to a container, returns the end iterator of the newly constructed container)

Set_intersection (intersection)

Set_symmetric_difference (symmetric union (different elements))

Set_union (union)

Shuffle

Sort (sort)

Sort_heap (heap sorting)

Stable_partition (Split returns the first iterator of the split (the same element does not change position)

Stable_sort (sort (same elements do not change position)

Swap (exchange two containers)

Swap_ranges (can be swapped by interval)

Transform (converting elements to rule elements)

Unique (adjacent elements weighted by rules)

Unique_copy (can be copied to a new container)

Upper_bound (element iterator returning >)

It's a versatile thing, but I'll put it all together. It's used in comparative development.

Correctness is guaranteed and code readability greatly increases.

There may be some problems with the verbal narrative because there are so many.

Header file: #include <algorithm>

Adjacent_find

Find two adjacent elements, you can change the search rule with predicates. Successfully return the iterator that finds the first element, and fail to return the tail iterator.

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

class Compare
{
public:
	bool operator()(int val, int val2)
	{
		return val > val2;
	}
};

int main()
{
	vector<int> v = { 1,2,3,4,4,5,7,6,9,8,8 };
	auto it = v.begin();

	it = adjacent_find(v.begin(), v.end());

	cout << *it << endl;	//4

	it = adjacent_find(it, v.end(), Compare());

	cout << *it << endl;	//7

	return 0;
}

All_of (returns true if all iterator intervals meet criteria, false otherwise)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,6,7 };
	//[] (int val) {return Val > 0;}) The lambda expression is not known, but it should all be understood.
	if (all_of(v.begin(), v.end(), [](int val) {return val > 0; }))
	{
		cout << "return val > 0" << endl;	//Will execute
	}

	if (all_of(v.begin(), v.end(), [](int val) {return val > 1; }))
	{
		cout << "return val > 1" << endl;	//Will not execute
	}

	return 0;
}

Any_of (returns true if one of the iterator intervals meets the criteria, false otherwise)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,6,7 };
	
	if (any_of(v.begin(), v.end(), [](int val) {return val == 7; }))
	{
		cout << "return val == 7" << endl;	//Will execute
	}

	if (any_of(v.begin(), v.end(), [](int val) {return val < 0; }))
	{
		cout << "return val < 0" << endl;	//Will not execute
	}

	return 0;
}

Binary_search (binary search, find return true, otherwise return false)

After dichotomy finds what it is, say in the data structure

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	//To use binary lookup, the sequence must be ordered
	vector<int> v = { 1,2,3,4,5,6,7 };

	if (binary_search(v.begin(), v.end(), 6))
	{
		cout << "found" << endl;
	}

	return 0;
}

Copy (copy an interval to the target iterator location)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v2 = { 3,2,4,5,6,7,8,9 };
	int arr[] = { 1,2,3,4,5,6 };
	vector<int> v(v2.size());

	copy(v2.begin(), v2.end(), v.begin());
	for (auto& val : v)
	{
		cout << val << " ";
	}
	cout << endl;

	v.resize(sizeof(int[6]) / sizeof(int));
	copy(arr, arr + 6, v.begin());
	for (auto& val : v)
	{
		cout << val << " ";
	}
	cout << endl;

	return 0;
}

Copy_backward (copy an interval from the tail to the target iterator location)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	int arr[] = { 1,2,3,4,5 };
	vector<int> v = { 1,2,3 };

	//v.resize(8);
	//copy_backward(arr, arr + 5, v.begin());//Collapse starts at the tail of the target and the tail of the interval, replicates back to front

	v.resize(5);
	copy_backward(arr, arr + 3, v.end());
	for (auto& val : v)
	{
		cout << val << " ";	//12123
	}
	cout << endl;

	return 0;
}

Copy_if (copies elements with qualified intervals to the target iterator location, returns the tail iterator of the target)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v;
	vector<int> v2 = { 1,2,3,4,5,6,7 };
	v.resize(v2.size());

	auto it = copy_if(v2.begin(), v2.end(), v.begin(), [](int val) {return val % 2; });
	v.resize(distance(v.begin(), it));	//Number of reassigned elements
	for (auto& val : v)
	{
		cout << val << " ";	//1357
	}
	cout << endl;

	return 0;
}

Copy_n (copy the first n elements from the iterator to the start of the target iterator)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v;
	vector<int> v2 = { 1,2,3,4,5,6,7 };
	v.resize(v2.size());

	copy_n(v2.begin(), v2.size(), v.begin());
	for (auto& val : v)
	{
		cout << val << " ";	//1234567
	}
	cout << endl;

	return 0;
}

Count (returns the number of elements whose iterator intervals are equal to value)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,5,5,6,7 };

	int my_count = count(v.begin(), v.end(), 5);
	cout << my_count << endl;	//3

	return 0;
}

Count_if (returns the number of elements eligible for an iterator interval)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,5,5,6,7 };

	int my_count = count_if(v.begin(), v.end(), [](int val) {return val % 2; });
	cout << my_count << endl;	//6

	return 0;
}

Equal (the iterator interval is compared with the target iterator location, all matches the criteria and returns true, otherwise returns false)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,5,5,6,7 };
	vector<int> v2 = { 1,2,3,4,5,5,5,6,7 };
	vector<int> v3 = { 11,21,31,41,15,51,51,61,17 };

	if (equal(v.begin(), v.end(), v2.begin()))
	{
		cout << "equal(v.begin(), v.end(), v2.begin())" << endl;	//Will execute
	}

	if (equal(v.begin(), v.end(), v3.begin(), [](int val, int val2) {return val < val2; }))
	{
		cout << "equal(v.begin(), v.end(), v3.begin(), [](int val, int val2) {return val < val2; })" << endl;	//Will execute
	}

	return 0;
}

Equal_range (Iterator interval versus value returns a pair of groups (upper and lower bounds), with the default comparison rule equal to)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	//Must be ordered
	vector<int> v = { 1,2,2,2,2,2,3,3,3,3,4,5,6 };
	auto ret = equal_range(v.begin(), v.end(), 2);
	cout << *ret.first << endl;	//2;
	cout << *ret.second << endl;	//3

	//3<1
	auto ret2 = equal_range(v.begin(), v.end(), 3, [](int val, int val2) {return val < val2; });
	cout << *ret2.first << endl;	//3
	cout << *ret2.second << endl;	//4

	return 0;
}

Fill (fill target values in iterator intervals)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v(5);
	fill(v.begin(), v.end(), 11);
	for (auto& val : v)
	{
		cout << val << " ";	//11 11 11 11 11
	}
	cout << endl;

	return 0;
}

Fill_n (fill several target values in iterator position, return to the next position pointing to the fill element in C++11)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5 };
	fill_n(v.begin(), 3, 11);
	for (auto& val : v)
	{
		cout << val << " ";	//11 11 11 4 5
	}
	cout << endl;

	return 0;
}

Find (iterator interval lookup, find val, find returns a matching element, otherwise returns function, ends iterator)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5 };
	auto it = find(v.begin(), v.begin() + 4, 4);
	if (it != v.begin() + 4)
	{
		cout << *it << endl;	//4
	}

	return 0;
}

Find_end (find the matching iterator interval, find the first iterator that returns a match, and find no return end iterator) goes back and forth

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,1,2,3,4,5 };
	vector<int> v2 = { 2,3,4 };
	auto it = find_end(v.begin(), v.end(), v2.begin(), v2.end());
	if (it != v.end())
	{
		cout << *it << endl;	//2
		cout << it - v.begin() << endl;	//6
	}

	auto it2 = find_end(v.begin(), v.end(), v2.begin(), v2.end(), [](int val, int val2) {return val > val2; });

	if (it2 != v.end())
	{
		cout << *it2 << endl;	//3
		cout << it2 - v.begin() << endl;	//7
	}

	return 0;
}

Find_first_of (looks for any matching element in an interval, finds an iterator that returns the first match, and finds no iterator that returns the end of the function)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5 };
	vector<int> v2 = { 9,5,11 };
	auto it = find_first_of(v.begin(), v.end(), v2.begin(), v2.end());
	if (it != v.end())
	{
		cout << *it << endl;	//5
	}

	auto it2 = find_first_of(v.begin(), v.end(), v2.begin(), v2.end(), [](int val, int val2) {return ((val + 4) == val2); });
	if (it2 != v.end())
	{
		cout << *it2 << endl;	//1
	}

	return 0;
}

Find_if (conditionally, returns the first iterator found, does not find the end of the return interval)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5 };
	auto it = find_if(v.begin(), v.end(), [](int val) {return val > 3; });
	if (it != v.end())
	{
		cout << *it << endl;    //4
	}

	return 0;
}

Find_if_not (conditionally, returns the first mismatched iterator, and all matches return the end of the interval)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5 };
	auto it = find_if_not(v.begin(), v.end(), [](int val) {return val > 3; });
	if (it != v.end())
	{
		cout << *it << endl;	//1
	}

	return 0;
}

For_each (traversal)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,6 };
	for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
	cout << endl;

	return 0;
}

Generate (return value assigned to iterator interval)

#include <iostream>
#include <vector>
#include <algorithm>
#include <time.h>

using namespace std;

int main()
{
	vector<int> v(10);
	srand((unsigned int)time(NULL));

	generate(v.begin(), v.end(), []() {return rand() % 10; });
	for (auto& val : v)
	{
		cout << val << " ";	//Random 10 Numbers
	}
	cout << endl;

	return 0;
}

Generate_n (return value assigns n to iterator specified location)

#include <iostream>
#include <vector>
#include <algorithm>
#include <time.h>

using namespace std;

int main()
{
	vector<int> v(10);
	srand((unsigned int)time(NULL));

	generate_n(v.begin(), 10, []() {return rand() % 10; });
	for (auto& val : v)
	{
		cout << val << " ";	//Random 10 Numbers
	}
	cout << endl;

	return 0;
}

Include (two sorted sequences, comparing intervals, returning true for all matches or false for all matches) is to find a subset when the collation is the same

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,6,7,8 };
	vector<int> v2 = { 2,3,4,5 };

	if (includes(v.begin(), v.end(), v2.begin(), v2.end()))
	{
		cout << "includes(v.begin(), v.end(), v2.begin(), v2.end())" << endl;
	}

	if (includes(v.begin(), v.end(), v2.begin(), v2.end(), [](int val, int val2) {return val < val2; }))
	{
		cout << "includes(v.begin(), v.end(), v2.begin(), v2.end(), [](int val, int val2) {return val < val2; })" << endl;
	}

	return 0;
}

Inplace_merge (merges two ordered into one ordered) merge and sort

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	//Sort two ordered sequences
	vector<int> v = { 1,2,4,6,8,0,2,5,7,9 };
	inplace_merge(v.begin(), v.begin() + 5, v.end());
	for (auto& val : v)
	{
		cout << val << " ";	//0 1 2 2 4 5 6 7 8 9
	}
	cout << endl;

	inplace_merge(v.begin(), v.begin() + 5, v.end(), [](int val, int val2) {return val < val2; });
	for (auto& val : v)
	{
		cout << val << " ";	//0 1 2 2 4 5 6 7 8 9
	}
	cout << endl;

	return 0;
}

Is_heap (determines whether it is a binary heap, returns true, not false) heap sort

What is a binary heap after is said in the data structure.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,6,7 };
	vector<int> v2 = { 7,6,5,4,3,2,1 };

	if (is_heap(v.begin(), v.end()))
	{
		cout << "is_heap(v.begin(), v.end()) is true" << endl;
	}
	else
	{
		cout << "is_heap(v.begin(), v.end()) is false" << endl;    //implement
	}

	if (is_heap(v.begin(), v.end(), [](int val, int val2) {return val > val2;}))
	{
		cout << "is_heap(v.begin(), v.end()) is true" << endl;    //implement
	}
	else
	{
		cout << "is_heap(v.begin(), v.end()) is false" << endl;
	}

	if (is_heap(v2.begin(), v2.end()))
	{
		cout << "is_heap(v2.begin(), v2.end()) is true" << endl;    //implement
	}
	else
	{
		cout << "is_heap(v2.begin(), v2.end()) is false" << endl;
	}

	return 0;
}

Is_heap_until (returns the first iterator in the iterator interval that destroys the binary heap structure element, all conforming to the return end iterator)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,6,7 };
	vector<int> v2 = { 7,6,5,4,3,2,1 };

	auto it = is_heap_until(v.begin(), v.end());
	cout << *it << endl;	//2

	auto it2 = is_heap_until(v2.begin(), v2.end());
	cout << *(it2-1) << endl;	//1

	return 0;
}

Is_partitioned (true if the condition is satisfied or false if it is not satisfied)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,3,5,7,2,4,6,8 };
	if (is_partitioned(v.begin(), v.end(), [](int val) {return val % 2; }))
	{
		cout << "is_partitioned(v.begin(), v.end(), [](int val) {return val % 2; })" << endl;    //implement
	}

	return 0;
}

Is_permutation (ratio of direction between interval elements and target elements, all match return true, otherwise return false. independent of location)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5 };
	vector<int> v2 = { 3,4,2,1,5 };

	if (is_permutation(v.begin(), v.end(), v2.begin()))
	{
		cout << "is_permutation(v.begin(), v.end(), v2.begin())" << endl;    //implement
	}

	v = { 2,3,4,5,6 };
	if (is_permutation(v.begin(), v.end(), v2.begin(), [](int val, int val2) {return val > val2; }))
	{
		cout << "is_permutation(v.begin(), v.end(), v2.begin(), [](int val, int val2) {return val > val2; })" << endl;    //implement
	}

	return 0;
}

Is_sorted (returns true if ordered by rules, false otherwise)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5 };
	vector<int> v2 = { 5,4,3,2,1 };

	if (is_sorted(v.begin(), v.end()))
	{
		cout << "is_sorted(v.begin(), v.end())" << endl;	//implement
	}

	if (is_sorted(v2.begin(), v2.end(), [](int val, int val2) {return val > val2; }))
	{
		cout << "v2.begin(), v2.end(), [](int val, int val2) {return val > val2; })" << endl;	//implement
	}

	return 0;
}

Is_sorted_until (returns the first element iterator that is not sorted by rule, and the end iterator if both are sorted by rule)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,2,3,1 };
	auto it = is_sorted_until(v.begin(), v.end());
	cout << *it << endl;	//2

	return 0;
}

Iter_swap (exchanging elements pointed to by two container iterators)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3 };
	vector<int> v2 = { 3,4,5 };
	iter_swap(v.begin(), v2.begin());
	for (auto& val : v)
	{
		cout << val << " ";
	}
	cout << endl;

	for (auto& val : v2)
	{
		cout << val << " ";
	}
	cout << endl;

	return 0;
}

Lexicographical_compare (compares two intervals by rule, returns bool)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3 };
	vector<int> v2 = { 3,4,5 };
	
	if (lexicographical_compare(v.begin(), v.end(), v2.begin(), v2.end()))
	{
		cout << "lexicographical_compare(v.begin(), v.end(), v2.begin(), v2.end())" << endl;	//Will execute
	}

	if (!lexicographical_compare(v.begin(), v.end(), v2.begin(), v2.end(), [](int val, int val2) {return val > val2; }))
	{
		cout << "!lexicographical_compare(v.begin(), v.end(), v2.begin(), v2.end(), [](int val, int val2) {return val > val2; })" << endl;	//Will execute
	}

	return 0;
}

Lower_bound (returns >= element, does not match end iterator within return function)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,4,5 };
	auto it = lower_bound(v.begin(), v.end(), 3);
	cout << *it << endl;	//4

	auto it2 = lower_bound(v.begin(), v.end(), 3, [](int val, int val2) {return val < val2; });
	cout << *it2 << endl;	//4

	return 0;
}

Make_heap (rearrange to make a heap)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,4,2,3,6,8,9 };
	
	make_heap(v.begin(), v.end());
	for (auto& val : v)
	{
		cout << val << " ";	//9 6 8 3 4 1 2
	}
	cout << endl;

	return 0;
}

Max (returns a larger value)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,4,2,3,6,8,9 };

	cout << max(v[1], v[2]) << endl;	//4

	return 0;
}

Max_element (returns maximum value of interval)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,4,2,3,6,8,9 };

	cout << *(max_element(v.begin(), v.end())) << endl;	//9

	return 0;
}

Merge (two ordered merges into a new one according to the rules)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 0,1,2,3,4 };
	vector<int> v2 = { 5,6,7,8,9 };
	vector<int> v3(10);
	merge(v.begin(), v.end(), v2.begin(), v2.end(), v3.begin());
	for (auto& val : v3)
	{
		cout << val << " ";    //0123456789
	}
	cout << endl;

	vector<int> v4 = { 4,3,2,1,0 };
	vector<int> v5 = { 9,8,7,6,5 };
	vector<int> v6(10);
	merge(v4.begin(), v4.end(), v5.begin(), v5.end(), v6.begin(), [](int val, int val2) {return val > val2; });
	for (auto& val : v6)
	{
		cout << val << " ";    //9876543210
	}
	cout << endl;

	return 0;
}

Min (returns smaller value)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 0,1,2,3,4 };
	
	cout << min(v[2], v[3]) << endl;	//2

	return 0;
}

Minmax (returns a pair of groups, maximum and minimum)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	auto ret = minmax({ 3,2,5,6,1,8,9 });
	cout << ret.first << " " << ret.second << endl;    //1 9
	auto ret2 = minmax(4, 5);
	cout << ret2.first << " " << ret2.second << endl;    //4 5

	return 0;
}

Minmax_element (an iterator that returns a pair of groups pointing to the minimum and maximum value with an iterator interval)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,6,2,4,3,8,9 };
	auto ret = minmax_element(v.begin(), v.end());
	cout << *ret.first << " " << *ret.second << endl;    //1 9

	return 0;
}

Min_element (returns an iterator pointing to the minimum with an interval parameter)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,6,2,4,3,8,9 };
	auto ret = min_element(v.begin(), v.end());    
	cout << *ret << endl;    //1

	return 0;
}

Mismatch (an iterator that returns a pair of groups with two mismatched numbers)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5 };
	vector<int> v2 = { 1,2,3,6,7 };
	auto ret = mismatch(v.begin(), v.end(), v2.begin());
	cout << *ret.first << " " << *ret.second << endl;	//4 6

	vector<int> v3 = { 11,12,3,1,2 };
	auto ret2 = mismatch(v.begin(), v.end(), v3.begin(), [](int val, int val2) {return val < val2; });
	cout << *ret2.first << " " << *ret2.second << endl;	//3 3

	return 0;
}

Move (moves an interval element to a specified position, returns the end iterator of the moving target)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5 };
	vector<int> v2(5);

	move(v.begin(), v.end(), v2.begin());
	for (auto val : v2)
	{	
		cout << val << " ";	//12345
	}
	cout << endl;

	for (auto val : v)
	{
		cout << val << " ";	//12345
	}
	cout << endl;

	return 0;
}

Move_backwrad (move backward and forward)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5 };
	vector<int> v2(7);
	
	move_backward(v.begin(), v.end(), v2.end());
	for (auto val : v2)
	{
		cout << val << " ";	//0012345
	}
	cout << endl;

	return 0;
}

Next_permutation (returns true if sorted by dictionary, false if sorted by maximum)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 8,3,9 };

	if (next_permutation(v.begin(), v.end()))
	{
		for (auto val : v)
		{
			cout << val << " ";	//893
		}
		cout << endl;
	}

	return 0;
}

None_of (returns true if none of the elements in the interval does not match, otherwise returns false)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 8,3,9 };

	if (none_of(v.begin(), v.end(), [](int val) {return val > 10; }))
	{
		cout << "none_of(v.begin(), v.end(), [](int val) {return val > 10; })" << endl;	//Will execute
	}

	return 0;
}

Nth_element (re-iterator position begins to split, front and back are ordered regularly (but not sorted)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 8,3,9 };
	
	nth_element(v.begin(), v.begin() + 2, v.end());
	for (auto val : v)
	{
		cout << val << " ";	//389
	}
	cout << endl;

	nth_element(v.begin(), v.begin() + 2, v.end(), [](int val, int val2) {return val > val2; });
	for (auto val : v)
	{
		cout << val << " ";	//983
	}
	cout << endl;

	return 0;
}

Partial_sort (partial sort, four for all data)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,5,8,3,9,2,3 };
	partial_sort(v.begin(), v.begin() + 4, v.end());
	for (auto val : v)
	{
		cout << val << " ";	//1 2 3 3 9 8 5
	}
	cout << endl;

	partial_sort(v.begin(), v.begin() + 4, v.end(), [](int val, int val2) {return val > val2; });
	for (auto val : v)
	{
		cout << val << " ";	//9 8 5 3 1 2 3
	}
	cout << endl;

	return 0;
}

Partial_sort_copy (copy to a container after partial sorting, top four for sorting)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,5,8,3,9,2,3 };
	vector<int> v2(4);

	partial_sort_copy(v.begin(), v.begin() + 4, v2.begin(), v2.end());
	for (auto val : v2)
	{
		cout << val << " ";	//1 3 5 8
	}
	cout << endl;

	partial_sort_copy(v.begin(), v.begin() + 4, v2.begin(), v2.end(), [](int val, int val2) {return val > val2; });
	for (auto val : v2)
	{
		cout << val << " ";	//8 5 3 1
	}
	cout << endl;

	return 0;
}

Partition (divides the interval into regular intervals, returning the end iterator of the division)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,5,8,3,9,2,3 };
	
	auto it = partition(v.begin(), v.end(), [](int val) {return val > 4; });
	for (auto i = v.begin(); i != it; ++i)
	{
		cout << *i << " ";	//9 5 8
	}
	cout << endl;

	for (auto i = it; i != v.end(); ++i)
	{
		cout << *i << " ";	//3 1 2 3
	}
	cout << endl;

	return 0;
}

Partition_copy (copy results to two containers)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,5,8,3,9,2,3 };

	int my_size = count_if(v.begin(), v.end(), [](int val) {return val > 4; });
	vector<int> v2(my_size);
	vector<int> v3(v.size() - v2.size());

	partition_copy(v.begin(), v.end(), v2.begin(), v3.begin(), [](int val) {return val > 4; });
	for (auto val : v2)
	{
		cout << val << " ";	//5 8 9
	}
	cout << endl;

	for (auto val : v3)
	{
		cout << val << " ";	//1 3 2 3
	}
	cout << endl;

	return 0;
}

Partition_point (splitting elements, returning the end iterator of the first part of splitting elements)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,5,8,3,9,2,3 };

	//auto it = partition_point(v.begin(), v.end(), [](int val) {return val > 4; });

	//for (auto i = v.begin(); i != it; ++i)
	//{
	//	cout << *i << " ";	//1 5 8 
	//}
	//cout << endl;

	//Split point
	partition(v.begin(),v.end(), [](int val) {return val > 4; });
	auto it = partition_point(v.begin(), v.end(), [](int val) {return val > 4; });

	for (auto i = v.begin(); i != it; ++i)
	{
		cout << *i << " ";	//9 5 8
	}
	cout << endl;

	return 0;
}

Pop_heap (after moving the top element to a range, it can be interpreted as a pop-up)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,5,8,3,9,2,3 };

	make_heap(v.begin(), v.end());

	for (auto val : v)
	{
		cout << val << " ";	//9 5 8 3 1 2 3
	}
	cout << endl;

	cout << v.front() << endl;	//9
	pop_heap(v.begin(), v.end());
	cout << v.front() << endl;	//8

	return 0;
}

Prev_parmutation (can be arranged in a dictionary to return true, or false if it is already the largest arrangement) prev 321 is arranged 123 next 123 to 321

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 3,2,1 };

	if (prev_permutation(v.begin(), v.end()))
	{
		for (auto val : v)
		{
			cout << val << " ";	//3 1 2
		}
		cout << endl;
	}

	return 0;
}

Push_heap (add heap, make add before call, predicate consistent)

That's it, the data structure is not said, it's okay not to understand it, C++ is done, the next step is data structure

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 3,2,1 };

	make_heap(v.begin(), v.end());

	for (auto val : v)
	{
		cout << val << " ";	//3 2 1
	}
	cout << endl;

	v.emplace_back(9);

	for (auto val : v)
	{
		cout << val << " ";	//3 2 1 9
	}
	cout << endl;

	push_heap(v.begin(), v.end());

	for (auto val : v)
	{
		cout << val << " ";	//9 3 2 1
	}
	cout << endl;

	return 0;
}

Random_shuffle

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,6,7,8,9 };
	
	random_shuffle(v.begin(), v.end());
	for (auto val : v)
	{
		cout << val << " ";
	}
	cout << endl;

	//This will make each randomization different
	random_shuffle(v.begin(), v.end(), [](int val) {
		srand((unsigned int)time(NULL));
		return rand() % val; 
		});
	for (auto val : v)
	{
		cout << val << " ";
	}
	cout << endl;

	return 0;
}

Remove (removes an element, but does not actually delete it, returns the last iterator pointing to the deleted element)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,5,6,7,3,2,1,6,8 };

	cout << v.size() << endl;	//10

	auto it = remove(v.begin(), v.end(), 1);

	cout << v.size() << endl;	//10

	cout << *(it+1) << endl;	//8

	for (auto val : v)
	{
		cout << val << " ";	//2 5 6 7 3 2 6 8 6 8
	}
	cout << endl;

	return 0;
}

Remove_copy (copy can be moved to a new container)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,5,6,7,3,2,1,6,8 };
	vector<int> v2(v.size());

	remove_copy(v.begin(), v.end(), v2.begin(), 1);
	for (auto val : v2)
	{
		cout << val << " ";	//2 5 6 7 3 2 6 8 0 0
	}
	cout << endl;

	return 0;
}

Remove_copy_if (move copies to new containers by rule)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,5,6,7,3,2,1,6,8 };
	vector<int> v2(v.size());

	remove_copy_if(v.begin(), v.end(), v2.begin(), [](int val) {return val > 5; });
	for (auto val : v2)
	{
		cout << val << " ";	//1 2 5 3 2 1 0 0 0 0
	}
	cout << endl;

	return 0;
}

Remove_if (remove by rule)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 2,4,5,6,2,23,3,4,5,2 };
	auto it = remove_if(v.begin(), v.end(), [](int val) {return val < 5; });
	for (auto val : v)
	{
		cout << val << " ";	//23 4 5 6 2 23 3 4 5 2
	}
	cout << endl;

	for (auto i = v.begin(); i != it; ++i)
	{
		cout << *i << " ";	//5 6 23 5
	}
	cout << endl;

	return 0;
}

Replace

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,5,6,7,3,2,1,6,8 };

	replace(v.begin(), v.end(), 1, 1111);

	for (auto val : v)
	{
		cout << val << " ";	//1111 2 5 6 7 3 2 1111 6 8
	}
	cout << endl;

	return 0;
}

Replace_copy (replace copy to new container)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,5,6,7,3,2,1,6,8 };
	vector<int> v2(v.size());

	replace_copy(v.begin(), v.end(), v2.begin(), 1, 1111);

	for (auto val : v2)
	{
		cout << val << " ";	//1111 2 5 6 7 3 2 1111 6 8
	}
	cout << endl;

	return 0;
}

Replace_copy_if (Replace copies to new containers by rules)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,5,6,7,3,2,1,6,8 };
	vector<int> v2(v.size());

	replace_copy_if(v.begin(), v.end(), v2.begin(), [](int val) {return val < 5; }, 1111);

	for (auto val : v2)
	{
		cout << val << " ";	//1111 1111 5 6 7 1111 1111 1111 6 8
	}
	cout << endl;

	return 0;
}

Replace_if (replace by rule)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,5,6,7,3,2,1,6,8 };

	replace_if(v.begin(), v.end(), [](int val) {return val < 5; }, 1111);

	for (auto val : v)
	{
		cout << val << " ";	//1111 1111 5 6 7 1111 1111 1111 6 8
	}
	cout << endl;

	return 0;
}

Reverse

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,6,7,8,9 };

	reverse(v.begin(), v.end());

	for (auto val : v)
	{
		cout << val << " ";	//9 8 7 6 5 4 3 2 1
	}
	cout << endl;

	return 0;
}

Reverse_copy (copy upside down to a new container)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,6,7,8,9 };
	vector<int> v2(v.size());

	reverse_copy(v.begin(), v.end(), v2.begin());

	for (auto val : v2)
	{
		cout << val << " ";	//9 8 7 6 5 4 3 2 1
	}
	cout << endl;

	return 0;
}

Rotate

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,6,7,8,9 };

	rotate(v.begin(), v.begin()+5, v.end());

	for (auto val : v)
	{
		cout << val << " ";	//6 7 8 9 1 2 3 4 5
	}
	cout << endl;

	return 0;
}

Rotate_copy (rotate to copy into a new container)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,6,7,8,9 };
	vector<int> v2(v.size());

	rotate_copy(v.begin(), v.begin() + 5, v.end(),v2.begin());

	for (auto val : v2)
	{
		cout << val << " ";	//6 7 8 9 1 2 3 4 5
	}
	cout << endl;

	return 0;
}

Search (Find another interval in one interval, find the iterator that returns the found, do not find the iterator that returns the end)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,6,7,8,9 };
	vector<int> v2 = { 3,4,5,6 };

	auto it = search(v.begin(), v.end(), v2.begin(), v2.end());

	if (it != v.end())
	{
		cout << *it << endl;	//3
	}

	auto it2 = search(v.begin(), v.end(), v2.begin(), v2.end(), [](int val, int val2) {return val < val2; });
	if (it2 != v.end())
	{
		cout << *it2 << endl;	//1
	}

	return 0;
}

Search_n (Looks for consecutive numbers in an interval, finds an iterator to return to, finds no end iterator to return to)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,4,4,5,6,7,8,9 };

	auto it = search_n(v.begin(), v.end(), 2, 4);

	if (it != v.end())
	{
		cout << *it << endl;	//4
		cout << *++it << endl;	//4
		cout << *++it << endl;	//4
		cout << *++it << endl;	//5
	}

	auto it2 = search_n(v.begin(), v.end(), 2, 4, [](int val, int val2) {return val > val2; });

	if (it2 != v.end())
	{
		cout << *it2 << endl;	//5
		cout << *++it2 << endl;	//6
		cout << *++it2 << endl;	//7
		cout << *++it2 << endl;	//8
	}

	return 0;
}

Set_difference (difference set) (an element that does not exist between one interval and another, assigned to a container, returns the end iterator of the newly constructed container)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,6,7,8,9 };
	vector<int> v2 = { 3,4,5,6 };

	vector<int> v3(v.size());

	auto it = set_difference(v.begin(), v.end(), v2.begin(), v2.end(), v3.begin());

	for (auto val : v3)
	{
		cout << val << " ";	//1 2 7 8 9 0 0 0 0
	}
	cout << endl;

	v3.resize(it - v3.begin());

	for (auto val : v3)
	{
		cout << val << " ";	//1 2 7 8 9
	}
	cout << endl;

	return 0;
}

Set_intersection (intersection)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,6,7,8,9 };
	vector<int> v2 = { 3,4,5,6,11,22,33 };

	vector<int> v3(v2.size());

	auto it = set_intersection(v.begin(), v.end(), v2.begin(), v2.end(), v3.begin());

	v3.resize(it - v3.begin());

	for (auto val : v3)
	{
		cout << val << " ";    //3 4 5 6
	}
	cout << endl;

	return 0;
}

Set_symmetric_difference (symmetric union (different elements))

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,6,7,8,9 };
	vector<int> v2 = { 3,4,5,6,11,22,33 };

	vector<int> v3(v2.size() + v.size());

	auto it = set_symmetric_difference(v.begin(), v.end(), v2.begin(), v2.end(), v3.begin());

	v3.resize(it - v3.begin());

	for (auto val : v3)
	{
		cout << val << " ";	//1 2 7 8 9 11 22 33
	}
	cout << endl;

	return 0;
}

Set_union (union)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,6,7,8,9 };
	vector<int> v2 = { 3,4,5,6,11,22,33 };

	vector<int> v3(v2.size() + v.size());

	auto it = set_union(v.begin(), v.end(), v2.begin(), v2.end(), v3.begin());

	v3.resize(it - v3.begin());

	for (auto val : v3)
	{
		cout << val << " ";	//1 2 3 4 5 6 7 8 9 11 22 33
	}
	cout << endl;

	return 0;
}

Shuffle

#include <iostream>     
#include <algorithm>    
#include <vector>       
#include <random>        

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5 };

	random_device r;

	shuffle(v.begin(), v.end(), default_random_engine(r()));

	for (auto val : v)
	{
		cout << val << " ";
	}

	cout << endl;

	return 0;
}

Sort (sort)

#include <iostream>     
#include <algorithm>    
#include <vector>          

using namespace std;

int main()
{
	vector<int> v = { 5,1,5,7,5,3,8,2 };
	sort(v.begin(), v.end());
	for (auto val : v)
	{
		cout << val << " ";	//1 2 3 5 5 5 7 8
	}
	cout << endl;

	vector<int> v2 = { 5,1,5,7,5,3,8,2 };
	sort(v2.begin(), v2.end(), [](int val, int val2) {return val > val2; });
	for (auto val : v2)
	{
		cout << val << " ";	//8 7 5 5 5 3 2 1
	}
	cout << endl;

	return 0;
}

Sort_heap (heap sorting)

#include <iostream>     
#include <algorithm>    
#include <vector>          

using namespace std;

int main()
{
	vector<int> v = { 5,1,5,7,5,3,8,2 };
	make_heap(v.begin(), v.end());
	sort_heap(v.begin(), v.end());
	for (auto val : v)
	{
		cout << val << " ";	//1 2 3 5 5 5 7 8
	}
	cout << endl;

	vector<int> v2 = { 5,1,5,7,5,3,8,2 };
	make_heap(v2.begin(), v2.end(), [](int val, int val2) {return val > val2; });
	sort_heap(v2.begin(), v2.end(), [](int val, int val2) {return val > val2; });
	for (auto val : v2)
	{
		cout << val << " ";	//8 7 5 5 5 3 2 1
	}
	cout << endl;

	return 0;
}

Stable_partition (Split returns the first iterator of the split (the same element does not change position)

#include <iostream>     
#include <algorithm>    
#include <vector>          

using namespace std;

int main()
{
	vector<int> v = { 5,1,5,7,5,3,8,2 };

	auto it = stable_partition(v.begin(), v.end(), [](int val) {return val > 5; });
	for (auto i = v.begin(); i != it; ++i)
	{
		cout << *i << " ";	//7 8
	}
	cout << endl;

	for (auto i = it; i != v.end(); ++i)
	{
		cout << *i << " ";	//5 1 5 5 3 2
	}
	cout << endl;

	return 0;
}

Stable_sort (sort (same elements do not change position)

#include <iostream>     
#include <algorithm>    
#include <vector>          

using namespace std;

int main()
{
	vector<int> v = { 5,1,5,7,5,3,8,2 };
	stable_sort(v.begin(), v.end());
	for (auto val : v)
	{
		cout << val << " ";	//1 2 3 5 5 5 7 8
	}
	cout << endl;

	vector<int> v2 = { 5,1,5,7,5,3,8,2 };
	stable_sort(v2.begin(), v2.end(), [](int val, int val2) {return val > val2; });
	for (auto val : v2)
	{
		cout << val << " ";	//8 7 5 5 5 3 2 1
	}
	cout << endl;

	return 0;
}

Swap (exchange two containers)

#include <iostream>     
#include <algorithm>    
#include <vector>          

using namespace std;

int main()
{
	vector<int> v = { 11,22,33,44,55 };
	vector<int> v2 = { 1,2,3,4,5 };
	
	swap(v, v2);
	for (auto val : v)
	{
		cout << val << " ";	//1 2 3 4 5
	}
	cout << endl;

	for (auto val : v2)
	{
		cout << val << " ";	//11 22 33 44 55
	}
	cout << endl;

	return 0;
}

Swap_ranges (can be swapped by interval)

#include <iostream>     
#include <algorithm>    
#include <vector>          

using namespace std;

int main()
{
	vector<int> v = { 11,22,33,44,55 };
	vector<int> v2 = { 1,2,3,4,5 };

	swap_ranges(v.begin() + 2, v.begin() + 4, v2.begin());
	for (auto val : v)
	{
		cout << val << " ";	//11 22 1 2 55
	}
	cout << endl;

	for (auto val : v2)
	{
		cout << val << " ";	//33 44 3 4 5
	}
	cout << endl;

	return 0;
}

Transform (converting elements to rule elements)

#include <iostream>     
#include <algorithm>    
#include <vector>          

using namespace std;

int main()
{
	vector<int> v = { 11,22,33,44,55 };
	vector<int> v2(v.size());

	transform(v.begin(), v.end(), v2.begin(), [](int val) {return val - 10; });

	for (auto val : v2)
	{
		cout << val << " ";	//1 12 23 34 45
	}
	cout << endl;

	return 0;
}

Unique (adjacent elements weighted by rules)

#include <iostream>     
#include <algorithm>    
#include <vector>          

using namespace std;

int main()
{
	vector<int> v = { 11,22,22,22,33,33,44,44,22,22,55 };

	auto it = unique(v.begin(), v.end());

	v.resize(it - v.begin());
	for (auto val : v)
	{
		cout << val << " ";	//11 22 33 44 22 55
	}
	cout << endl;

	vector<int> v2 = { 11,22,22,22,33,33,44,44,22,22,55 };

	auto it2 = unique(v2.begin(), v2.end(), [](int val, int val2) {return val == val2; });

	v2.resize(it2 - v2.begin());
	for (auto val : v2)
	{
		cout << val << " ";	//11 22 33 44 22 55
	}
	cout << endl;

	return 0;
}

Unique_copy (can be copied to a new container)

#include <iostream>     
#include <algorithm>    
#include <vector>          

using namespace std;

int main()
{
	vector<int> v = { 11,22,22,22,33,33,44,44,22,22,55 };
	vector<int> v2(v.size());

	auto it = unique_copy(v.begin(), v.end(), v2.begin());

	v2.resize(it - v2.begin());
	for (auto val : v2)
	{
		cout << val << " ";	//11 22 33 44 22 55
	}
	cout << endl;

	vector<int> vv = { 11,22,22,22,33,33,44,44,22,22,55 };
	vector<int> vv2(v.size());

	auto it2 = unique_copy(vv.begin(), vv.end(), vv2.begin(), [](int val, int val2) {return val == val2; });

	vv2.resize(it2 - vv2.begin());
	for (auto val : vv2)
	{
		cout << val << " ";	//11 22 33 44 22 55
	}
	cout << endl;

	return 0;
}

Upper_bound (element iterator returning >)

#include <iostream>     
#include <algorithm>    
#include <vector>          

using namespace std;

int main()
{
	vector<int> v = { 11,22,22,22,33,33,44,44,22,22,55 };
	
	auto it = upper_bound(v.begin(), v.end(), 22);
	cout << *it << endl;	//33

	return 0;
}

Posted by cpace1983 on Sat, 09 Oct 2021 09:47:06 -0700