-
Notifications
You must be signed in to change notification settings - Fork 0
Algorithm
DryPerspective edited this page Sep 20, 2023
·
3 revisions
Standard library algorithms added to the <algorithm> header since C++11. These do not include algorithms which require move semantics to function, and replace move semantics with copies where appropriate. Details of any function can be found on the cppreference <algorithm> page
The functions included are:
| all_of any_of none_of |
Checks if a predicate is true for all/any/none of the values in a range |
| for_each_n | Applies a function object to the first n elements of a range |
| find_if_not | Finds the first element of a range for which a predicate returns false |
| copy_if | Copies elements of a range which meet a certain predicate to another location |
| copy_n | Copies a number of elements to a new location |
| shift_left shift_right |
Shifts elements of a range left or right by n steps Note: this uses copy semantics, unlike the standard algorithm which moves |
| is_partitioned | Determines if a range is partitioned by a particular predicate |
| partition_copy | Copies a range, dividing the contents into two groups |
| partition_point | Finds the partition point of a given range |
| is_sorted | Checks whether a range is sorted into ascending order |
| is_sorted_until | Finds the largest sorted subrange |
| minmax | Finds the smaller and larger of two integers |
| minmax_element | Returns the smallest and largest values in a range |
| clamp | Clamps a value between two elements |
#include <iterator>
#include <vector>
#include "cpp98/algorithm.h"
struct is_even{
template<typename T>
bool operator()(T in){
return in % 2 == 0;
}
};
int main(){
std::vector<int> vec = fill_vector_with_data();
if(!dp::is_sorted(vec.begin(), vec.end())) std::sort(vec.begin(), vec.end());
std::vector<int> evens;
std::vector<int> odds;
//Copy evens and odds into separate vectors
dp::partition_copy(vec.begin(), vec.end(), std::back_inserter(evens.begin()), std::back_inserter(odds.begin()), is_even());
}