<algorithm> 是 C++ 标准模板库(STL)中最重要的头文件之一。它里面定义了一些有用的函数。下面就来一一介绍。
std::max
template< class T >
const T& max( const T& a, const T& b );
template< class T, class Compare >
const T& max( const T& a, const T& b, Compare comp );
template< class T >
T max( std::initializer_list<T> ilist );
template< class T, class Compare >
T max( std::initializer_list<T> ilist, Compare comp );该函数用于返回两个(前两个函数签名)或多个元素(后两个函数签名)中最大的那个。支持自定义比较函数(comp参数)。
在传入多个参数时需要用{}包裹表示为一个initializer_list对象,如std::max({1,2,3})
std::min
template< class T >
const T& min( const T& a, const T& b );
template< class T, class Compare >
const T& min( const T& a, const T& b, Compare comp );
template< class T >
T min( std::initializer_list<T> ilist );
template< class T, class Compare >
T min( std::initializer_list<T> ilist, Compare comp );该函数用于返回两个或多个元素中最小的那个,与std::max类似
std::max_element
template< class ForwardIt >
ForwardIt max_element( ForwardIt first, ForwardIt last );
template< class ForwardIt, class Compare >
ForwardIt max_element( ForwardIt first, ForwardIt last, Compare comp );这个函数用于查找指定范围([fist,last))内最大的元素,返回这个元素的迭代器。如果有多个元素等价于最大元素,则返回第一个此类元素的迭代器。这个函数支持自定义比较运算符。
std::min_element
template< class ForwardIt >
ForwardIt min_element( ForwardIt first, ForwardIt last );
template< class ForwardIt, class Compare >
ForwardIt min_element( ForwardIt first, ForwardIt last,Compare comp );这个函数与std::max_element类似,返回指定范围([fist,last))内最小的元素的迭代器。
std::minmax_element
template< class ForwardIt >
std::pair<ForwardIt, ForwardIt> minmax_element( ForwardIt first, ForwardIt last );
template< class ForwardIt, class Compare >
std::pair<ForwardIt, ForwardIt> minmax_element( ForwardIt first, ForwardIt last, Compare comp );从函数名也可看出,这个函数同时返回[first,last)范围内最大和最小元素的迭代器,返回一个std::pair类型(make_pair(min,max))。该算法不同于std::make_pair(std::min_element(), std::max_element()),不仅效率上更优,还因为该算法找到最后一个最大元素,而std::max_element找到第一个最大元素。
std::sort
std::sort用于对给定范围内的元素进行排序,以下是它的原型
template< class RandomIt >
void sort( RandomIt first, RandomIt last );
template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );minmax_elementfirst, last这两个参数表示排序范围的迭代器(必须是随机访问迭代器 也就是说list等容器无法使用std::sort)
comp参数传入一个二元比较函数(或函数对象、Lambda 表达式)(默认为std::less) 在传入std::less(<)时为从小到大 std::greater(>)时为从大到小 不传入参数时要求成员重载了<运算符
它的平均时间复杂度为 ,在旧标准中最坏复杂度可能退化到 在c++11以后版本保证最坏时间复杂度也为
std::sort的底层算法通常是内省排序,这是一种结合了快速排序、插入排序、堆排序的混合算法。结合了 快速排序 的高效、堆排序 的最坏情况保证 以及 插入排序 对小数据量的优势。
需要注意的是std::sort函数是不稳定的,也就是说它对于相同元素不保证位置靠前的元素仍然靠前
#include<iostream>
#include<algorithm> //包含std::sort
#include<vector>
using namespace std;
struct student{
string name;
int score;
};
bool cmp(const student &a,const student &b){
return a.score<b.score;
}
int main(){
int a[5]={5,3,4,1,2};
sort(a,a+5); //对从0开始的5个元素进行排序
int b[6]={0,5,3,4,1,2};
sort(b+1,b+6); //对从1开始的5个元素进行排序
string c="hello";
sort(c.begin(),c.end()); //对string进行排序
vector<int> v={5,3,4,1,2};
sort(v.begin(),v.end()); //对vector进行排序
string d[5]={"apple","banana","pear","orange","grape"};
sort(d,d+5); //对从0开始的5个元素进行排序 (std::string重载了<运算符,正确)
student s[3]={{"Tom",80},{"Jerry",90},{"Alice",85}};
sort(s,s+3,[](const student &a,const student &b){return a.score<b.score;});
//或 sort(s,s+3,cmp);
//对于自定义结构体,需要自定义排序规则(这里使用了lambda表达式,同样可以传入参数名)
int e[10]={1,4,3,5,2,6,7,8,9,10};
sort(e+1,e+5); //对从1开始的4个元素进行排序(对部分元素排序)
return 0;
}std::is_sorted
template< class ForwardIt >
bool is_sorted( ForwardIt first, ForwardIt last );
template< class ForwardIt, class Compare >
bool is_sorted( ForwardIt first, ForwardIt last, Compare comp );该函数用于判断指定范围[first,last)是否已经排好序。支持自定义比较谓词。
std::is_sorted_until
template< class ForwardIt >
ForwardIt is_sorted_until( ForwardIt first, ForwardIt last );该函数用于找到指定范围[first,last)中第一个破坏排序规则的元素,返回指向该元素的迭代器;若整个区间已排序,则返回区间的末尾迭代器(last);
std::stable_sort
std::stable_sort同样用于排序,不同的是std::stable_sort保证相等元素的相对顺序不变,它是稳定排序,通常使用归并排序实现,同样是稳定的 但归并排序的常数因子通常相比内省排序更大。此外归并排序通常需要的空间复杂度,在内存不够时通常会切换为原地归并排序。
std::partial_sort
template<typename RandomIt>
inline void partial_sort(RandomIt first,RandomIt middle,RandomIt last)
template<typename RandomIt,typename Compare>
inline void partial_sort(RandomIt first,RandomIt middle,RandomIt last,Compare comp)std::partial_sort函数用于对序列中指定范围内的元素进行排序,使得这个范围内的元素是整个序列中最小(或最大,取决于比较函数)的几个元素 也就是说从first到middle之间的元素是排好序的,
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
vector<int> v={5,2,8,1,9,3,7,4,6};
std::partial_sort(v.begin(),v.begin()+5,v.end());
for(int i=0;i<v.size();i++){
cout<<v[i]<<" ";
}
//实测输出:1 2 3 4 5 9 8 7 6
return 0;
}它通常使用堆排序变体实现 时间复杂度 在k远小于n时,这个算法比完全排序的要更快。
std::nth_element
template< class RandomIt >
void nth_element( RandomIt first, RandomIt nth, RandomIt last );对指定区间[first,last) 进行调整,使得第n个位置(nth)的元素等于 “完全排序后该位置的元素”;
调整后,nth 左侧的所有元素 nth 元素,右侧的所有元素 nth 元素(但左右侧内部无需有序);
std::swap
template< class T >
void swap( T& a, T& b );
template< class T2, std::size_t N >
void swap( T2 (&a)[N], T2 (&b)[N] );std::swap用于交换两个元素的值。
在c++98版本,它定义在<algorithm>头文件之中,
在c++11之后版本,它的定义移到了<utility>头文件之中,不过通常<algorithm>也会包含。以下头文件也会间接包含:<vector>、<string>、<deque>、<list>、<forward_list>、<array>、<map>、<set>、<unordered_map>、<unordered_set>、<queue>、<stack>、<numeric>、<tuple>、<memory>、<scoped_allocator>、<functional>、<iterator>、<ranges>、<atomic>、<thread>、<mutex>、<future>、<bitset>、<valarray>、<complex>、<regex>、<random>、<chrono>、<ratio>
总之就是哪里都有
std::swap还有关于数组的特化版本(函数签名2)用于交换两个同长数组
std::swap_ranges
template< class ForwardIt1, class ForwardIt2 >
ForwardIt2 swap_ranges( ForwardIt1 first1, ForwardIt1 last1,ForwardIt2 first2 );该函数用于交换两个等长区间[first,last),[first2,...)中的元素(第一个区间的第 i 个元素 与第二个区间的第 i 个元素交换)。该函数要求两个区间长度相同且无重叠,否则结果未定义。c++11前,该函数无返回值,在c++11后,该函数返回第二个区间交换后的末尾迭代器。
iter_swap
template< class ForwardIt1, class ForwardIt2 >
void iter_swap( ForwardIt1 a, ForwardIt2 b );该函数用于交换两个迭代器所指向的元素,相当于swap(*a,*b)。
std::reverse
template< class BidirIt >
void reverse( BidirIt first, BidirIt last );这个函数会将指定范围([first,last))内的元素按相反顺序重新排列。
需要双向迭代器(也就是需要支持++和--操作)。这个函数没有返回值。
std::vector<int> vec={1,2,3,4,5};
std::reverse(vec.begin(),vec.end());
// vec 现在是 {5, 4, 3, 2, 1}std::reverse_copy
template< class BidirIt, class OutputIt >
OutputIt reverse_copy( BidirIt first, BidirIt last,OutputIt d_first );这个函数同样用于将指定范围([first,last))内的元素按相反顺序重新排列,不同的是它会将反转后的结果写入d_first及之后位置,而原[first,last)位置数据不变。使用时需要保证d_first所处容器分配了足够的空间。
该函数的返回值是目标容器中最后一个被复制元素之后的位置(即last)。
std::lower_bound
原型如下:
template< class ForwardIt, class T >
ForwardIt lower_bound( ForwardIt first, ForwardIt last,
const T& value );
template< class ForwardIt, class T, class Compare >
ForwardIt lower_bound( ForwardIt first, ForwardIt last,
const T& value, Compare comp );该函数用于在[first,last)内查找不小于目标值(value)的第一个元素,如果找不到返回末尾迭代器。它对于线性容器时间复杂度是,但对于链表等不支持随机访问的数据结构为,这个函数要求输入序列必须是有序的(默认升序)
std::upper_bound
原型同std::lower_bound
该函数用于在[first,last)内查找大于目标值的第一个元素(value),如果找不到返回末尾迭代器。它对于线性容器时间复杂度是,但对于链表等不支持随机访问的数据结构为,,这个函数要求输入序列必须是有序的(默认升序)
std::binary_search
template< class ForwardIt, class T >
bool binary_search( ForwardIt first, ForwardIt last, const T& value );
template< class ForwardIt, class T, class Compare >
bool binary_search( ForwardIt first, ForwardIt last, const T& value, Compare comp );该函数用于在[first,last)内查找指定值是否存在。如果在范围内找到指定值,返回true,否则返回 false。它要求输入范围必须是已排序的,否则结果不可预测。它通常使用二分查找算法实现,时间复杂度为
std::find
原型如下:
template<typename InputIterator, typename _Tp>
find(InputIterator first, InputIterator last, const _Tp& val)find()用于在[first,last)内查找第一个匹配给定值(val)的元素,如果未找到匹配元素,返回last迭代器。时间复杂度为。使用operator==,自定义类型需要重载operator==。
std::find_if
template< class InputIt, class UnaryPred >
InputIt find_if( InputIt first, InputIt last, UnaryPred p );该函数用于在指定范围([first,last))中查找满足谓词(UnaryPred)的第一个元素。其第三个参数与std::sort类似,可以传入一个一元函数指针(如:std::isdigit)、函数对象function和lambda表达式
std::find_if_not
该函数用于在指定范围([first,last)中查找不满足谓词(UnaryPred)的第一个元素。原型同std::find_if
std::find_first_of
template< class InputIt, class ForwardIt >
InputIt find_first_of( InputIt first, InputIt last,ForwardIt s_first, ForwardIt s_last );
template< class InputIt, class ForwardIt, class BinaryPred >
InputIt find_first_of( InputIt first, InputIt last,ForwardIt s_first, ForwardIt s_last,BinaryPred p ); 这个函数用于查找指定范围s_first,s_last)中任意一个元素首次出现在[first,last)中的位置中最靠前的那个。
例如:
std::vector<int> a={10,20,30,40,50};
std::vector<int> b={25,30,35}; // 查找其中任意一个
auto result=std::find_first_of(a.begin(),a.end(),
b.begin(),b.end());
// result 指向 向量a中30 的位置std::find_end
template< class ForwardIt1, class ForwardIt2 >
ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last,ForwardIt2 s_first, ForwardIt2 s_last );
template< class ForwardIt1, class ForwardIt2, class BinaryPred >
ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last,ForwardIt2 s_first, ForwardIt2 s_last,BinaryPred p );该函数用于查找子序列[s_first,s_last)最后一次完整出现在序列[first,last]的位置。
std::fill
template< class ForwardIt, class T >
void fill( ForwardIt first, ForwardIt last, const T& value );该函数用于在指定范围[first,last)内所有元素填充为给定值value。没有返回值。
std::fill_n
template< class OutputIt, class Size, class T >
OutputIt fill_n( OutputIt first, Size count, const T& value );该函数用于从指定位置first开始填充count个元素为给定值value。返回指向末尾的迭代器。
std::search
template< class ForwardIt1, class ForwardIt2 >
ForwardIt1 search( ForwardIt1 first, ForwardIt1 last,ForwardIt2 s_first, ForwardIt2 s_last );
template< class ForwardIt1, class ForwardIt2, class BinaryPred >
ForwardIt1 search( ForwardIt1 first, ForwardIt1 last,ForwardIt2 s_first, ForwardIt2 s_last,BinaryPred p );该函数用于在指定范围[first,last)内查找第一个匹配的子序列[s_first,s_last)位置
std::search_n
template< class ForwardIt, class Size, class T >
ForwardIt search_n( ForwardIt first, ForwardIt last,Size count, const T& value );
template< class ForwardIt, class Size, class T, class BinaryPred >
ForwardIt search_n( ForwardIt first, ForwardIt last,Size count, const T& value, BinaryPred p );该函数用于查找指定范围[first,last)内查找第一次连续出现count次value的位置。
std::adjacent_find
template< class ForwardIt >
ForwardIt adjacent_find( ForwardIt first, ForwardIt last );
template< class ForwardIt, class BinaryPred >
ForwardIt adjacent_find( ForwardIt first, ForwardIt last,BinaryPred p );该函数用于查找指定范围[first,last)内第一对相邻且满足特定关系(默认是相等关系)的元素。
std::transform
template< class InputIt, class OutputIt, class UnaryOperation >
OutputIt transform( InputIt first1, InputIt last1,
OutputIt d_first, UnaryOperation unary_op );
template< class InputIt1, class InputIt2, class OutputIt, class BinaryOperation >
OutputIt transform( InputIt1 first1, InputIt1 last1,InputIt2 first2, OutputIt d_first,BinaryOperation binary_op );该函数用于对指定范围内的每个元素应用给定的函数或操作,并将结果存储到目标位置。如何是函数原型1,就是对[first,last)中的元素应用,如何是函数原型2,就是对上下每一个元素应用一个二元函数。
std::generate
template< class ForwardIt, class Generator >
void generate( ForwardIt first, ForwardIt last, Generator gen );该函数会使用生成器函数(函数无参数)填充指定范围[first,last)内的元素。
std::generate_n
template< class OutputIt, class Size, class Generator >
OutputIt generate_n( OutputIt first, Size count, Generator gen );该函数用于从指定范围first开始使用生成器函数填充前count个元素。
std::count
template< class InputIt, class T >
typename iterator_traits<InputIt>::difference_type
count( InputIt first, InputIt last, const T &value );该函数用于计算指定范围内([first,last))中等于特定值的元素的数量。
std::count_if
template< class InputIt, class UnaryPredicate >
typename iterator_traits<InputIt>::difference_type
count_if( InputIt first, InputIt last, UnaryPredicate p );该函数用于计算指定范围内([first,last))满足特定谓词条件的元素数量。
std::copy
template< class InputIt, class OutputIt >
OutputIt copy(InputIt first, InputIt last, OutputIt d_first);该函数用于将 [first, last) 范围内的所有元素按照源序列的顺序依次复制到目标位置(d_first),起始于d_first,返回指向最后一个复制元素之后位置的迭代器。该函数当d_first [first, last)时结果未定义。
std::copy_if
template< class InputIt, class OutputIt, class UnaryPredicate >
OutputIt copy_if(InputIt first, InputIt last, OutputIt d_first, UnaryPredicate pred);该函数同样用于复制元素,与std::copy类似,不同的是该函数会根据条件进行过滤,即只复制满足条件的元素。
std::copy_n
template< class InputIt, class Size, class OutputIt >
OutputIt copy_n( InputIt first, Size count, OutputIt result );该函数用于从源范围([first, ...))复制指定数量(count)的元素到目标位置([result, ...))。同样返回指向最后一个复制元素后的位置的迭代器。使用时要确保原范围和目标范围至少有count个元素。
std::copy_backward
template< class BidirIt1, class BidirIt2 >
BidirIt2 copy_backward( BidirIt1 first, BidirIt1 last, BidirIt2 d_last );该函数用于将指定范围[first,last)中的元素从后往前复制到[…,d_last)。d_last参数并不会实际改变,可以正常传入对象的.end()函数。除此之外,复制的对象不会因为反向输出而改变朝向。与std::copy不同的是std::copy_backward在d_last∈[first, last)时可以正常复制元素不会导致未定义行为
std::all_of
template< class InputIt, class UnaryPred >
bool all_of( InputIt first, InputIt last, UnaryPred p );该函数用于检查指定范围([first,last))内的所有元素是否都满足满足谓词条件。这个函数只有全部元素都满足谓词条件时才会返回true。一般情况下,这个函数一旦发现不满足条件的元素就立即返回false。
std::any_of
template< class InputIt, class UnaryPredicate >
bool any_of( InputIt first, InputIt last,UnaryPred p );该函数用于检查指定范围([first,last))内的所有元素是否有任意元素谓词条件,与std::all_of不同的是,指定范围内只要有一个满足谓词条件就会返回true`。一般情况下,这个函数一旦发现满足条件的元素就立即返回true
std::none_of
该函数用于检查指定范围([first,last))内的所有元素是否没有元素满足谓词条件。一般情况下,这个函数一旦发现满足条件的元素就立即返回false。
std::clamp
template<class T>
constexpr const T& clamp(const T& v, const T& lo,const T& hi);
template<class T, class Compare>
constexpr const T& clamp(const T& v, const T& lo,const T& hi, Compare comp);这个函数用于将值限制在指定的范围内。如果值小于下界,则返回下界;如果值大于上界,则返回上界;否则返回原值。这个函数需要c++17以上。如果lo>hi,结果未定义。
std::for_each
template< class InputIt, class UnaryFunc >
UnaryFunc for_each( InputIt first, InputIt last, UnaryFunc f );该函数会对指定范围([first,last))内的每个元素应用函数对象。如果f返回结果,结果被忽略。所以通常需要传入外界参数引用、指针,或者捕获外界变量。返回传入的函数对象
例如:
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void f(int& x){x*=2;}
int main(){
vector<int> v={1,2,3,4,5};
for_each(v.begin();v.end(),f);
/*
上面代码等价于
for(auto it=v.begin();v!=v.end();v++)f(*it);
输出结果:2 4 6 8 10
*/
return 0;
}std::for_each_n
template< class InputIt, class Size, class UnaryFunc >
InputIt for_each_n( InputIt, Size n, UnaryFunc f );与for_each函数类似,该函数会对指定范围([first,first+n))内的每个元素应用函数对象。返回最后一个被处理元素的下一个位置(first+n)。
std::mismach
template< class InputIt1, class InputIt2 >
std::pair<InputIt1, InputIt2> mismatch( InputIt1 first1, InputIt1 last1,InputIt2 first2 );
template< class InputIt1, class InputIt2, class BinaryPred >
std::pair<InputIt1, InputIt2> mismatch( InputIt1 first1, InputIt1 last1,InputIt2 first2, BinaryPred p );
template< class InputIt1, class InputIt2 >
std::pair<InputIt1, InputIt2> mismatch( InputIt1 first1, InputIt1 last1,InputIt2 first2, InputIt2 last2 );
template< class InputIt1, class InputIt2, class BinaryPred >
std::pair<InputIt1, InputIt2> mismatch( InputIt1 first1, InputIt1 last1,InputIt2 first2, InputIt2 last2, BinaryPred p );std::mismatch用于查找两个序列中第一个不匹配的位置。它会同时遍历两个序列,找到第一对不相等的元素,并返回指向这两个元素的迭代器对。完全匹配时返回的std::pair类型的first成员指向第一个序列的末尾迭代器(即last1),second成员指向第二个序列中对应的位置。
std::remove
template< class ForwardIt, class T >
ForwardIt remove( ForwardIt first, ForwardIt last, const T& value );该函数用于删除指定范围[first,last)中等于指定值(value)的所有元素。它不直接删除元素(不会改变容器大小),而是将不需要删除的元素移动到容器末尾。返回一个迭代器指向有效元素的末尾。。它的删除操作是稳定的:不被移除元素的相对顺序保持不变。时间复杂度为。
erase-remove 惯用法
当我们需要真正删除一个容器的元素时,可以组合使用std::remove和容器的erase函数。(仅适用于 std::vector、std::string、std::deque这类容器)如:
vector<int> v={1,2,3,2,4};
v.erase(remove(v.begin(),v.end(),2),v.end());
//删除所有为2的元素 此时容器长度为3,分别为1,3,4
string s="experience";
s.erase(remove(s.begin(),s.end(),'e'),s.end());
//删除所有的字母'e',结果"xprinc"std::remove_if
template< class ForwardIt, class UnaryPred >
ForwardIt remove_if( ForwardIt first, ForwardIt last, UnaryPred p );该函数与std::remove类似,不过它删除所有满足谓词p的元素,将它们移到容器末尾。
std::remove_copy
template< class InputIt, class OutputIt, class T >
OutputIt remove_copy( InputIt first, InputIt last,
OutputIt d_first, const T& value );该函数同样用于删除指定范围[first,last)中所有等于value的元素,不同的是它将需要保留的元素拷贝到新的目标空间[d_first,...),并不对原容器进行更改。
std::remove_copy_if
template< class InputIt, class OutputIt, class UnaryPred >
OutputIt remove_copy_if( InputIt first, InputIt last,OutputIt d_first, UnaryPred p );该函数与std::remove_copy类似,用于删除指定范围[first,last)中所有满足谓词p的元素,将需要保留的元素拷贝到新的目标空间[d_first,...)。
std::replace
template< class ForwardIt, class T >
void replace( ForwardIt first, ForwardIt last,
const T& old_value, const T& new_value );该函数用于将指定范围[first,last)中所有等于old_value的元素替换为new_value。
std::replace_if
template< class ForwardIt, class UnaryPred, class T >
void replace_if( ForwardIt first, ForwardIt last,
UnaryPred p, const T& new_value );该函数用于将指定范围[first,last)中所有满足谓词p元素替换为new_value。这个函数无返回值。
std::replace_copy
template< class InputIt, class OutputIt, class T >
OutputIt replace_copy( InputIt first, InputIt last, OutputIt d_first,const T& old_value, const T& new_value );该函数用于将指定范围[first,last)中所有等于old_value的元素替换为new_value,并将修改后的数据写入[d_first,...),返回目标区间[d_first,last)中最后一个有效元素的下一个位置迭代器。
std::replace_copy_if
template< class InputIt, class OutputIt, class UnaryPred, class T >
OutputIt replace_copy_if( InputIt first, InputIt last, OutputIt d_first,UnaryPred p, const T& new_value );该函数与std::replace_copy类似,用于将指定范围[first,last)中所有满足谓词p的元素替换为new_value,并将修改后的数据写入[d_first,...)。
std::rotate
template <class ForwardIt>
ForwardIt rotate(ForwardIt first, ForwardIt middle, ForwardIt last);该函数用于旋转容器,对指定区间 [first,last) 进行 “循环移位”,以middle为分界点,将区间分为 [first,middle)和[middle,last) 两部分,旋转后变为 [middle,last)+[first,middle)。如{1,2,3,4,5},旋转后变为{4,5,1,2,3}。
std::rotate_copy
template< class ForwardIt, class OutputIt >
OutputIt rotate_copy( ForwardIt first, ForwardIt middle,ForwardIt last, OutputIt d_first );该函数同样用于旋转容器,操作与std::rotate类似,不同的是它并不修改原范围[first,last),而是将旋转后的数据写入[d_first,...)。
std::unique
template< class ForwardIt >
ForwardIt unique( ForwardIt first, ForwardIt last );
template< class ForwardIt, class BinaryPred >
ForwardIt unique( ForwardIt first, ForwardIt last, BinaryPred p );该函数用于移除指定范围[first,last)中的连续重复元素,它不是去重所有重复元素,而是只移除相邻的重复项。它支持自定义比较谓词p。时间复杂度。与std::remove相同,它同样不会真正删除元素,而是将元素移动到容器末尾,返回目标区间内有效元素的迭代器的下一个。
std::unipue_copy
template< class InputIt, class OutputIt >
OutputIt unique_copy( InputIt first, InputIt last, OutputIt d_first );
template< class InputIt, class OutputIt, class BinaryPred >
OutputIt unique_copy( InputIt first, InputIt last,
OutputIt d_first, BinaryPred p );该函数同样用于移除指定范围[first,last)中的连续重复元素。与std::unipue不同的是,它不修改原容器,而是将操作后的数据写入[d_first,...)。
std::next_permutation
template< class BidirIt >
bool next_permutation( BidirIt first, BidirIt last );
template< class BidirIt, class Compare >
bool next_permutation( BidirIt first, BidirIt last, Compare comp );该函数基于字典序生成当前序列的”下一个”排列,如果当前序列已是字典序的最后一个,返回false,否则返回true。支持自定义谓词。
std::prev_permutation
template< class BidirIt >
bool prev_permutation( BidirIt first, BidirIt last );
template< class BidirIt, class Compare >
bool prev_permutation( BidirIt first, BidirIt last, Compare comp );该函数基于字典序生成当前序列的”上一个”排列,如果当前序列已是字典序的最后一个,返回false,否则返回true。同样支持自定义谓词。