前言
#include 是标准库头文件。此头文件是算法库的一部分。头文件函数包含常用的不修改序列的操作 、修改序列的操作 、划分操作 、排序操作 、二分搜索操作 、其他已排序范围上的操作 、集合操作、堆操作 、最小/最大操作 、比较操作 、排列操作 等;
这些算法通过迭代器直接对值进行操作,不会以任何方式影响任何可能容器的结构。
不修改序列的操作
1、all_of、any_of、none_of:检查谓词是否对范围中所有、任一或无元素为 true(函数模板)
2、for_each:应用函数到范围中的元素(函数模板)
3、countcount_if:返回满足指定判别标准的元素数(函数模板)
4、mismatch:寻找两个范围出现不同的首个位置(函数模板)
5、find、find_if、find_if_not:寻找首个满足特定判别标准的元素(函数模板)
6、find_end:在特定范围中寻找最后出现的元素序列(函数模板)
7、find_first_of:搜索元素集合中的任意元素(函数模板)
8、adjacent_find:查找首对相邻的相同元素(函数模板)
9、search:搜索一个元素范围(函数模板)
10、search_n:在范围中搜索一定量的某个元素的连续副本(函数模板)
all_of
template <class InputIterator, class UnaryPredicate>
bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred);
若一元谓词对范围中所有元素返回 true 则为 true ,否则为 false 。若范围为空则返回 true 。
all_of 版本
template<class InputIterator,class UnaryPredicate>
bool all_of(InputIterator first, InputIterator last,UnaryPredicate pred)
{
while(first != last)
{
if(!pred(*first))return false;
++first;
}
return true;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int uneven_number(int i)
{
return i%2;
}
int main (int argc, char **argv)
{
vector<int> v = {1,3,5,7,9,11,13,15,17,19};
if (all_of(v.begin(),v.end(),uneven_number))
{
cout <<"All are odd numbers"<<endl;
}
else
{
cout <<"Elements have even numbers"<<endl;
}
return 0;
}
输出结果:
any_of
template <class InputIterator, class UnaryPredicate>
bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred);
若一元谓词对范围中至少一个元素返回 true 则为 true ,否则为 false 。若范围为空则返回 false 。
any_of 版本
template<class InputIterator,class UnaryPredicate>
bool all_of(InputIterator first, InputIterator last,UnaryPredicate pred)
{
while(first != last)
{
if(!pred(*first))return true;
++first;
}
return false;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int uneven_number(int i)
{
return i < 0;
}
int main (int argc, char **argv)
{
vector<int> v = {1,2,3,-1,-3,-7,9};
if (any_of(v.begin(),v.end(),uneven_number))
{
cout <<"Have a negative"<<endl;
}
else
{
cout <<"not negative"<<endl;
}
return 0;
}
输出结果:
none_of
template <class InputIterator, class UnaryPredicate>
bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred);
若一元谓词不对范围中任何元素返回 true 则为 true ,否则为 false 。若范围为空则返回 true 。
template<class InputIterator,class UnaryPredicate>
bool all_of(InputIterator first, InputIterator last,UnaryPredicate pred)
{
while(first != last)
{
if(!pred(*first))return false;
++first;
}
return true;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int uneven_number(int i)
{
return i < 0;
}
int main (int argc, char **argv)
{
vector<int> v = {1,2,3,9};
if (none_of(v.begin(),v.end(),uneven_number))
{
cout <<"not negative"<<endl;
}
else
{
cout <<"Have a negative"<<endl;
}
return 0;
}
输出结果:
for_each
template <class InputIterator, class Function>
Function for_each (InputIterator first, InputIterator last, Function fn);
按顺序应用给定的函数对象 fn 到解引用范围 [first, last) 中每个迭代器的结果。
for_each 底层版本
template<class InputIterator,class Function>
Function all_of(InputIterator first, InputIterator last,Function fn)
{
while(first != last)
{
if(*first)
++first;
}
return fn;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
void Fun(int i)
{
cout <<" "<<i;
}
struct FunClass
{
FunClass():sum(0) { }
void operator() (int i)
{
sum += i;
}
int sum;
}myclass;
int main (int argc, char **argv)
{
vector<int> v = {1,2,3,9,30,50,90};
cout <<"traversal: ";
for_each(v.begin(),v.end(),Fun);
cout << endl;
FunClass s =for_each(v.begin(),v.end(),myclass);
cout << endl;
cout <<"sum: "<<s.sum<<endl;
return 0;
}
输出结果:
find
template <class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val);
find 搜索等于 val的元素。返回一个迭代器到范围[first,last]中比较等于val的第一个元素。如果没有找到这样的元素,函数返回last。
find 底层版本
template<class InputIterator,class T>
Function all_of(InputIterator first, InputIterator last,T val)
{
while(first != last)
{
if(*first == val) return first;
++first;
}
return last;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main (int argc, char **argv)
{
int n1 = 9;
vector<int> v = {1,2,3,9,30,50,90};
auto value = find(v.begin(),v.end(),n1);
if (value != v.end())
{
cout <<" Found elements:"<<n1<<endl;
}
else
{
cout <<" No element found:"<<n1<<endl;
}
return 0;
}
输出结果:
find_if
template <class InputIterator, class UnaryPredicate>
InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);
返回一个迭代器到pred返回true的范围[first,last]中的第一个元素。如果没有找到这样的元素,函数返回last。
find_if 底层版本
template<class InputIterator,class UnaryPredicate>
InputIterator find_if(InputIterator first, InputIterator last,UnaryPredicate pred)
{
while(first != last)
{
if(pred(*first)) return first;
++first;
}
return last;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
bool IsOdd(int i)
{
return ((i%2) == 1);
}
int main (int argc, char **argv)
{
vector<int> v = {12,24,25,30,55,60,80,93,100};
auto it = find_if(v.begin(),v.end(),IsOdd);
cout <<"The first odd value is "<<*it<<endl;
return 0;
}
输出结果:
find_if_not
template <class InputIterator, class UnaryPredicate>
InputIterator find_if_not (InputIterator first, InputIterator last, UnaryPredicate pred);
返回一个迭代器到pred返回false的范围[first,last]中的第一个元素。如果没有找到这样的元素,函数返回last。
find_if_not 底层代码
template<class InputIterator,class UnaryPredicate>
InputIterator find_if_not(InputIterator first, InputIterator last,UnaryPredicate pred)
{
while(first != last)
{
if(!pred(*first)) return first;
++first;
}
return last;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
bool IsOdd(int i)
{
return (i%2);
}
int main (int argc, char **argv)
{
vector<int> v = {1,3,24,25,30,55,60,80,93,100};
auto it = find_if_not(v.begin(),v.end(),IsOdd);
cout <<"The first even value is "<<*it<<endl;
return 0;
}
输出结果:
find_end
(1)
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2);
(2)
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred);
在特定范围中寻找最后出现的元素序列 。
搜索范围[first1,last1),查找[first2,last2]定义的序列的最后一个出现项,并返回一个迭代器到它的第一个元素,如果没有找到出现项,则返回last1。
find_first_of 底层实现
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 find_end(ForwardIterator1 first1,ForwardIterator1 last1
ForwardIterator2 first2,ForwardIterator2 last2)
{
if (first2 == last2) //如果找不到
return last1;
ForwardIterator1 result = last1;
while (first1 != last1)//搜索范围
{
ForwardIterator1 it1 = first1;
ForwardIterator2 it2 = first2;
while (*it1 == *it2)
{
++it1;
++it2;
if (it2 == last2) //如果查找第一个等于最后一个,返回第一个
{
result = first1; //最后一个值等于第一个值
break;
}
if (it1 == last1)//如果搜索等于最后一个
return result;
}
++ first1;
}
return result;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main (int argc, char **argv)
{
vector<int> v = {1,2,3,4,1,2,3,4,1,2,3,4}; //
vector<int> f={1,2,3,4};//查找范围
auto result = find_end(v.begin(),v.end(),f.begin(),f.end());
if (result != v.end())
{
cout <<"The last place I found it was: "<<result - v.begin()<<endl;
}
else
{
cout <<"not found"<<endl;
}
return 0;
}
输出结果:
find_first_of
(1)
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2);
(2)
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred);
参数:
first1, last1 - 要检验的元素范围
first2,last2 - 要搜索的元素范围
返回范围[first1,last1)中的第一个元素的迭代器,该元素与[first2,last2)中的任何元素匹配。如果没有找到这样的元素,函数将返回last1。
find_first_of 底层代码
template <class InputIterator, class ForwardIterator>
InputIterator find_first_of (InputIterator first1,InputIterator last1
ForwardIterator first2,ForwardIterator last2)
{
while (first1 != last1)//搜索范围
{
for (ForwardIterator it = first2; it != last2; ++it)
{
if (*it == *first1)
return first1;
}
++ first1;
}
return last1;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main (int argc, char **argv)
{
vector<char> v = {'a','b','c','A','B','C'}; //
vector<char> f={'A','B','C'};//查找范围
auto result = find_first_of(v.begin(),v.end(),f.begin(),f.end());
if (result != v.end())
{
cout <<"found a match at: "<<*result<<endl;
}
else
{
cout <<"not found"<<endl;
}
return 0;
}
输出结果:
adjacent_find
(1)
template <class ForwardIterator>
ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last);
(2)
template <class ForwardIterator, class BinaryPredicate>
ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last,
BinaryPredicate pred);
在范围 [first, last) 中搜索二个相继的等同元素。
搜索匹配的两个连续元素的第一次出现的范围[first,last],并返回一个迭代器到这两个元素中的第一个元素,如果没有找到这样的对,则返回last。
adjacent_find 底层实现
template <class ForwardIterator>
InputIterator adjacent_find(ForwardIterator first,ForwardIterator last)
{
if (first == last)
return last;
InputIterator next = first;
while (next != last1)//搜索范围
{
if (*first == *last)
return first;
++ first;
++ next;
}
return last1;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main (int argc, char **argv)
{
vector<int> v = {2,3,1,3,4,4,7,8,9,9};
auto result = adjacent_find (v.begin(),v.end());
if (result != v.end())
{
//cout <<"the first pair of repeated elements are: "<<*result<<endl;
cout <<"the first pair of repeated elements are: "<<result - v.begin()<<endl;
}
else
{
cout <<"no matching adjacent elements"<<endl;
}
return 0;
}
输出结果:
count
template <class InputIterator, class T>
typename iterator_traits<InputIterator>::difference_type
count (InputIterator first, InputIterator last, const T& val);
参数:
first, last - 要检验的元素范围
val- 要搜索的值
计数等于 val的元素。
返回范围[first,last]中比较起来等于val的元素数量。
template <class InputIterator,class T>
tempname iterator_traits<InputIterator>::difference_type
count (InputIterator first, InputIterator last, const T& val)
{
typename iterator_traits<InputIterator>::difference_type ret = 0;
while (first != last)
{
if (*first == val) ++ ret;
++ first;
}
return ret;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main (int argc, char **argv)
{
int num1 = 4;
int num2 = 5;
vector<int> v = {1, 2, 3, 4, 4, 3, 7, 8, 9, 10};
int result1 = count(v.begin(),v.end(),4);
int result2 = count(v.begin(),v.end(),5);
cout <<"number: "<<num1 <<" count: "<<result1<<endl;
cout <<"number: "<<num2 <<" count: "<<result2<<endl;
return 0;
}
输出结果:
count_if
template <class InputIterator, class UnaryPredicate>
typename iterator_traits<InputIterator>::difference_type
count_if (InputIterator first, InputIterator last, UnaryPredicate pred);
返回pred为true的范围(first,last)中的元素数量。
template <class InputIterator, class UnaryPredicate>
typename iterator_traits<InputIterator>::difference_type
count_if (InputIterator first, InputIterator last, UnaryPredicate pred)
{
typename iterator_traits<InputIterator>::difference_type ret = 0;
while (first!=last) {
if (pred(*first)) ++ret;
++first;
}
return ret;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
bool fun(int i)
{
return ((i%3) == 0);
}
int main (int argc, char **argv)
{
vector<int> v = {1, 2, 3, 4, 4, 3, 7, 8, 9, 10};
int result1 = count_if(v.begin(),v.end(),fun);
cout <<" number divisible by three: "<<result1<<endl;
return 0;
}
输出结果:
search
(1)
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2);
(2)
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred);
搜索范围 [first1, last1 - (last2 - first2)) 中元素子序列 [first2, last2) 的首次出现。
搜索[first2,last2)定义的序列的第一个出现项的范围[first1,last1),并返回一个迭代器到它的第一个元素,如果没有发现出现项,则返回last1。
search 底层代码
template <class ForwardIterator1,class ForwardIterator2>
ForwardIterator1 search(ForwardIterator1 first1,ForwardIterator1 last1
ForwardIterator2 first2,ForwardIterator2 last1)
{
if (first1 == last1)
return first1;
while (first1 != last1)
{
ForwardIterator1 it1 = first1;
ForwardIterator2 it2 = first2;
while (*it1 == *it2)
{
if (*it2 == *last2)
return first1;
if (*it1 == last1)
return last1;
++it1;
++it2;
}
++first1;
}
return last1;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main (int argc, char **argv)
{
vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
vector<int> f = {4,5,6};
auto it = search(v.begin(),v.end(),f.begin(),f.end());
if (it != v.end())
{
cout <<"found at position: "<<(it - v.begin())<<endl;
}
else
{
cout <<"not found"<<endl;
}
return 0;
}
输出结果:
修改序列的操作
1、copy/copy_if:将某一范围的元素复制到一个新的位置(函数模板)
2、copy_n:将一定数目的元素复制到一个新的位置(函数模板)
3、copy_backward:按从后往前的顺序复制一个范围内的元素(函数模板)
4、move:将某一范围的元素移动到一个新的位置(函数模板)
5、move_backward:按从后往前的顺序移动某一范围的元素到新的位置(函数模板)
6、fill:将一个给定值复制赋值给一个范围内的每个元素(函数模板)
7、fill_n:将一个给定值复制赋值给一个范围内的 N 个元素(函数模板)
8、transform:将一个函数应用于某一范围的各个元素(函数模板)
9、generate:将相继的函数调用结果赋值给一个范围中的每个元素(函数模板)
10、generate_n:将相继的函数调用结果赋值给一个范围中的 N 个元素(函数模板)
11、remove/remove_if:移除满足特定判别标准的元素(函数模板)
12、remove_copy/remove_copy_if:复制一个范围的元素,忽略满足特定判别标准的元素
(函数模板)
13、replace/replace_if:将所有满足特定判别标准的值替换为另一个值(函数模板)
14、replace_copy/replace_copy_if:复制一个范围内的元素,并将满足特定判别标准的元素替换为另一个值(函数模板)
15、swap:交换两个对象的值(函数模板)
16、swap_ranges:交换两个范围的元素(函数模板)
17、iter_swap:交换两个迭代器所指向的元素(函数模板)
18、reverse:逆转范围中的元素顺序(函数模板)
19、reverse_copy:创建一个范围的逆向副本(函数模板)
20、rotate:旋转范围中的元素顺序(函数模板)
21、rotate_copy:复制并旋转元素范围(函数模板)
22、unique:移除范围内的连续重复元素(函数模板)
23、unique_copy:创建某范围的不含连续重复元素的副本(函数模板)
copy
template <class InputIterator, class OutputIterator>
OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result);
参数:
first, last - 要复制的元素范围
result- 目标范围的起始。
将范围[first,last]中的元素复制到以result开头的范围中。
指向目标范围中最后复制元素的下个元素的输出迭代器。
copy底层代码
template <class InputIterator,class OutputIterator>
OutputIterator copy(InputIterator first,InputIterator last, OutputIterator result)
{
while (first != last)
{
*result = *first;
++result;
++first;
}
return result;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main (int argc, char **argv)
{
vector<int> dst = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
vector<int> src(10);
copy(dst.begin(),dst.end(),src.begin());
cout <<" src contains: ";
for (auto it = src.begin(); it != src.end(); ++it)
{
cout <<" " << *it;
}
cout << endl;
return 0;
}
输出结果
copy_n
template <class InputIterator, class Size, class OutputIterator>
OutputIterator copy_n (InputIterator first, Size n, OutputIterator result);
参数
first - 复制来源的元素范围起始
n - 要复制的元素数
result - 目标范围起始
将开始于第一个范围的前n个元素复制到开始于result的范围中。
copy_n底层代码
template <class InputIterator,class Size,class OutputIterator>
OutputIterator copy(InputIterator first,Size n, OutputIterator result)
{
while (n > 0)
{
*result = *first;
++result;
++first;
--n;
}
return result;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main (int argc, char **argv)
{
vector<int> dst = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
vector<int> src(10);
copy_n(dst.begin(),10,src.begin());
cout <<" src contains: ";
for (auto it = src.begin(); it != src.end(); ++it)
{
cout <<" " << *it;
}
cout << endl;
return 0;
}
输出结果
copy_if
template <class InputIterator, class OutputIterator, class UnaryPredicate>
OutputIterator copy_if (InputIterator first, InputIterator last,
OutputIterator result, UnaryPredicate pred);
参数
first, last - 要复制的元素范围
result- 目标范围的起始。
pred - 对所要求的元素则返回 true 的一元谓词。
复制范围[first,last]中的元素,其中pred返回true到result开始的范围。
copy_if底层代码
template <class InputIterator, class OutputIterator, class UnaryPredicate>
OutputIterator copy_if (InputIterator first, InputIterator last,
OutputIterator result, UnaryPredicate pred)
{
while (first != last)
{
if (pred(*first))
{
*result = *first;
++result;
}
++first;
}
return result;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
bool Copy_Fun(int i)
{
return (i > 0);
}
int main (int argc, char **argv)
{
vector<int> dst = {1, -2, 3, -4, 5, 6, -7, 8, 9, -10};
vector<int> src(dst.size());
auto it = copy_if(dst.begin(),dst.end(),src.begin(),Copy_Fun);
src.resize(distance(src.begin(),it)); //distance返回从 first 到 last 的路程。
for (vector<int>::iterator x = src.begin(); x != src.end(); ++x)
{
cout <<" "<<*x;
}
cout << endl;
return 0;
}
输出结果
copy_backward
template <class BidirectionalIterator1, class BidirectionalIterator2>
BidirectionalIterator2 copy_backward (BidirectionalIterator1 first,
BidirectionalIterator1 last,
BidirectionalIterator2 result);
参数
first, last - 要复制的元素范围
result- 目标范围的结尾。
将范围[first,last]中的元素从末尾复制到结束于result的范围中。
copy_backward 底层代码
template<class BidirectionalIterator1, class BidirectionalIterator2>
BidirectionalIterator2 copy_backward ( BidirectionalIterator1 first,
BidirectionalIterator1 last,
BidirectionalIterator2 result )
{
while (last!=first)
{
*(--result) = *(--last);
}
return result;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main (int argc, char **argv)
{
vector<int> dst = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
vector<int> src(dst.size());
copy_backward(dst.begin(),dst.end(),src.end());
cout <<" src contains:";
for (vector<int>::iterator it = src.begin(); it != src.end(); ++it)
{
cout <<" "<< *it;
}
cout << endl;
return 0;
}
输出结果
move
template <class InputIterator, class OutputIterator>
OutputIterator move (InputIterator first, InputIterator last, OutputIterator result);
将范围内的元素[first,last]移动到以result开头的范围内。[first,last]中的元素的值被转移到result所指向的元素。调用之后,范围[first,last]中的元素将处于未指定但有效的状态。
move底层代码
template<class InputIterator, class OutputIterator>
OutputIterator move (InputIterator first, InputIterator last, OutputIterator result)
{
while (first!=last) {
*result = std::move(*first);
++result; ++first;
}
return result;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main (int argc, char **argv)
{
vector<string> dst = {"zhangsan","lisi","wangwu","laoliu"};
vector<string> src(dst.size());
move(dst.begin(),dst.end(),src.begin());
cout <<" src contains: " << src.size()<<endl ;
cout << " elements ";
for (auto it = src.begin(); it != src.end(); ++it)
{
cout <<" "<<*it;
}
cout << endl;
return 0;
}
输出结果
swap
template void swap (T& a, T& b);
交换a和b的值。
swap 底层实现
template <class T> void swap ( T& a, T& b )
{
T c(a); a=b; b=c;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main (int argc, char **argv)
{
int a = 6;
int b = 8;
cout << a <<" "<<b<<endl;
swap(a,b);
cout <<a<<" "<<b<<endl;
return 0;
}
输出结果
swap_ranges
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator2 swap_ranges (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2);
参数:
first1, last1 - 要交换的第一个元素范围
first2 - 要交换的第二个元素范围的起始
swap_ranges底层代码
template<class ForwardIterator1, class ForwardIterator2>
ForwardIterator2 swap_ranges (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2)
{
while (first1!=last1) {
swap (*first1, *first2);
++first1; ++first2;
}
return first2;
}
replace
template <class ForwardIterator, class T>
void replace (ForwardIterator first, ForwardIterator last,
const T& old_value, const T& new_value);
参数:
first, last - 要处理的元素范围
old_value - 要被替换的元素值
new_value - 用作替换者的值
以 new_value 替换范围 [first, last) 中所有满足特定判别标准的元素。
replace 底层代码
template <class ForwardIterator, class T>
void replace (ForwardIterator first, ForwardIterator last,
const T& old_value, const T& new_value)
{
while (first != last)
{
if (*first == old_value)
{
*first = new_value;
}
++first;
}
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main (int argc, char **argv)
{
vector<int> v = {5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
replace(v.begin(),v.end(),4,66);
cout <<"v contains: ";
for (int n : v)
{
cout <<" "<< n ;
}
cout <<endl;
return 0;
}
输出结果
replace
template <class ForwardIterator, class UnaryPredicate, class T>
void replace_if (ForwardIterator first, ForwardIterator last,
UnaryPredicate pred, const T& new_value );
参数:
first, last - 要处理的元素范围
old_value - 要被替换的元素值
p - 若应该替换元素则返回 true 的一元谓词。
new_value - 用作替换者的值
replace_if底层实现
template < class ForwardIterator, class UnaryPredicate, class T >
void replace_if (ForwardIterator first, ForwardIterator last,
UnaryPredicate pred, const T& new_value)
{
while (first != last)
{
if (pred(*first))
{
*first = new_value;
}
++first;
}
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
bool IsGreater(int i)
{
return (i > 5);
}
int main (int argc, char **argv)
{
vector<int> v = {5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
replace_if(v.begin(),v.end(),IsGreater,66);
cout <<"v contains: ";
for (int n : v)
{
cout <<" "<< n ;
}
cout <<endl;
return 0;
}
输出结果
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);
参数:
first, last - 要复制的元素范围
result- 目标范围的起始
old_value - 要被替换的元素值
new_value - 用作替换者的元素值
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)
{
while (first!=last) {
*result = (*first==old_value)? new_value: *first;
++first; ++result;
}
return result;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main (int argc, char **argv)
{
vector<int> v = {5, 3, 4, 3, 8, 6, 3, 9, 0, 3};
vector<int> f(v.size());
replace_copy(v.begin(),v.end(),f.begin(),3,88);
cout <<"v contains: ";
for (int n : f)
{
cout <<" "<< n ;
}
cout <<endl;
return 0;
}
输出结果
fill
template <class ForwardIterator, class T>
void fill (ForwardIterator first, ForwardIterator last, const T& val);
参数
first, last - 要修改的元素范围
value - 要赋的值
fill底层代码实现
template <class ForwardIterator, class T>
void fill (ForwardIterator first, ForwardIterator last, const T& val)
{
while (first != last)
{
*first = val;
++first;
}
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main (int argc, char **argv)
{
vector<int> v = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
fill(v.begin(),v.end(),88);
cout <<"v contains: ";
for (int n : v)
{
cout <<" "<< n ;
}
cout <<endl;
return 0;
}
输出结果
fill_n
template <class OutputIterator, class Size, class T>
void fill_n (OutputIterator first, Size n, const T& val);
first - 要修改的元素范围起始
n - 要修改的元素数
val - 要赋的值
将val赋值给first指向的序列的前n个元素。若 count > 0 则为指向最后赋值元素后一位置的迭代器,否则为 first
template <class ForwardIterator, class Size,class T>
void fill (ForwardIterator first, Size n, const T& val)
{
while (n > 0)
{
*first = val;
++first;
--n;
}
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main (int argc, char **argv)
{
vector<int> v = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
fill_n(v.begin(),5,66);
cout <<"v contains: ";
for (int n : v)
{
cout <<" "<< n ;
}
cout <<endl;
return 0;
}
输出结果
generate
template <class ForwardIterator, class Generator>
void generate (ForwardIterator first, ForwardIterator last, Generator gen);
参数:
first, last - 要生成的元素范围
gen - 将要调用的生成器函数。
将对gen的连续调用返回的值赋给范围[first,last]中的元素。
generate 底层实现
template <class ForwardIterator, class Generator>
void generate ( ForwardIterator first, ForwardIterator last, Generator gen )
{
while (first != last)
{
*first = gen();
++first;
}
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
#include <ctime>
using namespace std;
int Number_Fun()
{
return (rand()%100);
}
int main (int argc, char **argv)
{
srand(unsigned(time(0)));
vector<int> v(8);
generate(v.begin(),v.end(),Number_Fun);
cout <<"v contains: ";
for (int n : v)
{
cout <<" "<< n ;
}
cout <<endl;
return 0;
}
输出结果
remove
template <class ForwardIterator, class T>
ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val);
参数:
first, last - 要处理的元素范围
value - 要移除的元素值
将范围[first,last]转换为一个范围,删除所有与val相比较的元素,并返回一个迭代器到该范围的新末尾。
template <class ForwardIterator, class T>
ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val)
{
ForwardIterator result = first;
while (first != last)
{
if (!(*first == val))
{
*result = * first;
++ result;
}
++first;
}
return result;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
#include <ctime>
using namespace std;
int main (int argc, char **argv)
{
string str1 = "Text with some spaces";
str1.erase(remove(str1.begin(), str1.end(), ' '),
str1.end());
cout << str1 << endl;
return 0;
}
remove_copy
template <class InputIterator, class OutputIterator, class T>
OutputIterator remove_copy (InputIterator first, InputIterator last,
OutputIterator result, const T& val);
参数:
first, last - 要复制的元素范围
result - 目标范围的起始。
val - 不复制的元素的值
将范围内的元素[first,last]复制到以result开头的范围,但比较起来等于val的那些元素除外。
remove_copy底层代码
template <class InputIterator, class OutputIterator, class T>
OutputIterator remove_copy (InputIterator first, InputIterator last,
OutputIterator result, const T& val)
{
while (first != last)
{
if (!(*first == val))
{
*result = * first;
++ result;
}
++first;
}
return result;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
#include <ctime>
using namespace std;
int main (int argc, char **argv)
{
vector<int> v = {10,20,30,30,20,10,10,20};
vector<int> myvector(v.size());
remove_copy(v.begin(),v.end(),myvector.begin(),20);
for (auto it = myvector.begin(); it != myvector.end(); ++it)
{
cout <<" " << *it;
}
cout << endl;
return 0;
}
输出结果:
unique
(1)
template <class ForwardIterator>
ForwardIterator unique (ForwardIterator first, ForwardIterator last);
(2)
template <class ForwardIterator, class BinaryPredicate>
ForwardIterator unique (ForwardIterator first, ForwardIterator last,
BinaryPredicate pred);
从范围[first,last]中每个连续的等价元素组中删除除第一个元素外的所有元素。
unique底层实现
template <class ForwardIterator>
ForwardIterator unique (ForwardIterator first, ForwardIterator last)
{
if (first==last) return last;
ForwardIterator result = first;
while (++first != last)
{
if (!(*result == *first)) // or: if (!pred(*result,*first)) for version (2)
*(++result)=*first;
}
return ++result;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
#include <ctime>
using namespace std;
int main (int argc, char **argv)
{
vector<int> v = {1,2,3,1,2,3,3,4,5,4,5,6,7};
sort(v.begin(),v.end());
auto last = unique(v.begin(),v.end());
v.erase(last,v.end());
for (int n : v)
{
cout <<" "<<n;
}
cout << endl;
return 0;
}
输出结果
unique_copy
(1)
template <class InputIterator, class OutputIterator>
OutputIterator unique_copy (InputIterator first, InputIterator last,
OutputIterator result);
(2)
template <class InputIterator, class OutputIterator, class BinaryPredicate>
OutputIterator unique_copy (InputIterator first, InputIterator last,
OutputIterator result, BinaryPredicate pred);
参数:
first, last - 要处理的元素范围
result- 目标范围的起始
将范围内的元素[first,last]复制到从result开始的范围,但连续复制除外
unique_copy底层实现代码
template <class ForwardIterator>
ForwardIterator unique (ForwardIterator first, ForwardIterator last)
{
if (first == last)
return last;
ForwardIterator result = first;
while (first != last)
{
if ()
++first;
}
return
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
#include <ctime>
using namespace std;
int main (int argc, char **argv)
{
vector<int> vdst = {1,2,3,1,2,3,3,4,5,4,5,6,7};
vector<int> vsrc(vdst.size());
sort(vdst.begin(),vdst.end());
auto last = unique_copy(vdst.begin(),vdst.end(),vsrc.begin());
vsrc.erase(last,vsrc.end());
for (int n : vsrc)
{
cout <<" "<<n;
}
cout << endl;
return 0;
}
输出结果:
reverse
template <class BidirectionalIterator>
void reverse (BidirectionalIterator first, BidirectionalIterator last);
参数:
first, last - 要反转的元素的范围
反转范围[first,last]中元素的顺序。
reverse底层实现
template <class BidirectionalIterator>
void reverse (BidirectionalIterator first, BidirectionalIterator last)
{
while ((first!=last)&&(first!=--last)) {
std::iter_swap (first,last);
++first;
}
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
#include <ctime>
using namespace std;
int main (int argc, char **argv)
{
vector<int> v = {1,2,3,4,5,6,7,8,9};
reverse(v.begin(),v.end());
for (int n : v)
{
cout <<" "<<n;
}
cout << endl;
return 0;
}
输出结果
reverse_copy
template <class BidirectionalIterator, class OutputIterator>
OutputIterator reverse_copy (BidirectionalIterator first,
BidirectionalIterator last, OutputIterator result);
参数:
first, last - 要复制的元素范围
result- 新范围的起始
将范围[first,last]中的元素复制到以result开头的范围,但顺序相反。
reverse_copy底层实现
template <class BidirectionalIterator, class OutputIterator>
OutputIterator reverse_copy (BidirectionalIterator first,
BidirectionalIterator last, OutputIterator result)
{
while (first != last)
{
--last;
*result = *last;
++first;
}
return result;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
#include <ctime>
using namespace std;
int main (int argc, char **argv)
{
vector<int> vdst = {1,2,3,4,5,6,7,8,9};
vector<int> vsrc(vdst.size());
reverse_copy(vdst.begin(),vdst.end(),vsrc.begin());
cout <<" vsrc contains:";
for (int n : vsrc)
{
cout <<" "<<n;
}
cout << endl;
return 0;
}
输出结果
排序操作
1、sort:将范围按升序排序(函数模板)
2、 is_sorted:检查范围是否已按升序排列(函数模板)
3、is_sorted_until:找出最大的已排序子范围(函数模板)
4、partial_sort:排序一个范围的前 N 个元素(函数模板)
5、partial_sort_copy:对范围内的元素进行复制并部分排序(函数模板)
6、stable_sort:将范围内的元素排序,同时保持相等的元素之间的顺序(函数模板)
7、nth_element:将给定的范围部分排序,确保其按给定元素划分(函数模板)
sort
(1)
template <class RandomAccessIterator>
void sort (RandomAccessIterator first, RandomAccessIterator last);
(2)
template <class RandomAccessIterator, class Compare>
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
参数:
first, last - 要排序的元素范围 。
以升序排序范围 [first, last) 中的元素。不保证维持相等元素的顺序。
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
bool Fun_Less(int i, int j)
{
return (i > j);
}
struct Less_Class
{
bool operator() (int i, int j)
{
return (i < j);
}
}Less_Obj;
int main (int argc, char **argv)
{
vector<int> v = {5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
sort (v.begin(),v.end());
cout <<" sort: ";
for (vector<int>::iterator it = v.begin(); it != v.end(); ++it)
{
cout <<" "<<*it;
}
cout << endl;
//sort (v.begin(),v.end(),greater<int>());
sort (v.begin(),v.end(),Fun_Less);
cout <<"Fun_Less: ";
for (vector<int>::iterator it = v.begin(); it != v.end(); ++it)
{
cout <<" "<<*it;
}
cout << endl;
return 0;
}
输出结果:
is_sorted
(1)
template <class ForwardIterator>
bool is_sorted (ForwardIterator first, ForwardIterator last);
(2)
template <class ForwardIterator, class Compare>
bool is_sorted (ForwardIterator first, ForwardIterator last, Compare comp);
参数:
first, last - 要检验的元素范围
is_sorted底层实现
template <class ForwardIterator>
bool is_sorted (ForwardIterator first, ForwardIterator last)
{
if(first == last)
return true;
ForwardIterator next = first;
while (++next != last)
{
if (*next < *first)
return false;
++ first;
}
return true;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main (int argc, char **argv)
{
vector<int > v = {2,7,2,5,1,8,4,6,9};
for(int n : v)
{
cout <<" "<<n;
}
cout <<" is_sorted: "<<is_sorted(v.begin(),v.end());
cout <<endl;
sort(v.begin(),v.end());
for (vector<int>::iterator it = v.begin(); it != v.end(); ++it)
{
cout <<" "<<*it;
}
cout <<" is_sorted: "<<is_sorted(v.begin(),v.end())<<endl;
return 0;
}
输出结果
二分搜索操作
1、lower_bound:返回指向第一个不小于给定值的元素的迭代器(函数模板)
2、upper_bound:返回指向第一个大于给定值的元素的迭代器(函数模板)
3、binary_search:确定元素是否存在于某范围中(函数模板)
4、equal_range:返回匹配特定键值的元素范围
lower_bound
(1)
template <class ForwardIterator, class T>
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
const T& val);
(2)
template <class ForwardIterator, class T, class Compare>
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
const T& val, Compare comp);
参数:
first, last - 定义要检验的已部分排序范围的迭代器
comp- 要与元素比较的值
返回指向范围 [first, last) 中首个不小于(即大于或等于) value 的元素的迭代器,或若找不到这种元素则返回 last 。
lower_bound底层代码
template <class ForwardIterator, class T>
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val)
{
ForwardIterator it;
iterator_traits<ForwardIterator>::difference_type count, step;
count = distance(first,last);
while (count>0)
{
it = first; step=count/2; advance (it,step);
if (*it<val) { // or: if (comp(*it,val)), for version (2)
first=++it;
count-=step+1;
}
else count=step;
}
return first;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main(int argc, char const *argv[])
{
std::vector<int> v = {1,4,3,5,6,4,4,6,7,6,2,8,9};
sort(v.begin(),v.end());
for (int n : v)
{
cout << " "<<n;
}
cout << endl;
vector<int>::iterator low,up;
low = lower_bound(v.begin(),v.end(),4);
up = upper_bound(v.begin(),v.end(),4);
cout <<"lower_bound at position "<<(low - v.begin())<<endl;
cout <<"upper_bound at position "<<(up - v.begin())<<endl;
return 0;
}
输出结果:
其他已排序范围上的操作
merge:归并两个已排序的范围(函数模板)
inplace_merge:就地归并两个有序范围
merge
(1)
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator merge (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result);
(2)
template <class InputIterator1, class InputIterator2,
class OutputIterator, class Compare>
OutputIterator merge (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp);
参数:
first1, last1 - 要归并的元素的第一范围
first2, last2 - 要归并到元素的第二范围
result - 目标范围的起始 .
comp - 比较函数对象(即满足比较 (Compare) 概念的对象),若第一参数小于(即先序于)第二参数则返回 true
将排序范围[first1,last1)和[first2,last2)中的元素组合到一个以result开头的新范围中,并对其所有元素进行排序。
merge底层实现
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator merge (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
while (true) {
if (first1==last1) return std::copy(first2,last2,result);
if (first2==last2) return std::copy(first1,last1,result);
*result++ = (*first2<*first1)? *first2++ : *first1++;
}
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main(int argc, char const *argv[])
{
std::vector<int> v1 = {1,24,52,7,35,92,6,15,81};
std::vector<int> v2 = {5,12,17,44,3,65,2,64,9,12};
std::vector<int> v3(v1.size()+v2.size());
merge(v1.begin(),v1.end(),v2.begin(),v2.end(),v3.begin());
cout <<" v3 contains: " ;
for (vector<int>::iterator it = v3.begin(); it != v3.end(); ++it)
{
cout <<" "<<*it;
}
cout << endl;
return 0;
}
输出结果
(已排序范围上的)集合操作
1、includes:若一个集合是另一个的子集则返回 true(函数模板)
2、set_difference:计算两个集合的差集(函数模板)
3、set_intersection:计算两个集合的交集(函数模板)
4、set_symmetric_difference:计算两个集合的对称差(函数模板)
5、set_union:计算两个集合的并集(函数模板)
includes
template <class InputIterator1, class InputIterator2>
bool includes ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2 );
template <class InputIterator1, class InputIterator2, class Compare>
bool includes ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2, Compare comp );
参数:
first1, last1 - 要检验的已排序元素范围
first2, last2 - 要搜索的已排序元素范围
comp - 比较函数对象(即满足比较 (Compare) 概念的对象),若第一参数小于(即先序于)第二参数则返回 true 。
如果排序范围[first1,last1)包含排序范围[first2,last2)中的所有元素,则返回true。
includes底层实现
template <class InputIterator1, class InputIterator2>
bool includes (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2)
{
while (first2!=last2) {
if ( (first1==last1) || (*first2<*first1) ) return false;
if (!(*first1<*first2)) ++first2;
++first1;
}
return true;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main(int argc, char const *argv[])
{
std::vector<char> v1 = {'a', 'b', 'c', 'f', 'h', 'x'};
std::vector<char> v2 = {'a', 'b', 'c'};
std::vector<char> v3 = {'a', 'g', 'e'};
for (vector<char>::iterator it = v1.begin(); it != v1.end(); ++it)
{
cout <<" "<<*it;
}
cout << endl;
cout <<"v2 includes ";
for (vector<char>::iterator it = v2.begin(); it != v2.end(); ++it)
{
cout <<" "<<*it;
}
cout <<" "<<includes(v1.begin(), v1.end(), v2.begin(), v2.end())<<endl;
cout <<"v3 includes ";
for (vector<char>::iterator it = v3.begin(); it != v3.end(); ++it)
{
cout <<" "<<*it;
}
cout <<" "<<includes(v1.begin(), v1.end(), v3.begin(), v3.end())<<endl;
return 0;
}
输出结果
set_difference
(1)
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result);
(2)
template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp);
参数:
first1, last1 - 要检验的元素范围
first2, last2 - 要搜索的元素范围
comp - 比较函数对象(即满足比较 (Compare) 概念的对象),若第一参数小于(即先序于)第二参数则返回 true 。
用已排序范围[first1,last1]相对于已排序范围[first2,last2)的集差在result所指向的位置构造一个排序范围。
set_difference底层实现
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
while (first1!=last1 && first2!=last2)
{
if (*first1<*first2) { *result = *first1; ++result; ++first1; }
else if (*first2<*first1) ++first2;
else { ++first1; ++first2; }
}
return std::copy(first1,last1,result);
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main(int argc, char const *argv[])
{
vector<int>::iterator it;
std::vector<int> v1 = {2,4,6,7,8,8,8,9};
std::vector<int> v2 = {2,6,8,9};
std::vector<int> v3(v1.size()+v2.size());
it = set_difference(v1.begin(),v1.end(),v2.begin(),v2.end(),v3.begin());
v3.resize(it-v3.begin());
for ( it= v3.begin(); it != v3.end(); ++it)
{
cout <<" " << *it;
}
cout << endl;
return 0;
}
输出结果
set_intersection
(1)
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result);
(2)
template <class InputIterator1, class InputIterator2,
class OutputIterator, class Compare>
OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp);
参数:
first1, last1 - 要检验的第一元素范围
first2, last2 - 要检验的第二元素范围
comp - 比较函数对象(即满足比较 (Compare) 概念的对象),若第一参数小于(即先序于)第二参数则返回 true 。
用两个排序范围[first1,last1)和[first2,last2)的交集集在result指向的位置开始构造一个排序范围。两个集合的交集只由两个集合中的元素组成。函数复制的元素总是来自第一个范围,顺序相同。
set_intersection底层实现
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
while (first1!=last1 && first2!=last2)
{
if (*first1<*first2) ++first1;
else if (*first2<*first1) ++first2;
else {
*result = *first1;
++result; ++first1; ++first2;
}
}
return result;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main(int argc, char const *argv[])
{
vector<int>::iterator it;
std::vector<int> v1 = {2,4,5,7,8,8,8,10};
std::vector<int> v2 = {2,6,8,9};
std::vector<int> v3(v1.size()+v2.size());
it = set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),v3.begin());
v3.resize(it-v3.begin());
cout << "The intersection has " << (v3.size()) << " elements:\n";
for ( it= v3.begin(); it != v3.end(); ++it)
{
cout <<" " << *it;
}
cout << endl;
return 0;
}
输出结果
set_union
(1)
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_union (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result);
(2)
template <class InputIterator1, class InputIterator2,
class OutputIterator, class Compare>
OutputIterator set_union (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp);
参数:
first1, last1 - 第一个输入的已排序范围
first2, last2 - 第二个输入的已排序范围
comp - 比较函数对象(即满足比较 (Compare) 概念的对象),若第一参数小于(即先序于)第二参数则返回 true。
使用两个排序范围[first1,last1)和[first2,last2)的集合并置,从result指向的位置开始构造一个排序范围。两个集合的并集是由存在于其中一个集合或同时存在于这两个集合中的元素组成的。在第一个范围中具有等效元素的第二个范围中的元素不会复制到结果范围中。
set_union底层代码
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_union (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
while (true)
{
if (first1==last1) return std::copy(first2,last2,result);
if (first2==last2) return std::copy(first1,last1,result);
if (*first1<*first2) { *result = *first1; ++first1; }
else if (*first2<*first1) { *result = *first2; ++first2; }
else { *result = *first1; ++first1; ++first2; }
++result;
}
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main(int argc, char const *argv[])
{
vector<int>::iterator it;
std::vector<int> v1 = {2,4,5,7,8,8,8,10};
std::vector<int> v2 = {2,6,8,9};
std::vector<int> v3(v1.size()+v2.size());
it = set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),v3.begin());
v3.resize(it-v3.begin());
cout << "The union has " << (v3.size()) << " elements:\n";
for ( it= v3.begin(); it != v3.end(); ++it)
{
cout <<" " << *it;
}
cout << endl;
return 0;
}
输出结果
堆操作
1、is_heap:检查给定范围是否为一个最大堆(函数模板)
2、is_heap_until:查找能成为最大堆的最大子范围(函数模板)
3、make_heap:从一个元素范围创建出一个最大堆(函数模板)
4、push_heap:将一个元素加入到一个最大堆(函数模板)
5、pop_heap:从最大堆中移除最大元素(函数模板)
6、sort_heap:将一个最大堆变成一个按升序排序的元素范围(函数模板)
is_heap
(1)
template <class RandomAccessIterator>
bool is_heap (RandomAccessIterator first, RandomAccessIterator last);
(2)
template <class RandomAccessIterator, class Compare>
bool is_heap (RandomAccessIterator first, RandomAccessIterator last,
Compare comp);
参数:
first, last - 要检验的元素范围
comp - 比较函数对象(即满足比较 (Compare) 要求的对象),若首个参数小于第二个,则返回 true 。
检查范围 [first, last) 中的元素是否为最大堆。
is_heap底层实现
(1)
template <class RandomAccessIterator>
bool is_heap (RandomAccessIterator first, RandomAccessIterator last);
(2)
template <class RandomAccessIterator, class Compare>
bool is_heap (RandomAccessIterator first, RandomAccessIterator last,
Compare comp);
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main(int argc, char const *argv[])
{
std::vector<int> foo = {3,5,2,6,8,4,7,9,1};
if (!is_heap(foo.begin(),foo.end()));
make_heap(foo.begin(),foo.end());
while (!foo.empty())
{
pop_heap(foo.begin(),foo.end());
cout <<" "<<foo.back();
foo.pop_back();
}
cout << endl;
return 0;
}
输出结果
push_heap
(1)
template <class RandomAccessIterator>
void push_heap (RandomAccessIterator first, RandomAccessIterator last);
(2)
template <class RandomAccessIterator, class Compare>
void push_heap (RandomAccessIterator first, RandomAccessIterator last,
Compare comp);
参数
first, last - 定义要修改的堆的元素范围
给定范围[first,last-1]中的堆,该函数将堆的范围扩展到[first,last],方法是将值放在(last-1)中的相应位置。
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main(int argc, char const *argv[])
{
std::vector<int> v={1,3,5,2,8,5,7,4};
make_heap(v.begin(),v.end());
for (auto i: v)
{
cout << i <<" ";
}
cout << endl;
v.push_back(6);
cout <<"before push_heap:";
for (auto i : v)
{
cout << i <<" ";
}
cout << endl;
push_heap(v.begin(),v.end());
cout <<"after push_heap:";
for (auto i : v)
{
cout << i <<" ";
}
cout << endl;
return 0;
}
输出结果
sort_heap
(1)
template <class RandomAccessIterator>
void sort_heap (RandomAccessIterator first, RandomAccessIterator last);
(2)
template <class RandomAccessIterator, class Compare>
void sort_heap (RandomAccessIterator first, RandomAccessIterator last,
Compare comp);
参数
first, last - 要排序的元素范围
comp - 比较函数对象(即满足比较 (Compare) 要求的对象),若首个参数小于第二个,则返回 true 。
按升序排列堆范围内的元素(第一个,最后一个)。
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main(int argc, char const *argv[])
{
std::vector<int> v={1,3,5,2,8,5,7,4};
make_heap(v.begin(),v.end());
cout <<" heap:";
for (vector<int>::iterator it = v.begin(); it != v.end(); ++it)
{
cout << *it<<" ";
}
cout << endl;
sort_heap(v.begin(),v.end());
cout <<"sorted:";
for (vector<int>::iterator it = v.begin(); it != v.end(); ++it)
{
cout << *it <<" ";
}
cout << endl;
return 0;
}
输出结果:
最小/最大操作
1、max:返回各给定值中的较大者(函数模板)
2、max_element:返回范围内的最大元素(函数模板)
3、min:返回各给定值中的较小者(函数模板)
4、min_element:返回范围内的最小元素(函数模板)
5、minmax:返回两个元素的较小和较大者(函数模板)
6、minmax_element:返回范围内的最小元素和最大元素(函数模板)
max
(1)
template <class T> const T& max (const T& a, const T& b);
(2)
template <class T, class Compare>
const T& max (const T& a, const T& b, Compare comp);
(3)
template <class T> T max (initializer_list<T> il);
template <class T, class Compare>
T max (initializer_list<T> il, Compare comp);
参数:
a, b - 要比较的值
返回a和b中最大的一个。如果两者相等,则返回a。
max底层实现
template<class T>
const T& max(const T& a, const T& b)
{
return (a < b) ? b : a;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main(int argc, char const *argv[])
{
cout << " max(5,8)=="<<max(5,8)<<endl;
cout <<"max(8,5)==" <<max(8,5)<<endl;
cout <<"max('a','c')=="<<max('a','c')<<endl;
cout <<"max(3.12,2.74)=="<<max(3.12,2.74)<<endl;
return 0;
}
输出结果
max_element
(1)
template <class ForwardIterator>
ForwardIterator max_element (ForwardIterator first, ForwardIterator last);
(2)
template <class ForwardIterator, class Compare>
ForwardIterator max_element (ForwardIterator first, ForwardIterator last,
Compare comp);
参数:
first, last - 定义要检验范围的向前迭代器
comp - 比较函数对象(即满足比较 (Compare) 要求的对象),若首个参数小于第二个,则返回 true 。
寻找范围 [first, last) 中的最大元素。
max_element底层实现
template <class ForwardIterator>
ForwardIterator max_element ( ForwardIterator first, ForwardIterator last )
{
if (first==last) return last;
ForwardIterator largest = first;
while (++first!=last)
if (*largest<*first) // or: if (comp(*largest,*first)) for version (2)
largest=first;
return largest;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main(int argc, char const *argv[])
{
std::vector<int> v = {3,85,45,78,23,11};
cout <<"The largest element is:"<<*max_element(v.begin(),v.end())<<endl;
cout <<"The smallest element is:"<<*min_element(v.begin(),v.end())<<endl;
return 0;
}
输出结果
min
(1)
template <class T> const T& min (const T& a, const T& b);
(2)
template <class T, class Compare>
const T& min (const T& a, const T& b, Compare comp);
(3)
template <class T> T min (initializer_list<T> il);
template <class T, class Compare>
T min (initializer_list<T> il, Compare comp);
参数:
a, b - 要比较的值
返回给定值中的较小者。
min底层实现
template<class T>
const T& min(const T& a, const T& b)
{
return (b < a) ? b : a;
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main(int argc, char const *argv[])
{
cout <<"min(5,8)=="<<min(5,8)<<endl;
cout <<"min(8,5)==" <<min(8,5)<<endl;
cout <<"min('a','c')=="<<min('a','c')<<endl;
cout <<"min(3.12,2.74)=="<<min(3.12,2.74)<<endl;
return 0;
}
输出结果
minmax
(1)
template <class T>
pair <const T&,const T&> minmax (const T& a, const T& b);
(2)
template <class T, class Compare>
pair <const T&,const T&> minmax (const T& a, const T& b, Compare comp);
(3)
template <class T>
pair<T,T> minmax (initializer_list<T> il);
template <class T, class Compare>
pair<T,T> minmax (initializer_list<T> il, Compare comp);
参数
a, b - 要比较的值
返回给定值的最小和最大者。
minmax底层实现
template <class T> pair <const T&,const T&> minmax (const T& a, const T& b) {
return (b<a) ? std::make_pair(b,a) : std::make_pair(a,b);
}
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main(int argc, char const *argv[])
{
auto ret = minmax({5,6,7,8});
cout <<" minmax({5,6,7,8})"<<ret.first<<" "<<ret.second<<endl;
return 0;
}
输出结果
排列操作
next_permutation:产生某个元素范围的按字典顺序的下一个较大的排列(函数模板)
prev_permutation:产生某个元素范围的按字典顺序的下一个较小的排列(函数模板)
next_permutation
(1)
template <class BidirectionalIterator>
bool next_permutation (BidirectionalIterator first,
BidirectionalIterator last);
(2)
template <class BidirectionalIterator, class Compare>
bool next_permutation (BidirectionalIterator first,
BidirectionalIterator last, Compare comp);
参数:
first, last - 要重排的元素范围
comp - 比较函数对象(即满足比较 (Compare) 要求的对象),若首个参数小于第二个,则返回 true 。
将范围内的元素[first,last]重新排列到下一个字典上更大的排列中。
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main(int argc, char const *argv[])
{
vector<int> v = {1,2,3,4};
cout <<"next_permutation element :";
for(int n : v)
{
cout << n <<" ";
}
cout << endl;
sort(v.begin(),v.end());
do
{
cout << v[0]<<" "<<v[1]<<" "<<v[2]<<" "<<v[3];
cout << endl;
}while (next_permutation(v.begin(),v.end()));
return 0;
}
输出结果:
prev_permutation
(1)
template <class BidirectionalIterator>
bool prev_permutation (BidirectionalIterator first,
BidirectionalIterator last );
(2)
template <class BidirectionalIterator, class Compare>
bool prev_permutation (BidirectionalIterator first,
BidirectionalIterator last, Compare comp);
参数:
first, last - 要重排的元素范围
comp - 比较函数对象(即满足比较 (Compare) 要求的对象),若首个参数小于第二个,则返回 true 。
将范围[first,last]中的元素重新排列到前面的按字典顺序排列的排列中。
举例
#include <iostream>
#include <vector>
#include <algorithm> //算法库头文件
using namespace std;
int main(int argc, char const *argv[])
{
vector<int> v = {1,2,3,4};
cout <<"prev_permutation element :";
for(int n : v)
{
cout << n <<" ";
}
cout << endl;
sort(v.begin(),v.end());
reverse (v.begin(),v.end());
do
{
cout << v[0]<<" "<<v[1]<<" "<<v[2]<<" "<<v[3];
cout << endl;
}while (prev_permutation(v.begin(),v.end()));
return 0;
}
输出结果:
扫二维码关注微信公众号,获取技术干货