STL具有高性能,高移植性和代码重用性高的优点.
Sorting
sort(startaddress, endaddress)
1 |
|
Refer std::sort() for more details.
Searching
binary_search(startaddress, endaddress, valuetofind)
1 | // CPP program to implement |
std::bsearch in C++
Important STL Algorithms
sort(first_iterator, last_iterator) – To sort the given vector.
reverse(first_iterator, last_iterator) – To reverse a vector.
*max_element (first_iterator, last_iterator) – To find the maximum element of a vector.
*min_element (first_iterator, last_iterator) – To find the minimum element of a vector.
accumulate(first_iterator, last_iterator, initial value of sum) – Does the summation of vector elements
1 | // A C++ program to demonstrate working of sort(), |
count(first_iterator, last_iterator,x) – To count the occurrences of x in vector.
find(first_iterator, last_iterator, x) – Points to last address of vector ((name_of_vector).end()) if element is not present in vector.
1 | // C++ program to demonstrate working of count() |
binary_search(first_iterator, last_iterator, x) – Tests whether x exists in sorted vector or not.
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’.
1 | // C++ program to demonstrate working of lower_bound() |
arr.erase(position to be deleted) – This erases selected element in vector and shifts and resizes the vector elements accordingly.
arr.erase(unique(arr.begin(),arr.end()),arr.end()) – This erases the duplicate occurrences in sorted vector in a single line.
1 | // C++ program to demonstrate working of erase() |
next_permutation(first_iterator, last_iterator) – This modified the vector to its next permutation.
prev_permutation(first_iterator, last_iterator) – This modified the vector to its previous permutation.
1 | // C++ program to demonstrate working of next_permutation() |
- distance(first_iterator,desired_position) – It returns the distance of desired position from the first iterator.This function is very useful while finding the index.
1 | // C++ program to demonstrate working of distance() |
Useful Array algorithms
- all_of()
1 | // C++ code to demonstrate working of all_of() |
- any_of()
1 | // C++ code to demonstrate working of any_of() |
- none_of()
1 | // C++ code to demonstrate working of none_of() |
- copy_n()
1 | // C++ code to demonstrate working of copy_n() |
- iota()
1 | // C++ code to demonstrate working of iota() |
待续