C++ Standard Template Library(STL)算法是一组泛型算法,它们可以在各种容器上操作。这些算法被设计为与容器无关,因此可以在任何提供必要迭代器接口的容器上使用。STL算法分为以下几个主要类别:
非修改算法Non-modifying sequence operations:不改变容器内容,主要用于搜索和排序。
修改算法Modifying sequence operations:改变容器内容,如替换元素或改变元素顺序。
杂项算法:包括设置操作、堆操作等。
由于算法数量较多,我将按照分类逐一列出所有算法的详细介绍和函数原型,以及相应的简单示例。
Non-modifying sequence operations
-
std::all_of
- 函数原型:
template <class InputIterator, class UnaryPredicate> bool all_of(InputIterator first, InputIterator last, UnaryPredicate pred);
- 示例:检查范围内所有元素是否满足条件
std::vector<int> v = {1, 2, 3, 4, 5}; bool all_positive = std::all_of(v.begin(), v.end(), [](int i) { return i > 0; });
- 函数原型:
-
std::any_of
- 函数原型:
template <class InputIterator, class UnaryPredicate> bool any_of(InputIterator first, InputIterator last, UnaryPredicate pred);
- 示例:检查范围内是否有元素满足条件
bool has_positive = std::any_of(v.begin(), v.end(), [](int i) { return i > 0; });
- 函数原型:
-
std::none_of
- 函数原型:
template <class InputIterator, class UnaryPredicate> bool none_of(InputIterator first, InputIterator last, UnaryPredicate pred);
- 示例:检查范围内是否没有元素满足条件
bool no_negative = std::none_of(v.begin(), v.end(), [](int i) { return i < 0; });
- 函数原型:
-
std::for_each
- 函数原型:
template <class InputIterator, class Function> Function for_each(InputIterator first, InputIterator last, Function f);
- 示例:对每个元素应用函数对象
- 函数原型:
std::vector<int> v = {1, 2, 3, 4, 5};
std::for_each(v.begin(), v.end(), [](int i) { std::cout << i << ' '; });
std::find
- 函数原型:
template <class InputIterator, class T> InputIterator find(InputIterator first, InputIterator last, const T& value);
- 示例:查找特定值的第一个匹配项
- 函数原型:
std::vector<int> v = {1, 2, 3, 4, 5};
auto it = std::find(v.begin(), v.end(), 3);
-
std::find_if
- 函数原型:
template <class InputIterator, class UnaryPredicate> InputIterator find_if(InputIterator first, InputIterator last, UnaryPredicate pred);
- 示例:查找第一个满足条件的元素
auto it = std::find_if(v.begin(), v.end(), [](int i) { return i > 3; });
- 函数原型:
-
std::find_if_not
- 函数原型:
template <class InputIterator, class UnaryPredicate> InputIterator find_if_not(InputIterator first, InputIterator last, UnaryPredicate pred);
- 示例:查找第一个不满足条件的元素
auto it = std::find_if_not(v.begin(), v.end(), [](int i) { return i > 3; });
- 函数原型:
-
std::find_end
- 函数原型:
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator1 find_end(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
- 示例:查找最后一个子序列的匹配项
std::vector<int> v1 = {1, 2, 3, 4, 5, 1, 2}; std::vector<int> v2 = {4, 5, 1}; auto it = std::find_end(v1.begin(), v1.end(), v2.begin(), v2.end());
- 函数原型:
-
std::find_first_of
- 函数原型:
template <class InputIterator1, class InputIterator2, class BinaryPredicate> InputIterator1 find_first_of(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred);
- 示例:查找第一个来自另一个范围的元素
std::vector<int> v1 = {1, 2, 3, 4, 5}; std::vector<int> v2 = {3, 4, 5, 6}; auto it = std::find_first_of(v1.begin(), v1.end(), v2.begin(), v2.end());
- 函数原型:
-
std::adjacent_find
- 函数原型:
template <class ForwardIterator, class BinaryPredicate> ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
- 示例:查找第一个相邻的匹配项
std::vector<int> v = {1, 2, 3, 2, 4}; auto it = std::adjacent_find(v.begin(), v.end());
- 函数原型:
-
std::count
- 函数原型:
template <class InputIterator, class T> typename iterator_traits<InputIterator>::difference_type count(InputIterator first, InputIterator last, const T& value);
- 示例:计算特定值的出现次数
- 函数原型:
std::vector<int> v = {1, 2, 2, 3, 2, 4};
int count = std::count(v.begin(), v.end(), 2);
-
std::count_if
- 函数原型:
template <class InputIterator, class UnaryPredicate> typename iterator_traits<InputIterator>::difference_type count_if(InputIterator first, InputIterator last, UnaryPredicate pred);
- 示例:计算满足条件的元素数量
int count = std::count_if(v.begin(), v.end(), [](int i) { return i > 3; });
- 函数原型:
-
std::mismatch
- 函数原型:
template <class InputIterator1, class InputIterator2, class BinaryPredicate> std::pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred);
- 示例:查找第一个不匹配的元素对
std::vector<int> v1 = {1, 2, 3, 4, 5}; std::vector<int> v2 = {1, 2, 4, 4, 5}; auto pair = std::mismatch(v1.begin(), v1.end(), v2.begin());
- 函数原型:
-
std::equal
- 函数原型:
template <class InputIterator1, class InputIterator2> bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
- 示例:比较两个范围是否相等
std::vector<int> v1 = {1, 2, 3, 4, 5}; std::vector<int> v2 = {1, 2, 3, 4, 5}; bool is_equal = std::equal(v1.begin(), v1.end(), v2.begin());
- 函数原型:
-
std::is_permutation
- 函数原型:
template <class ForwardIterator1, class ForwardIterator2> bool is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
- 示例:检查一个范围是否是另一个范围的排列
std::vector<int> v1 = {1, 2, 3, 4, 5}; std::vector<int> v2 = {5, 4, 3, 2, 1}; bool is_perm = std::is_permutation(v1.begin(), v1.end(), v2.begin());
- 函数原型:
-
std::search
- 函数原型:
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
- 示例:搜索子序列
- 函数原型:
std::vector<int> v = {1, 2, 3, 4, 5};
std::vector<int> pat = {3, 4};
auto it = std::search(v.begin(), v.end(), pat.begin(), pat.end());
std::search_n
- 函数原型:
template <class ForwardIterator, class Size, class T, class BinaryPredicate> ForwardIterator search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value, BinaryPredicate pred);
- 示例:搜索特定数量的连续元素
auto it = std::search_n(v.begin(), v.end(), 3, 2);
- 函数原型:
Modifying sequence operations
-
std::copy
- 函数原型:
template <class InputIterator, class OutputIterator> OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result);
- 示例:复制范围的元素到新位置
std::vector<int> src = {1, 2, 3, 4, 5}; std::vector<int> dest(src.size()); std::copy(src.begin(), src.end(), dest.begin());
- 函数原型:
-
std::copy_n
- 函数原型:
template <class InputIterator, class Size, class OutputIterator> OutputIterator copy_n(InputIterator first, Size count, OutputIterator result);
- 示例:复制指定数量的元素到新位置
std::vector<int> src = {1, 2, 3, 4, 5}; std::vector<int> dest(src.size()); std::copy_n(src.begin(), src.size(), dest.begin());
- 函数原型:
-
std::copy_if
- 函数原型:
template <class InputIterator, class OutputIterator, class UnaryPredicate> OutputIterator copy_if(InputIterator first, InputIterator last, OutputIterator result, UnaryPredicate pred);
- 示例:复制满足条件的元素到新位置
std::vector<int> v = {1, 2, 3, 4, 5}; std::vector<int> dest(v.size()); std::copy_if(v.begin(), v.end(), dest.begin(), [](int i) { return i > 2; });
- 函数原型:
-
std::copy_backward
- 函数原型:
template <class BidirectionalIterator1, class BidirectionalIterator2> BidirectionalIterator2 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result);
- 示例:反向复制范围的元素到新位置
std::vector<int> src = {1, 2, 3, 4, 5}; std::vector<int> dest(src.size()); std::copy_backward(src.begin(), src.end(), dest.end());
- 函数原型:
-
std::move
- 函数原型:
template <class InputIterator, class OutputIterator> OutputIterator move(InputIterator first, InputIterator last, OutputIterator result);
- 示例:移动范围的元素到新位置
std::vector<int> src = {1, 2, 3, 4, 5}; std::vector<int> dest(src.size()); std::move(src.begin(), src.end(), dest.begin());
- 函数原型:
-
std::move_backward
- 函数原型:
template <class BidirectionalIterator1, class BidirectionalIterator2> BidirectionalIterator2 move_backward(BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result);
- 示例:反向移动范围的元素到新位置
std::vector<int> src = {1, 2, 3, 4, 5}; std::vector<int> dest(src.size()); std::move_backward(src.begin(), src.end(), dest.end());
- 函数原型:
-
std::swap
- 函数原型:
template <class T> void swap(T& a, T& b);
- 示例:交换两个对象的值
int a = 1, b = 2; std::swap(a, b);
- 函数原型:
-
std::swap_ranges
- 函数原型:
template <class ForwardIterator1, class ForwardIterator2> ForwardIterator2 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
- 示例:交换两个范围的元素
std::vector<int> v1 = {1, 2, 3}; std::vector<int> v2 = {4, 5, 6}; std::swap_ranges(v1.begin(), v1.end(), v2.begin());
- 函数原型:
-
std::iter_swap
- 函数原型:
template <class Iterator> void iter_swap(Iterator a, Iterator b);
- 示例:交换两个迭代器指向的元素
std::vector<int> v = {1, 2, 3, 4, 5}; std::iter_swap(v.begin(), v.begin() + 2);
- 函数原型:
-
std::transform
- 函数原型:
template <class InputIterator, class OutputIterator, class UnaryOperation> OutputIterator transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
- 示例:对每个元素应用函数并存储结果
- 函数原型:
// 创建一个包含整数的向量
std::vector<int> numbers = {1, 4, 9, 16, 25};
// 创建一个空向量,用于存储转换后的结果
std::vector<double> roots;
// 使用 std::transform 计算每个元素的平方根
std::transform(numbers.begin(), numbers.end(), std::back_inserter(roots),
[](int x) -> double { return std::sqrt(x); });
-
std::replace
- 函数原型:
template <class ForwardIterator, class T> void replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
- 示例:替换范围内的值
std::vector<int> v = {1, 2, 3, 2, 4}; std::replace(v.begin(), v.end(), 2, 5);
- 函数原型:
-
std::replace_if
- 函数原型:
template <class ForwardIterator, class UnaryPredicate, class T> void replace_if(ForwardIterator first, ForwardIterator last, UnaryPredicate pred, const T& new_value);
- 示例:替换满足条件的元素
std::vector<int> v = {1, 2, 3, 4, 5}; std::replace_if(v.begin(), v.end(), [](int i) { return i > 3; }, 0);
- 函数原型:
-
std::replace_copy
- 函数原型:
template <class InputIterator, class OutputIterator, class T> OutputIterator replace_copy(InputIterator first, InputIterator last, OutputIterator result, const T& old_value, const T& new_value);
- 示例:复制范围并替换值
std::vector<int> v = {1, 2, 3, 2, 4}; std::vector<int> dest(v.size()); std::replace_copy(v.begin(), v.end(), dest.begin(), 2, 5);
- 函数原型:
-
std::replace_copy_if
- 函数原型:
template <class InputIterator, class OutputIterator, class UnaryPredicate, class T> OutputIterator replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, UnaryPredicate pred, const T& new_value);
- 示例:复制范围并替换满足条件的元素
std::vector<int> v = {1, 2, 3, 4, 5}; std::vector<int> dest(v.size()); std::replace_copy_if(v.begin(), v.end(), dest.begin(), [](int i) { return i > 3; }, 0);
- 函数原型:
-
std::fill
- 函数原型:
template <class ForwardIterator, class T> void fill(ForwardIterator first, ForwardIterator last, const T& value);
- 示例:用给定值填充范围
std::vector<int> v(5); std::fill(v.begin(), v.end(), 1);
- 函数原型:
-
std::fill_n
- 函数原型:
template <class OutputIterator, class Size, class T> OutputIterator fill_n(OutputIterator first, Size n, const T& value);
- 示例:用给定值填充指定数量的元素
std::vector<int> v(5); std::fill_n(v.begin(), v.size(), 1);
- 函数原型:
-
std::generate
- 函数原型:
template <class ForwardIterator, class Generator> void generate(ForwardIterator first, ForwardIterator last, Generator gen);
- 示例:用生成器函数填充范围
std::vector<int> v(5); std::generate(v.begin(), v.end(), []() { static int i = 0; return ++i; });
- 函数原型:
-
std::generate_n
- 函数原型:
template <class OutputIterator, class Size, class Generator> OutputIterator generate_n(OutputIterator first, Size n, Generator gen);
- 示例:用生成器函数填充指定数量的元素
std::vector<int> v(5); std::generate_n(v.begin(), v.size(), []() { static int i = 0; return ++i; });
- 函数原型:
-
std::remove
- 函数原型:
template <class ForwardIterator, class T> ForwardIterator remove(ForwardIterator first, ForwardIterator last, const T& value);
- 示例:移除特定值的元素
std::vector<int> v = {1, 2, 3, 2, 4}; auto new_end = std::remove(v.begin(), v.end(), 2); v.erase(new_end, v.end());
- 函数原型:
-
std::remove_if
- 函数原型:
template <class ForwardIterator, class UnaryPredicate> ForwardIterator remove_if(ForwardIterator first, ForwardIterator last, UnaryPredicate pred);
- 示例:移除满足条件的元素
std::vector<int> v = {1, 2, 3, 4, 5}; auto new_end = std::remove_if(v.begin(), v.end(), [](int i) { return i > 3; }); v.erase(new_end, v.end());
- 函数原型:
-
std::remove_copy
- 函数原型:
template <class InputIterator, class OutputIterator, class T> OutputIterator remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
- 示例:复制范围并移除特定值
std::vector<int> v = {1, 2, 3, 2, 4}; std::vector<int> dest(v.size()); std::remove_copy(v.begin(), v.end(), dest.begin(), 2);
- 函数原型:
-
std::remove_copy_if
- 函数原型:
template <class InputIterator, class OutputIterator, class UnaryPredicate> OutputIterator remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, UnaryPredicate pred);
- 示例:复制范围并移除满足条件的元素
std::vector<int> v = {1, 2, 3, 4, 5}; std::vector<int> dest(v.size()); std::remove_copy_if(v.begin(), v.end(), dest.begin(), [](int i) { return i > 3; });
- 函数原型:
-
std::unique
- 函数原型:
template <class ForwardIterator> ForwardIterator unique(ForwardIterator first, ForwardIterator last);
- 示例:移除连续重复的元素
std::vector<int> v = {1, 2, 2, 3, 4, 4, 5}; auto new_end = std::unique(v.begin(), v.end()); v.erase(new_end, v.end());
- 函数原型:
-
std::unique_copy
- 函数原型:
template <class InputIterator, class OutputIterator> OutputIterator unique_copy(InputIterator first, InputIterator last, OutputIterator result);
- 示例:复制范围并移除连续重复的元素
std::vector<int> v = {1, 2, 2, 3, 4, 4, 5}; std::vector<int> dest(v.size()); std::unique_copy(v.begin(), v.end(), dest.begin());
- 函数原型:
-
std::reverse
- 函数原型:
template <class BidirectionalIterator> void reverse(BidirectionalIterator first, BidirectionalIterator last);
- 示例:反转范围内元素的顺序
std::vector<int> v = {1, 2, 3, 4, 5}; std::reverse(v.begin(), v.end());
- 函数原型:
-
std::reverse_copy
- 函数原型:
template <class BidirectionalIterator, class OutputIterator> OutputIterator reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
- 示例:复制范围并反转顺序
std::vector<int> v = {1, 2, 3, 4, 5}; std::vector<int> dest(v.size()); std::reverse_copy(v.begin(), v.end(), dest.begin());
- 函数原型:
-
std::rotate
- 函数原型:
template <class ForwardIterator> void rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
- 示例:向左旋转范围内的元素
std::vector<int> v = {1, 2, 3, 4, 5}; std::rotate(v.begin(), v.begin() + 2, v.end());
- 函数原型:
-
std::rotate_copy
- 函数原型:
template <class ForwardIterator, class OutputIterator> OutputIterator rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
- 示例:复制并旋转范围的元素
std::vector<int> v = {1, 2, 3, 4, 5}; std::vector<int> dest(v.size()); std::rotate_copy(v.begin(), v.begin() + 2, v.end(), dest.begin());
- 函数原型:
-
std::random_shuffle
- 函数原型:
template <class RandomAccessIterator> void random_shuffle(RandomAccessIterator first, RandomAccessIterator last);
- 示例:随机打乱范围内元素的顺序(已在上文展示)
- 函数原型:
-
std::shuffle
- 函数原型:
template <class RandomAccessIterator, class UniformRandomBitGenerator> void shuffle(RandomAccessIterator first, RandomAccessIterator last, UniformRandomBitGenerator&& g);
- 示例:随机打乱范围内元素的顺序
std::vector<int> v = {1, 2, 3, 4, 5}; std::shuffle(v.begin(), v.end(), std::default_random_engine(std::random_device{}()));
- 函数原型:
Sorting operations
-
std::is_sorted
- 函数原型:
template <class ForwardIterator> bool is_sorted(ForwardIterator first, ForwardIterator last);
- 示例:检查范围是否已排序
std::vector<int> v = {1, 2, 3, 4, 5}; bool is_sorted = std::is_sorted(v.begin(), v.end());
- 函数原型:
-
std::is_sorted_until
- 函数原型:
template <class ForwardIterator> ForwardIterator is_sorted_until(ForwardIterator first, ForwardIterator last);
- 示例:找到第一个未排序的元素
std::vector<int> v = {1, 2, 3, 4, 5}; auto it = std::is_sorted_until(v.begin(), v.end());
- 函数原型:
-
std::sort
- 函数原型:
template <class RandomAccessIterator> void sort(RandomAccessIterator first, RandomAccessIterator last);
- 示例:对范围内的元素进行排序(已在上文展示)
- 函数原型:
-
std::stable_sort
- 函数原型:
template <class RandomAccessIterator> void stable_sort(RandomAccessIterator first, RandomAccessIterator last);
- 示例:稳定排序范围内的元素
std::vector<int> v = {1, 2, 3, 4, 5}; std::stable_sort(v.begin(), v.end());
- 函数原型:
-
std::partial_sort
- 函数原型:
template <class RandomAccessIterator, class T> void partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp = Compare());
- 示例:部分排序范围内的元素
std::vector<int> v = {5, 3, 4, 2, 1}; std::partial_sort(v.begin(), v.begin() + 2, v.end());
- 函数原型:
-
std::partial_sort_copy
- 函数原型:
template <class InputIterator, class RandomAccessIterator, class Compare> RandomAccessIterator partial_sort_copy(InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp = Compare());
- 示例:复制并部分排序范围的元素
std::vector<int> v = {5, 3, 4, 2, 1}; std::vector<int> dest(2); std::partial_sort_copy(v.begin(), v.end(), dest.begin(), dest.end());
- 函数原型:
-
std::nth_element
- 函数原型:
template <class RandomAccessIterator, class T> void nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp = Compare());
- 示例:对范围内的元素进行部分排序,确保第n个元素处于正确的位置
std::vector<int> v = {5, 3, 4, 2, 1}; std::nth_element(v.begin(), v.begin() + 2, v.end());
- 函数原型:
Binary search operations
-
std::lower_bound
- 函数原型:
template <class ForwardIterator, class T> ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
- 示例:在有序范围内找到元素的下界(已在上文展示)
- 函数原型:
-
std::upper_bound
- 函数原型:
template <class ForwardIterator, class T> ForwardIterator upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
- 示例:在有序范围内找到元素的上界
std::vector<int> v = {1, 2, 3, 4, 5}; auto it = std::upper_bound(v.begin(), v.end(), 3);
- 函数原型:
-
std::equal_range
- 函数原型:
template <class ForwardIterator, class T> std::pair<ForwardIterator, ForwardIterator> equal_range(ForwardIterator first, ForwardIterator last, const T& value);
- 示例:找到范围内与给定值相等的子范围
std::vector<int> v = {1, 2, 2, 3, 4, 4, 5}; auto pair = std::equal_range(v.begin(), v.end(), 2);
- 函数原型:
-
std::binary_search
- 函数原型:
template <class ForwardIterator, class T> bool binary_search(ForwardIterator first, ForwardIterator last, const T& value);
- 示例:测试给定值是否存在于已排序的序列中
std::vector<int> v = {1, 2, 3, 4, 5}; bool found = std::binary_search(v.begin(), v.end(), 3);
- 函数原型:
Set operations
-
std::includes
- 函数原型:
template <class InputIterator1, class InputIterator2> bool includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
- 示例:测试一个有序序列是否包含另一个有序序列
std::vector<int> v1 = {1, 2, 3, 4, 5}; std::vector<int> v2 = {3, 4}; bool includes = std::includes(v1.begin(), v1.end(), v2.begin(), v2.end());
- 函数原型:
-
std::set_union
- 函数原型:
template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_union(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result);
- 示例:计算两个有序序列的并集
std::vector<int> v1 = {1, 2, 3, 4, 5}; std::vector<int> v2 = {3, 4, 5, 6, 7}; std::vector<int> dest(v1.size() + v2.size()); std::set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), dest.begin());
- 函数原型:
-
std::set_intersection
- 函数原型:
template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_intersection(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result);
- 示例:计算两个有序序列的交集
std::vector<int> v1 = {1, 2, 3, 4, 5}; std::vector<int> v2 = {3, 4, 5, 6, 7}; std::vector<int> dest(v1.size() + v2.size()); std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), dest.begin());
- 函数原型:
-
std::set_difference
- 函数原型:
template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_difference(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result);
- 示例:计算两个有序序列的差集
std::vector<int> v1 = {1, 2, 3, 4, 5}; std::vector<int> v2 = {3, 4, 5, 6, 7}; std::vector<int> dest(v1.size() + v2.size()); std::set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), dest.begin());
- 函数原型:
-
std::set_symmetric_difference
- 函数原型:
template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_symmetric_difference(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result);
- 示例:计算两个有序序列的对称差集
std::vector<int> v1 = {1, 2, 3, 4, 5}; std::vector<int> v2 = {3, 4, 5, 6, 7}; std::vector<int> dest(v1.size() + v2.size()); std::set_symmetric_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), dest.begin());
- 函数原型:
Heap operations
-
std::push_heap
- 函数原型:
template <class RandomAccessIterator> void push_heap(RandomAccessIterator first, RandomAccessIterator last);
- 示例:将元素推入最大堆
std::vector<int> v = {1, 2, 3, 4, 5}; std::push_heap(v.begin(), v.end());
- 函数原型:
-
std::pop_heap
- 函数原型:
template <class RandomAccessIterator> void pop_heap(RandomAccessIterator first, RandomAccessIterator last);
- 示例:从最大堆中弹出最大元素
std::vector<int> v = {1, 2, 3, 4, 5}; std::pop_heap(v.begin(), v.end()); v.pop_back();
- 函数原型:
-
std::make_heap
- 函数原型:
template <class RandomAccessIterator> void make_heap(RandomAccessIterator first, RandomAccessIterator last);
- 示例:从范围创建最大堆
std::vector<int> v = {1, 2, 3, 4, 5}; std::make_heap(v.begin(), v.end());
- 函数原型:
-
std::sort_heap
- 函数原型:
template <class RandomAccessIterator> void sort_heap(RandomAccessIterator first, RandomAccessIterator last);
- 示例:将最大堆排序为升序范围
std::vector<int> v = {1, 2, 3, 4, 5}; std::sort_heap(v.begin(), v.end());
- 函数原型:
-
std::is_heap
- 函数原型:
template <class RandomAccessIterator> bool is_heap(RandomAccessIterator first, RandomAccessIterator last);
- 示例:检查范围是否为最大堆
std::vector<int> v = {1, 2, 3, 4, 5}; bool is_heap = std::is_heap(v.begin(), v.end());
- 函数原型:
-
std::is_heap_until
- 函数原型:
template <class RandomAccessIterator> RandomAccessIterator is_heap_until(RandomAccessIterator first, RandomAccessIterator last);
- 示例:找到最大堆中的首个元素,它不是堆序的
std::vector<int> v = {1, 2, 3, 4, 5}; auto it = std::is_heap_until(v.begin(), v.end());
- 函数原型:
Minimum/maximum operations
-
std::min
- 函数原型:
template <class T1, class T2> constexpr decltype(auto) min(T1&& a, T2&& b);
- 示例:返回两个值中较小的一个
int a = 5, b = 10; int min_val = std::min(a, b);
- 函数原型:
-
std::max
- 函数原型:
template <class T1, class T2> constexpr decltype(auto) max(T1&& a, T2&& b);
- 示例:返回两个值中较大的一个
int a = 5, b = 10; int max_val = std::max(a, b);
- 函数原型:
-
std::min_element
- 函数原型:
template <class ForwardIterator> ForwardIterator min_element(ForwardIterator first, ForwardIterator last);
- 示例:返回范围内最小元素的迭代器
std::vector<int> v = {5, 3, 4, 2, 1}; auto it = std::min_element(v.begin(), v.end());
- 函数原型:
-
std::max_element
- 函数原型:
template <class ForwardIterator> ForwardIterator max_element(ForwardIterator first, ForwardIterator last);
- 示例:返回范围内最大元素的迭代器
std::vector<int> v = {5, 3, 4, 2, 1}; auto it = std::max_element(v.begin(), v.end());
- 函数原型:
-
std::minmax
- 函数原型:
template <class T1, class T2> constexpr std::pair<decay_t<T1>, decay_t<T2>> minmax(T1&& a, T2&& b);
- 示例:返回两个值中较小和较大的一对
int a = 5, b = 10; auto result = std::minmax(a, b);
- 函数原型:
-
std::minmax_element
- 函数原型:
template <class ForwardIterator> std::pair<ForwardIterator, ForwardIterator> minmax_element(ForwardIterator first, ForwardIterator last);
- 示例:返回范围内最小和最大元素的迭代器对
std::vector<int> v = {5, 3, 4, 2, 1}; auto result = std::minmax_element(v.begin(), v.end());
- 函数原型:
-
std::clamp
- 函数原型:
template <class T> constexpr const T& clamp(const T& v, const T& lo, const T& hi);
- 示例:将值限制在指定范围内
int value = 5, lo = 2, hi = 8; int clamped = std::clamp(value, lo, hi);
- 函数原型:
numeric
-
std::accumulate
- 函数原型:
template <class InputIterator, class T> T accumulate(InputIterator first, InputIterator last, T init);
- 示例:计算范围内元素的总和
std::vector<int> v = {1, 2, 3, 4, 5}; int sum = std::accumulate(v.begin(), v.end(), 0);
- 函数原型:
-
std::inner_product
- 函数原型:
template <class InputIterator1, class InputIterator2, class T> T inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init);
- 示例:计算两个范围的内积
std::vector<int> v1 = {1, 2, 3}; std::vector<int> v2 = {4, 5, 6}; int product = std::inner_product(v1.begin(), v1.end(), v2.begin(), 0);
- 函数原型:
-
std::adjacent_difference
- 函数原型:
template <class InputIterator, class OutputIterator> OutputIterator adjacent_difference(InputIterator first, InputIterator last, OutputIterator result);
- 示例:计算范围内相邻元素的差
std::vector<int> v = {1, 2, 3, 4, 5}; std::vector<int> diff(v.size()); std::adjacent_difference(v.begin(), v.end(), diff.begin());
- 函数原型:
-
std::partial_sum
- 函数原型:
template <class InputIterator, class OutputIterator> OutputIterator partial_sum(InputIterator first, InputIterator last, OutputIterator result);
- 示例:计算范围内元素的前缀和
std::vector<int> v = {1, 2, 3, 4, 5}; std::vector<int> sums(v.size()); std::partial_sum(v.begin(), v.end(), sums.begin());
- 函数原型:
-
std::iota
- 函数原型:
template <class ForwardIterator, class T> void iota(ForwardIterator first, ForwardIterator last, T value);
- 示例:填充范围内的序列值(已在上文展示)
- 函数原型:
-
std::gcd
- 函数原型:
template <class M, class N> constexpr M gcd(M m, N n);
- 示例:计算两个整数的最大公约数(已在上文展示)
- 函数原型:
-
std::lcm
- 函数原型:
template <class M, class N> constexpr common_type_t<M, N> lcm(M m, N n);
- 示例:计算两个整数的最小公倍数
int a = 48, b = 18; int result = std::lcm(a, b);
- 函数原型:
functional
-
std::function
- 函数原型:
template <class> class function;
- 示例:创建一个可调用对象的封装(已在上文展示)
- 函数原型:
-
std::bind
- 函数原型:
template <class> class bind;
- 示例:创建一个可调用对象,其行为类似于函数或成员函数的绑定版本(已在上文展示)
- 函数原型:
-
std::hash
- 函数原型:
template <class T> class hash;
- 示例:计算给定值的哈希值
- 函数原型:
// 创建一个字符串
std::string str = "Hello, World!";
// 使用 std::hash 生成哈希值
std::hash<std::string> hasher;
size_t hash_value = hasher(str);
-
std::equal_to
- 函数原型:
struct equal_to { constexpr bool operator()(const T& a, const T& b) const noexcept; };
- 示例:比较两个值是否相等
std::vector<int> v1 = {1, 2, 3}; std::vector<int> v2 = {1, 2, 3}; bool is_equal = std::equal_to<>()(v1.begin(), v1.end(), v2.begin());
- 函数原型:
-
std::not_equal_to
- 函数原型:
struct not_equal_to { constexpr bool operator()(const T& a, const T& b) const noexcept; };
- 示例:比较两个值是否不相等
std::vector<int> v1 = {1, 2, 3}; std::vector<int> v2 = {1, 2, 4}; bool is_not_equal = std::not_equal_to<>()(v1.begin(), v1.end(), v2.begin());
- 函数原型:
-
std::greater
- 函数原型:
struct greater { constexpr bool operator()(const T& a, const T& b) const noexcept; };
- 示例:比较两个值是否满足
a > b
std::vector<int> v = {1, 2, 3, 4, 5}; std::sort(v.begin(), v.end(), std::greater<>());
- 函数原型:
-
std::less
- 函数原型:
struct less { constexpr bool operator()(const T& a, const T& b) const noexcept; };
- 示例:比较两个值是否满足
a < b
std::vector<int> v = {5, 3, 4, 2, 1}; std::sort(v.begin(), v.end(), std::less<>());
- 函数原型:
-
std::plus
- 函数原型:
struct plus { constexpr T operator()(const T& a, const T& b) const noexcept; };
- 示例:计算两个值的和
int result = std::plus<int>()(2, 3);
- 函数原型:
-
std::minus
- 函数原型:
struct minus { constexpr T operator()(const T& a, const T& b) const noexcept; };
- 示例:计算两个值的差
int result = std::minus<int>()(5, 2);
- 函数原型:
-
std::multiplies
- 函数原型:
struct multiplies { constexpr T operator()(const T& a, const T& b) const noexcept; };
- 示例:计算两个值的积
int result = std::multiplies<int>()(3, 4);
- 函数原型:
-
std::divides
- 函数原型:
struct divides { constexpr T operator()(const T& a, const T& b) const noexcept; };
- 示例:计算两个值的商
int result = std::divides<int>()(6, 2);
- 函数原型:
-
std::modulus
- 函数原型:
struct modulus { constexpr T operator()(const T& a, const T& b) const noexcept; };
- 示例:计算两个值的模
int result = std::modulus<int>()(9, 2);
- 函数原型:
-
std::negate
- 函数原型:
struct negate { constexpr T operator()(const T& a) const noexcept; };
- 示例:计算值的相反数
int result = std::negate<int>()(3);
- 函数原型:
这些是C++ STL中、和头文件中定义的算法。每个算法都有其特定的用途,覆盖了从搜索、排序到数值计算等多种操作。
标签:std,begin,end,示例,STL,c++,vector,template,模板 From: https://blog.csdn.net/u010196944/article/details/143163092