#include
注意result迭代器可以用copy(haozi.begin(), haozi.end(), ostream_iterator{std::cout,"\n"});来代替输出
1.Non-modifying sequence operations://不改动顺序的函数
/
1.1.all_of
bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred);//前两个参数是迭代的起始和结束,第三个参数是判定函数体,这个函数的作用就是把迭代范围内的所有元素作为参数送进函数体,如果全体满足,就返回true,如果有一个不满足,就返回false
example:
vector haozi { 10,4,8,12,4,5,3,20,6,18}; int num = 40;
cout << all_of( haozi.begin(), haozi.end(), [num](int hao) {return hao < num;}) << endl;//[]是传递的外部的参数,可以空着,()是接收前面迭代的元素的,这个执行的过程是把haozi中的所有元素一次送入参数hao,然后hao和num进行比较,全都满足hao haozi { 10,4,8,12,4,5,3,20,6,18};
cout << all_of( haozi.begin(), haozi.end(),my_bool_or_true) << endl;
}//和上面还是一样的效果
vector haozi { 10,4,8,12,4,5,3,20,6,18,0};
int num = 30;
int num1 = 10;
cout << all_of( haozi.begin(), haozi.end(), [num,num1](int hao) {return hao;}) << endl;//这块打印出来的就是0,因为在vector中添加了一个0,所以其中有一个元素的return hao;是0,也就是false的,然后all_of只要有一个false,就会整体返回一个false
/
1.2.any_of
bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred);//前两个是迭代,第三个是函数体,区别是只要有一个满足,就true,全不满足,就false
example:
vector haozi { 10,4,8,12,4,5,3,20,6,18};
cout << any_of( haozi.begin(), haozi.end(), [](int hao) {return hao < 1;}) << endl;//没有一个元素小于1,所以false
vector haozi { 10,4,8,12,4,5,3,20,6,18};
cout << any_of( haozi.begin(), haozi.end(), [](int hao) {return hao < 5;}) << endl;//有元素小于5,所以是true
/
1.3.none_of
bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred);//前俩迭代确定范围,第三个函数体,没有一个返回true
example:
vector haozi { 10,4,8,12,4,5,3,20,6,18};
cout << none_of( haozi.begin(), haozi.end(), [](int hao) {return hao < 1;}) << endl;//全部都不满足小于1,全部都是false,所以返回true,打印出1
vector haozi { 10,4,8,12,4,5,3,20,6,18};
cout << none_of( haozi.begin(), haozi.end(), [](int hao) {return hao < 5;}) << endl;//有元素满足小于5,所以返回false,打印出0
/
1.4.for_each
Function for_each (InputIterator first, InputIterator last, Function fn);//第一个参数和第二个参数是迭代范围,第三个参数是函数体,这个函数的作用是对范围内的所有元素执行第三个参数的函数体
example:
void my_print( int num)
{
cout << "haozi:" << num << endl;
}
int main()
{
vector haozi { 10,4,8,12,4,5,3,20,6,18};
for_each( haozi.begin(), haozi.end(), my_print);
}//试了试把my_print参数去掉,报错,参数个数增加,报错
vector haozi { 10,4,8,12,4,5,3,20,6,18};
for_each( haozi.begin(), haozi.end(), [](int hao) {cout << "haozi:" << hao << endl;});//这么写和上面效果一样
/
1.5.find
InputIterator find (InputIterator first, InputIterator last, const T& val);//前两参数是迭代范围,第三个是常量,返回值是一个迭代器,要拿迭代接,失败了返回.end
example:
vector haozi { 10,4,8,12,4,5,3,20,6,18};
auto iter = find( haozi.begin(), haozi.end(),12);
cout << *iter << endl;//打印出来是12
vector haozi { 10,4,8,12,4,5,3,20,6,18};
auto iter = find( haozi.begin(), haozi.end(),55);
cout << *iter << endl;//打印了一个奇怪的值,是指到了haozi.end
vector haozi { 10,4,8,12,4,5,3,20,6,18};
auto iter = find( haozi.begin(), haozi.end(),55);
iter--;
cout << *iter << endl;//自减一位,从haozi.end挪回了最后一个元素,打印出18
/
1.6.find_if
InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);//前两迭代范围,第三个函数体,会返回第一个送进函数体是true的元素的迭代,如果没有,就返回个.end
example:
vector haozi { 10,4,8,12,4,5,3,20,6,18};
auto iter = find_if( haozi.begin(), haozi.end(),[](int hao) {return hao < 5;});
cout << *iter << endl;//打印出来4,10不小于5,4小于5了,所以把第二位的迭代返回了
/
1.7.find_if_not
InputIterator find_if_not (InputIterator first, InputIterator last, UnaryPredicate pred);//前两迭代范围,第三个函数体,会返回第一个送进函数体是false的元素的迭代,如果没有,就返回个.end
example:
vector haozi { 10,4,8,12,4,5,3,20,6,18};
auto iter = find_if_not( haozi.begin(), haozi.end(),[](int hao) {return hao < 11;});
cout << *iter << endl;//打印12,因为12是第一个不小于11的元素
/
1.8.find_end
ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2, ForwardIterator2 last2);//四个参数分别是母体迭代范围,和需要查找的子序的迭代范围,作用就是检查子序在不在母序中,如果存在,返回符合的母体的第一位的元素的迭代器,没有返回.end
example:
vector haozi { 10,4,8,12,4,5,3,20,6,18};
vector Xqiang { 12,4,5,3};
auto iter = find_end( haozi.begin(), haozi.end(),Xqiang.begin(), Xqiang.end());
cout << *iter << endl;//打印的是12,12,4,5,3在haozi的序列中,查到了
vector haozi { 10,4,8,12,4,5,3,20,6,18,12,4,5,3};
vector Xqiang {12,4,5,3};
auto iter = find_end( haozi.begin(), haozi.end(),Xqiang.begin(), Xqiang.end());
iter--;
cout << *iter << endl;//打印的是18,说明是返回的最后一个符合的迭代
ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2, ForwardIterator2 last2,BinaryPredicate pred);//前四个参数和上面一样,多的参数是函数体
example:
vector haozi { 10,4,8,12,4,5,3,20,6,18,12,4,5,3};
vector Xqiang { 13,5,6,4};
auto iter = find_end( haozi.begin(), haozi.end(),Xqiang.begin(), Xqiang.end(), [](int hao, int qiang) {return hao < qiang;});
iter--;
cout << *iter << endl;//打印的18
vector haozi { 10,4,8,12,4,5,3,20,6,18,12,4,5,3};
vector Xqiang { 13,5,6,3};//把4改成了3
auto iter = find_end( haozi.begin(), haozi.end(),Xqiang.begin(), Xqiang.end(), [](int hao, int qiang) {return hao < qiang;});
cout << *iter << endl;//打印了一个随机数,说明指到了.end
/
1.9.find_first_of
InputIterator find_first_of (InputIterator first1, InputIterator last1,ForwardIterator first2, ForwardIterator last2);//还是母子四迭代,先选取母序中的一个元素,然后子序的所有元素依次和这个元素比,只要有一个相等,就返回一个母序列的迭代器
example:
vector haozi { 10,4,8,12,4,5,3,20,6,18,12,4,5,3};
vector Xqiang { 12,5,8,3};
auto iter = find_first_of( haozi.begin(), haozi.end(),Xqiang.begin(), Xqiang.end());
cout << *iter << endl;//打印出来是8
vector haozi { 10,4,8,12,4,5,3,20,6,18,12,4,5,3};
vector Xqiang { 111,222,333,444};
auto iter = find_first_of( haozi.begin(), haozi.end(),Xqiang.begin(), Xqiang.end());
cout << *iter << endl;//打出了随机数,都不满足,所以指向了.end
InputIterator find_first_of (InputIterator first1, InputIterator last1,ForwardIterator first2, ForwardIterator last2,BinaryPredicate pred);//母子四迭代,第五个参数是函数体
example:
vector haozi { 10,4,8,12,4,5,3,20,6,18,12,4,5,3};
vector Xqiang { 111,30,333,444};
auto iter = find_first_of( haozi.begin(), haozi.end(),Xqiang.begin(), Xqiang.end(), [](int hao,int qiang) { return (hao + 10) == qiang;});
cout << *iter << endl;//打印出来的是20
/
1.10.adjacent_find
ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last);//两个参数是迭代范围,输出其中两个挨着的并且相同的前一个元素的迭代,如果没有,返回.end
example:
vector haozi { 10,4,8,12,4,5,3,3,20,6,18,12,4,5,3};
auto iter = adjacent_find( haozi.begin(), haozi.end());
iter--;
cout << *iter << endl;//打印5
vector haozi { 10,4,8,12,4,5,3,,20,6,18,12,4,5,3};//把两个连续的3删掉一个
auto iter = adjacent_find( haozi.begin(), haozi.end());
cout << *iter << endl;//打印一个奇怪的数,指向了.end
ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last,BinaryPredicate pred);//两个迭代,一个函数体
example:
vector haozi { 10,4,8,12,4,5,3,20,6,18,12,4,5,3};
auto iter = adjacent_find( haozi.begin(), haozi.end(), [](int hao1,int hao2) { return (hao1 + 4) == hao2;});
cout << *iter << endl;//打印4,这说明打印的是第一个符合的,后面不检查了就
vector haozi { 10,4,8,12,4,5,3,20,6,18,12,4,5,3};
auto iter = adjacent_find( haozi.rbegin(), haozi.rend(), [](int hao1,int hao2) { return (hao1 - 4) == hao2;});
cout << *iter << endl;//用反向迭代,打印的是12,说明反向检查到满足的,后面的也不检查了
/
1.11.count
difference_type count (InputIterator first, InputIterator last, const T& val);//前两参是迭代范围,第三参是元素,返回这个元素在迭代范围内出现的次数,用int接就可以
example:
vector haozi { 10,4,8,12,4,5,3,20,6,18,12,4,5,3};
int num = count( haozi.begin(), haozi.end(), 3);
cout << num << endl;//打印2
vector haozi { 10,4,8,12,4,5,3,20,6,18,12,4,5,3};
int num = count( haozi.begin(), haozi.end(), 44);
cout << num << endl;//打印0
/
1.12.count_if
difference_type count_if (InputIterator first, InputIterator last, UnaryPredicate pred);//前两参是迭代范围,第三参是函数体,返回符合的元素的个数
example:
vector haozi { 10,4,8,12,4,5,3,20,6,18,12,4,5,3};
int num = count_if( haozi.begin(), haozi.end(), [](int hao) { return (hao - 4) == 1;});
cout << num << endl;//打印2,因为有两个5符合条件
/
1.13.mismatch
pair
example:
vector haozi { 10,4,8,12,4,5,3,20,6,18,12,4,5,3};
vector Xqiang { 10,4,8,11,4};
auto my_pair = mismatch( haozi.begin(), haozi.end(), Xqiang.begin());
cout << *my_pair.first << endl;//打印12
cout << *my_pair.second << endl;//打印11
vector haozi { 10,4,8,12,4,5,3,20,6,18,12,4,5,3};
vector Xqiang { 10,4,8,12,4};
auto my_pair = mismatch( haozi.begin(), haozi.end(), Xqiang.begin());
my_pair.second--;
cout << *my_pair.first << endl;//打印5
cout << *my_pair.second << endl;//打印4,说明指的地方是.end
vector haozi { 10,4,8,12,4};
vector Xqiang { 10,4,8,12,4,7,6};
auto my_pair = mismatch( haozi.begin(), haozi.end(), Xqiang.begin());
cout << *my_pair.first << endl;//打印随机数,指向了.end
cout << *my_pair.second << endl;//打印7,haozi走向了end以后Xqiang的迭代也不走了
pair
example:
vector haozi { 10,4,8,12,4,5,3,20,6,18,12,4,5,3};
vector Xqiang { 12,6,10,13,6};
auto my_pair = mismatch( haozi.begin(), haozi.end(), Xqiang.begin(), []( int hao, int qiang){ return (hao + 2) == qiang;});
cout << *my_pair.first << endl;//打印12
cout << *my_pair.second << endl;//打印13
/
1.14.equal
bool equal (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2);//前两个迭代范围,一直走,在这个范围内,第三个迭代的数是否和前面的相等
example:
vector haozi { 12,6,10,13,6};
vector Xqiang { 12,6,10,13,6};
bool my_bool = equal( haozi.begin(), haozi.end(), Xqiang.begin());
cout << my_bool << endl;//打印1
vector haozi { 12,6,10,13,6};
vector Xqiang { 12,6,10,13,6,7};
bool my_bool = equal( haozi.begin(), haozi.end(), Xqiang.begin());
cout << my_bool << endl;//还是打印1
vector haozi { 12,6,10,13,6};
vector Xqiang { 12,6,10,13};
bool my_bool = equal( haozi.begin(), haozi.end(), Xqiang.begin());
cout << my_bool << endl;//0了
bool equal (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, BinaryPredicate pred);//一样,第四参是函数体
example:
vector haozi { 12,6,10,13,6};
vector Xqiang { 13,7,11,14,7};
bool my_bool = equal( haozi.begin(), haozi.end(), Xqiang.begin(), []( int hao, int qiang) { return (hao + 1) == qiang ;});
cout << my_bool << endl;//打印1,同样如果Xqiang的元素多了,也不会有影响,两个序列同步检查,检查到haozi.end结束以后,Xqiang的迭代也不再增了
/
1.15.is_permutation
bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2);//前两个是迭代范围,第三个是迭代起始,作用是元素相同,但顺序可以不一样,返回true,否则false
example:
vector haozi { 10,4,8,12,4};
vector Xqiang { 4,10,8,4,12};
auto my_bool = is_permutation( haozi.begin(), haozi.end(), Xqiang.begin());
cout << my_bool << endl;//打印1
vector haozi { 10,4,8,12,4};
vector Xqiang { 4,10,8,4,12,1};
auto my_bool = is_permutation( haozi.begin(), haozi.end(), Xqiang.begin());
cout << my_bool << endl;//依然打印1,说明第三个参数传进来的元素检查范围还是和前两个参数的迭代范围保持同步自加
bool is_permutation(ForwardIterator1 first1,ForwardIterator1 last1,ForwardIterator2 first2,BinaryPredicate pred)//没太想通这个怎么传函数体。。。也没查到什么好例子
/
1.16.search
ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2, ForwardIterator2 last2);//前俩迭代母序列,后俩迭代子序列,返回子序列所在的母序列的位置的首位的迭代
example:
vector haozi { 22,44,55,77,4,10,8,4,12,78,89,34};
vector Xqiang { 4,10,8,4,12};
auto my_iter = search( haozi.begin(), haozi.end(), Xqiang.begin(), Xqiang.end());
my_iter--;
cout << *my_iter << endl;//打印77,Xqiang是haozi的子序,找到位置4,再自减,指向了77
vector haozi { 22,44,55,77,5,10,8,4,12,78,89,34};//4改成了5
vector Xqiang { 4,10,8,4,12};
auto my_iter = search( haozi.begin(), haozi.end(), Xqiang.begin(), Xqiang.end());
my_iter--;
cout << *my_iter << endl;//打印34,Xqiang不是haozi的子序,说明返回值指向了haozi.end
ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2, ForwardIterator2 last2,BinaryPredicate pred);//前四参一样,第五参函数体
example:
vector haozi { 22,44,55,77,4,10,8,4,12,78,89,34};
vector Xqiang { 14,20,18,14,22};
auto my_iter = search( haozi.begin(), haozi.end(), Xqiang.begin(), Xqiang.end(), [](int hao, int qiang){ return ( hao + 10) == qiang;});
my_iter--;
cout << *my_iter << endl;//打印77,按函数体匹配上了,找到位置4,再自减,指向了77
/
1.17.search_n
ForwardIterator search_n (ForwardIterator first, ForwardIterator last,Size count, const T& val);//前两参是迭代范围,第三个参数是几,第四个是元素,检查范围内,有没有连续的相同的几个指定的元素,如果有返回首位迭代
example:
vector haozi { 22,44,55,77,4,10,8,4,12,78,89,34};
auto my_iter = search_n( haozi.begin(), haozi.end(),2,4);
my_iter--;
cout << *my_iter << endl;//打印34,说明没有连着的两个4,返回值指向了haozi.end
vector haozi { 22,44,55,77,4,4,10,8,12,78,89,34};
auto my_iter = search_n( haozi.begin(), haozi.end(),2,4);
my_iter--;
cout << *my_iter << endl;//打印77,有连着的两个4,第一个4的位置
ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,Size count, const T& val, BinaryPredicate pred );//
example:
vector haozi { 22,44,55,77,4,4,6,10,8,12,78,89,34};
auto my_iter = search_n( haozi.begin(), haozi.end(),2,4, [](int hao1, int hao2) {return hao1 + hao2 == 8;});
my_iter--;
cout << *my_iter << endl;//打印77,实际上这个函数体,除了俩元素相等之外,没想到其他的能用,试传3个参数进去,报错,也没查到好的例子,就这样吧
/
2.Modifying sequence operations://修改位置的操作
/
2.1.copy
OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result);//前两参数是迭代范围,第三个是迭代开始,将前两参的元素复制到第三参,复制的过程是按照迭代范围的迭代器同步走的,就是迭代范围是几步,就会复制进去几个,返回值是复制结束迭代指的位置,指向的是第三个参数的位置
example:
vector haozi { 1,3};
vector Xqiang { 14,20,18,14,22};
auto my_iter = copy( Xqiang.begin(), Xqiang.end(), haozi.begin());
my_iter ++;//打印18,虽然后面遍历的时候只有两个元素,但是实际上元素都存进去了
cout << *my_iter << endl;
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印14,20说明复制了两个元素进去
vector haozi { 1,3,4,66,123,43,45,76,21,43};
vector Xqiang { 14,20,18,14,22};
auto my_iter = copy( Xqiang.begin(), Xqiang.end(), haozi.begin());
my_iter ++;
cout << *my_iter << endl;//打印45
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印14,20,18,14,22,45,76,21,43
/
2.2.copy_n
OutputIterator copy_n (InputIterator first, Size n, OutputIterator result);//第一个参数,迭代起始,第二个参数,复制元素个数,第三个参数,接收的迭代起始,作用是将第一个参数起始位置的第二参数个元素复制到第三参起始的位置,返回指向第三参接收结束的迭代器
example:
vector haozi { 1,3,4,66,123,43,45,76,21,43};
vector Xqiang { 14,20,18,14,22};
auto my_iter = copy_n( Xqiang.begin(), 3, haozi.begin());
my_iter ++;//打印123
cout << *my_iter << endl;
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印14,20,18,66,123,43,45,76,21,43
/
2.3.copy_if
OutputIterator copy_if (InputIterator first, InputIterator last,OutputIterator result, UnaryPredicate pred);//前两参迭代范围,第三参是接收迭代范围,第四参是函数体,对前两参指定的范围内的元素一次带入函数体,符合条件的一次向第三参指定的位置开始复制,覆盖原有元素,返回的是复制完成以后被覆盖的迭代位置
example:
vector haozi { 1,3,4,66,123,43,45,76,21,43};
vector Xqiang { 14,20,18,14,22};
auto my_iter = copy_if( Xqiang.begin(), Xqiang.end(), haozi.begin(), [] (int qiang) { return (qiang % 4) != 0;});
my_iter ++;
cout << *my_iter << endl;//打印43,说明返回值随着复制过程走了四步
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印14,18,14,22,123,43,45,76,21,43说明符合条件的四个数被复制进来了
/
2.4.copy_backward
BidirectionalIterator2 copy_backward (BidirectionalIterator1 first,BidirectionalIterator1 last,BidirectionalIterator2 result);//前两参是要被复制的迭代范围,第三参是接收的迭代尾巴,不一样在把前两参的范围内元素的尾巴和第三参对齐,然后复制过来,返回值是复制的头
example:
vector haozi { 1,3,4,66,123,43,45,76,21,43};
vector Xqiang { 14,20,18,14,22};
auto my_iter = copy_backward( Xqiang.begin(), Xqiang.end(), haozi.end());
my_iter ++;
cout << *my_iter << endl;//打印20,复制过来以后迭代指向14,再自加指向20
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印1,3,4,66,123,14,20,18,14,22从第一个14开始,就是从Xqiang里面复制过来的元素了
/
2.5.move
OutputIterator move (InputIterator first, InputIterator last, OutputIterator result);//将前两参指定的迭代范围的数,移动到第三参为指定的起始位置中,返回被赋值的组的迭代位置
example:
vector haozi{1,2,3,4,5,6};
vector Xqiang { 14,20,18,14,22};
auto my_iter = move( Xqiang.begin(), Xqiang.end(), haozi.begin());
cout << *my_iter << endl;//打印6,说明指向了haozi的最后一个位置
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印14,20,18,14,22,6说明前5个元素移动过来了,后面元素保留了
for( auto iter = Xqiang.begin(); iter != Xqiang.end(); iter++)
{
cout<< *iter << endl;
}//打印14,20,18,14,22说明还是赋值操作,并没有把X强置空
/
2.6.move_backward
BidirectionalIterator2 move_backward (BidirectionalIterator1 first,BidirectionalIterator1 last,BidirectionalIterator2 result);//将前两参指定的迭代范围的数,移动到第三参为指定的尾巴的位置中,返回被赋值的组的头迭代位置
example:
vector haozi{1,2,3,4,5,6};
vector Xqiang { 14,20,18,14,22};
auto my_iter = move_backward ( Xqiang.begin(), Xqiang.end(), haozi.end());
cout << *my_iter << endl;//打印1,指向被赋值的haozi的第一个位置,因为后5个位置被赋值了
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印1,14,20,18,14,22说明是两个尾巴对齐,然后反向挪过来
/
2.7.swap
void swap (T& a, T& b)//交换两个变量的值
void swap(T (&a)[N], T (&b)[N])//交换两个组的值,不同元素个数也可以交换
example:
int x = 10;
int y = 20;
swap( x,y);
cout << "x:" << x < haozi{1,2,3,4,5,6,7};
vector Xqiang { 14,20,18,14,22};
swap( haozi, Xqiang);
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印14,20,18,14,22
for( auto iter1 = Xqiang.begin(); iter1 != Xqiang.end(); iter1++)
{
cout<< *iter1 << endl;
}//打印1,2,3,4,5,6,7
/
2.8.swap_ranges
ForwardIterator2 swap_ranges (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2);前两个迭代指定范围,和最后一个迭代作为起始进行交换,返回值是最后一个迭代指向的被换掉的最后一个值的下一位
example:
vector haozi{1,2,3,4,5,6,7};
vector Xqiang { 14,20,18,14,22,567,321,234,444};
auto my_iter = swap_ranges( haozi.begin(),haozi.end(), Xqiang.begin());
cout << *my_iter << endl;//打印Xqiang的第8个元素234
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印14,20,18,14,22,567,321
for( auto iter1 = Xqiang.begin(); iter1 != Xqiang.end(); iter1++)
{
cout<< *iter1 << endl;
}//打印1,2,3,4,5,6,7,234,444
vector haozi{1,2,3,4,5,6,7};
vector Xqiang { 14,20,18,14,22};//留下5个元素
auto my_iter = swap_ranges( haozi.begin(),haozi.end(), Xqiang.begin());
cout << *my_iter << endl;//打印一个随机数,是Xqiang.begin()+8指向的随机数
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印14,20,18,14,22,随机数,随机数,强行换过来7个元素,Xqiang的第6和第7个元素是没指定的,是随机的
for( auto iter1 = Xqiang.begin(); iter1 != Xqiang.end(); iter1++)
{
cout<< *iter1 << endl;
}//打印1,2,3,4,5
/
2.9.iter_swap
void iter_swap (ForwardIterator1 a, ForwardIterator2 b);//两个指定迭代位置单个元素互换
example:
vector haozi{1,2,3,4,5,6,7};
iter_swap( haozi.begin(),haozi.begin() + 1);
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印2,1,3,4,5,6,7
/
2.10.transform
OutputIterator transform (InputIterator first1, InputIterator last1,OutputIterator result, UnaryOperation op);//前两个参数是输入1的迭代范围,第三个参数是输出的保存位置,第四个是函数体,返回值是变换结束后存储的位置的尾巴的迭代
example:
vector haozi{1,2,3,4,5,6,7};
auto iter = transform( haozi.begin(), haozi.end(), haozi.begin(), [](int hao) { return hao + 10;});
iter -- ;
cout << *iter << endl;//打印17
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印11,12,13,14,15,16,17
transform(input.begin(), input.end(), input.begin(), ::tolower); // 这个函数会把所有的大写字母转小写
vector laofan { 'a', 'B', 'c'};
transform(laofan.begin(), laofan.end(), laofan.begin(), ::tolower);//变成了a,b,c
transform(input.begin(), input.end(), input.begin(), ::toupper); //改下最后的参数,变成小写转大写
vector laofan { 'a', 'B', 'c'};
transform(laofan.begin(), laofan.end(), laofan.begin(), ::toupper);//变成了A,B,C
//仿函数需要另外好好找一下
OutputIterator transform (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, OutputIterator result,BinaryOperation binary_op);//前两参数是迭代范围,第三参是迭代起始,第四参是结果存放迭代起始,第五参是函数体,这块的函数体允许接两个参数,返回值是存结果的迭代的end
example:
vector haozi{1,2,3,4,5};
vector Xqiang { 14,20,18,14,22};
vector laofan (5);
auto iter = transform( haozi.begin(), haozi.end(), Xqiang.begin(), laofan.begin(), [](int hao, int qiang) { return hao + qiang;});
iter -- ;
cout << *iter << endl;//打印27,说明指向了laofan.end,然后自减回到了最后一个元素
transform(laofan.begin(), laofan.end(), laofan.begin(), ::tolower);
for( auto iter = laofan.begin(); iter != laofan.end(); iter++)
{
cout<< *iter << endl;
}//打印15,22,21,18,27
/
2.11.replace
void replace (ForwardIterator first, ForwardIterator last,const T& old_value, const T& new_value);//前两参是迭代范围,第三参是旧的值,第四参是新的值,作用是把前两参范围内的旧的元素替换成新元素
example:
vector Xqiang { 14,20,18,14,22};
replace( Xqiang.begin(), Xqiang.end(), 14,999);
for( auto iter = Xqiang.begin(); iter != Xqiang.end(); iter++)
{
cout<< *iter << endl;
}//打印999,20,18,999,22
/
2.12.replace_if
void replace_if (ForwardIterator first, ForwardIterator last,UnaryPredicate pred, const T& new_value );//前两参是迭代范围,第三参是函数体,第四参是新的值,作用是把前两参范围内的符合函数体条件的元素替换成新元素
example:
vector Xqiang { 14,20,18,14,22};
replace_if( Xqiang.begin(), Xqiang.end(), []( int qiang){ return ( qiang %4) != 0;},999);
for( auto iter = Xqiang.begin(); iter != Xqiang.end(); iter++)
{
cout<< *iter << endl;
}//打印999,20,999,999,999符合条件的除以4有余数的都被替换成了999
/
2.13.replace_copy
OutputIterator replace_copy (InputIterator first, InputIterator last,OutputIterator result,const T& old_value, const T& new_value);//前两参是迭代范围,第三参是接收的迭代起始,第四个是旧值,第五个是新值,把前两参指定的迭代范围内的旧值替换成新值,然后复制到接收的迭代起始里,返回一个接收结束的迭代器
example:
vector haozi{1,2,3,4,5};
vector Xqiang { 14,20,18,14,22};
auto iter = replace_copy( Xqiang.begin(), Xqiang.end(), haozi.begin(), 14, 999);
iter -- ;
cout << *iter << endl;//打印22,这个22是haozi的22,是被赋值的元素的尾
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印999,20,18,999,22
for( auto iter = Xqiang.begin(); iter != Xqiang.end(); iter++)
{
cout<< *iter << endl;
}//打印14,20,18,14,22说明源组没有被改动
/
2.14.replace_copy_if
OutputIterator replace_copy_if (InputIterator first, InputIterator last,OutputIterator result, UnaryPredicate pred,const T& new_value);//前两参是迭代范围,第三参是接收的迭代起始,第四个是函数体,第五个是新值,把前两参指定的迭代范围内的符合函数体条件的值替换成新值,然后复制到接收的迭代起始里,返回一个接收结束的迭代器
example:
vector haozi{1,2,3,4,5};
vector Xqiang { 14,20,18,14,22};
auto iter = replace_copy_if( Xqiang.begin(), Xqiang.end(), haozi.begin(), []( int qiang){ return ( qiang % 4) != 0;}, 999);
iter -- ;
cout << *iter << endl;//打印999,指向的是haozi的尾
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印999,20,999,999,999将符合余4不为0的元素替换成999赋值到haozi.begin为起始的位置
for( auto iter = Xqiang.begin(); iter != Xqiang.end(); iter++)
{
cout<< *iter << endl;
}//打印14,20,18,14,22说明源组没有被改动
/
2.15.fill
void fill (ForwardIterator first, ForwardIterator last, const T& val);//前两个是迭代范围,第三个是值,把迭代范围内的元素,全都换成第三参的值
example:
vector haozi{1,2,3,4,5};
fill( haozi.begin(), haozi.end(), 5);
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印5,5,5,5,5
/
2.16.fill_n
OutputIterator fill_n (OutputIterator first, Size n, const T& val);//第一参是迭代起始,第二参是数字长度,第三参是元素,作用是把第一参开始的位置的第二参个元素都替换成第三参,返回一个替代完以后的下一个位置的迭代
example:
vector haozi{1,2,3,4,5};
auto iter = fill_n( haozi.begin(),3, 999);
cout << *iter << endl;//打印4
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印999,999,999,4,5
/
2.17.generate
void generate (ForwardIterator first, ForwardIterator last, Generator gen);//前两参是迭代范围,第三个是函数体,这里的作用是把每次把函数体的返回值存进迭代中,每次迭代走一步,知道走到结束
example:
vector haozi{1,2,3,4,5};
int num = 10;
generate( haozi.begin(), haozi.end(), [num](){ return num;});
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印出来10,10,10,10,10
generate( haozi.begin(), haozi.end(), [num](){ return rand()%100;});//如果换成这样会打印出来5个100以内随机数
/
2.18.generate_n
OutputIterator generate_n (OutputIterator first, Size n, Generator gen);//第一参是迭代起始,第二参是长度,第三参是函数体,作用是把第二参个第三参返回的元素存进去,返回一个存完的迭代的下一步
example:
vector haozi{1,2,3,4,5};
int num = 10;
auto iter = generate_n( haozi.begin(), 3, [num](){ return num;});
cout << *iter << endl;//打印4
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印出来10,10,10,4,5
/
2.19.remove
ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val);//把前两参迭代指定的范围中的第三参都删掉,返回一个删除后的序列中的最后一位的迭代
example:
vector Xqiang { 14,20,18,14,22};
auto my_iter = remove( Xqiang.begin(), Xqiang.end(), 14);
for( auto iter = Xqiang.begin(); iter != my_iter; iter++)//注意这里的终止条件用的是remove的返回值
{
cout<< *iter << endl;
}//打印20,18,22
/
2.20.remove_if
ForwardIterator remove_if (ForwardIterator first, ForwardIterator last,UnaryPredicate pred);//把前两参指定的范围内的符合第三参函数体条件的元素都删掉,返回一个删除后的序列中的最后一位的迭代
example:
vector Xqiang { 14,20,18,14,22};
auto my_iter = remove_if( Xqiang.begin(), Xqiang.end(), []( int qiang){ return qiang > 19;});
for( auto iter = Xqiang.begin(); iter != my_iter; iter++)//注意这里的终止条件用的是remove_if的返回值
{
cout<< *iter << endl;
}//打印14,18,14
/
2.21.remove_copy
OutputIterator remove_copy (InputIterator first, InputIterator last,OutputIterator result, const T& val);//把前两参迭代指定的范围中的第四参都删掉,然后把剩余的序列赋值到第三参指定的位置,返回一个删除后的赋值的最后一位的迭代
example:
vector haozi{1,2,3,4,5};
vector Xqiang { 14,20,18,14,22};
auto my_iter = remove_copy( Xqiang.begin(), Xqiang.end(), haozi.begin(), 14);
for( auto iter = haozi.begin(); iter != my_iter; iter++)//注意终止条件是my_iter
{
cout<< *iter << endl;
}//打印20,18,22
for( auto iter = Xqiang.begin(); iter != Xqiang.end(); iter++)
{
cout<< *iter << endl;
}//打印14,20,18,14,22说明原序列无变动
/
2.22.remove_copy_if
OutputIterator remove_copy_if (InputIterator first, InputIterator last,OutputIterator result, UnaryPredicate pred);//把前两参迭代指定的范围中的符合第四参函数体的都删掉,然后把剩余的序列赋值到第三参指定的位置,返回一个删除后的赋值的最后一位的迭代
example:
vector haozi{1,2,3,4,5};
vector Xqiang { 14,20,18,14,22};
auto my_iter = remove_copy_if( Xqiang.begin(), Xqiang.end(), haozi.begin(),[]( int qiang){ return qiang > 19;} );
for( auto iter = haozi.begin(); iter != my_iter; iter++)//注意终止条件是my_iter
{
cout<< *iter << endl;
}//打印14,18,14
for( auto iter = Xqiang.begin(); iter != Xqiang.end(); iter++)
{
cout<< *iter << endl;
}//打印14,20,18,14,22说明原序列无变动
/
2.23.unique
ForwardIterator unique (ForwardIterator first, ForwardIterator last);//两参是迭代范围,把连续的一样的数字只留下一个,返回的迭代用于新的遍历终止点
example:
vector haozi{1,2,3,4,5,6,6,6,6,6,6,7,8,8,8,8,9,9,1};
auto my_iter = unique( haozi.begin(), haozi.end());
for( auto iter = haozi.begin(); iter != my_iter; iter++)
{
cout<< *iter << endl;
}//打印1,2,3,4,5,6,7,8,9,1
ForwardIterator unique (ForwardIterator first, ForwardIterator last,BinaryPredicate pred);//两参是迭代范围,第三参是函数体,指把连续相同数符合条件的删了,返回值迭代用于新的遍历终止点
example:
vector haozi{1,2,3,4,5,6,6,6,6,6,6,7,8,8,8,8,9,9,1};
auto my_iter = unique( haozi.begin(), haozi.end(), [](int hao1, int hao2){ return hao1 + hao2 == 12;});
for( auto iter = haozi.begin(); iter != my_iter; iter++)
{
cout<< *iter << endl;
}//打印1,2,3,4,5,6,7,8,8,8,8,9,9,1
/
2.24.unique_copy
OutputIterator unique_copy (InputIterator first, InputIterator last,OutputIterator result);//前两参是源迭代范围,最后一参是目标赋值起始,返回的迭代用于遍历终止点
example:
vector haozi{1,2,3,4,5,6,6,6,6,6,6,7,8,8,8,8,9,9,1};
vector Xqiang { 14,20,18,14,22};
auto my_iter = unique_copy( haozi.begin(), haozi.end(),Xqiang.begin());
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印1,2,3,4,5,6,6,6,6,6,6,7,8,8,8,8,9,9,1,说明对源组无影响
for( auto iter = Xqiang.begin(); iter != my_iter; iter++)
{
cout<< *iter << endl;
}//打印1,2,3,4,5,6,7,8,9,1
OutputIterator unique_copy (InputIterator first, InputIterator last,OutputIterator result, BinaryPredicate pred);//前两参是源迭代范围,第三参是目标赋值起始,最后一参是函数体,返回的迭代用于遍历终止点
example:
vector haozi{1,2,3,4,5,6,6,6,6,6,6,7,8,8,8,8,9,9,1};
vector Xqiang { 14,20,18,14,22};
auto my_iter = unique_copy( haozi.begin(), haozi.end(),Xqiang.begin(),[](int hao1, int hao2){ return hao1 + hao2 == 12;});
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印1,2,3,4,5,6,6,6,6,6,6,7,8,8,8,8,9,9,1,说明对源组无影响
for( auto iter = Xqiang.begin(); iter != my_iter; iter++)
{
cout<< *iter << endl;
}//打印1,2,3,4,5,6,7,8,8,8,8,9,9,1
/
2.25.reverse
void reverse (BidirectionalIterator first, BidirectionalIterator last);//两参迭代范围,元素反向排列
example:
vector haozi{1,2,3,4,5,6,7};
reverse( haozi.begin(), haozi.end());
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印7,6,5,4,3,2,1
/
2.26.reverse_copy
OutputIterator reverse_copy (BidirectionalIterator first,BidirectionalIterator last, OutputIterator result);//前两参迭代范围,第三参迭代起始,将反向后的向量赋值给第三参作为起始,返回一个迭代器作为遍历终点
example:
vector haozi{1,2,3,4,5,6,7};
vector Xqiang { 14,20,18,14,22};
auto my_iter = reverse_copy( haozi.begin(), haozi.end(), Xqiang.begin());
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印1,2,3,4,5,6,7说明对源无影响
for( auto iter = Xqiang.begin(); iter != my_iter; iter++)
{
cout<< *iter << endl;
}//打印7,6,5,4,3,2,1
/
2.27.rotate
void rotate (ForwardIterator first, ForwardIterator middle,ForwardIterator last);//以第一个参为起点,第二参为中间点,第三参为终止点,中间点前后元素互换
example:
vector haozi{1,2,3,4,5,6,7};
rotate( haozi.begin(), haozi.begin() + 3, haozi.end());
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印4,5,6,7,1,2,3
/
2.28.rotate_copy
OutputIterator rotate_copy (ForwardIterator first, ForwardIterator middle,ForwardIterator last, OutputIterator result);以第一个参为起点,第二参为中间点,第三参为终止点,中间点前后元素互换,然后赋值给第四参迭代的起始,返回一个迭代作为遍历终点
example:
vector haozi{1,2,3,4,5,6,7};
vector Xqiang { 14,20,18,14,22};
auto my_iter = rotate_copy( haozi.begin(), haozi.begin() + 3, haozi.end(), Xqiang.begin());
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印1,2,3,4,5,6,7
for( auto iter = Xqiang.begin(); iter != my_iter; iter++)
{
cout<< *iter << endl;
}//打印4,5,6,7,1,2,3
/
2.29.random_shuffle
void random_shuffle (RandomAccessIterator first, RandomAccessIterator last);//将迭代范围内的元素位置随机分配
example:
vector haozi{1,2,3,4,5,6,7};
random_shuffle( haozi.begin(), haozi.end());
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印5,2,7,3,1,6,4,是伪随机,应该可以改种子,但是没看到怎么改
void random_shuffle (RandomAccessIterator first, RandomAccessIterator last,RandomNumberGenerator&& gen);//第三参是函数体,这个具体怎么运行的,没太整明白
example:
vector haozi{1,2,3,4,5,6,7};
random_shuffle( haozi.begin(), haozi.end(), []( int hao){ return rand() % hao;});
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印5,2,7,3,1,6,4,
/
2.30.shuffle
void shuffle (RandomAccessIterator first, RandomAccessIterator last, URNG&& g);//没整明白
example:
/
3.Partitions://分区用的
/
3.1.is_partitioned
bool is_partitioned (InputIterator first, InputIterator last, UnaryPredicate pred);//前两参迭代指定的范围内所有元素是否满足第三个函数体的条件,满足返回true,否则false
example:
vector haozi { 14,20,18,14,22};
if( is_partitioned( haozi.begin(), haozi.end(), []( int hao){ return hao > 11;}))
{
cout << "da" <
/
3.2.partition
ForwardIterator partition (ForwardIterator first,ForwardIterator last, UnaryPredicate pred);//前两参指定的范围内的元素符合第三参函数体条件的提到前面,返回的迭代器是符合条件的结束迭代
example:
vector haozi { 14,20,18,14,22};
auto my_iter = partition( haozi.begin(), haozi.end(), []( int hao){ return hao > 19;});
for( auto iter = haozi.begin(); iter != my_iter; iter++)
{
cout<< *iter << endl;
}//打印22,20
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印20,22,18,14,14,
for( auto iter = my_iter; iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印18,14,14
/
3.3.stable_partition
BidirectionalIterator stable_partition (BidirectionalIterator first,BidirectionalIterator last,UnaryPredicate pred);//和partition区别是结束以后的元素按原顺序排列
example:
vector haozi { 14,20,18,14,22};
auto my_iter = partition( haozi.begin(), haozi.end(), []( int hao){ return hao > 19;});
for( auto iter = haozi.begin(); iter != my_iter; iter++)
{
cout<< *iter << endl;
}//打印20,22
for( auto iter = my_iter; iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印14,18,14
/
3.4.partition_copy
pair
example:
vector haozi { 14,20,18,14,22};
vector haozi1(5), haozi2(5);
auto my_iter = partition_copy( haozi.begin(), haozi.end(), haozi1.begin(), haozi2.begin(), []( int hao){ return hao > 19;});
for( auto iter = haozi1.begin(); iter != my_iter.first; iter++)
{
cout<< *iter << endl;
}//打印20,22
for( auto iter = haozi2.begin(); iter != my_iter.second; iter++)
{
cout<< *iter << endl;
}//打印14,18,14
/
3.5.partition_point
ForwardIterator partition_point (ForwardIterator first, ForwardIterator last,UnaryPredicate pred);//没弄明白什么意思,示例给的和输出结果不对,攻略也没太找到
/
4.Sorting://排序
/
4.1.sort
void sort (RandomAccessIterator first, RandomAccessIterator last);//第一参到第二参范围内元素升序排序
example:
vector haozi { 14,20,18,14,22};
sort( haozi.begin(), haozi.end());
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印14,14,18,20,22
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);//前两参迭代范围内的元素按函数体规则排序,函数体双参
example:
vector haozi { 14,20,18,14,22};
sort( haozi.begin(), haozi.end(), [] (int hao1, int hao2){return hao1 > hao2;});
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印22,20,18,14,14,函数体返回真则第一参排在前,否则在后
/
4.2.stable_sort
void stable_sort ( RandomAccessIterator first, RandomAccessIterator last );//和sort的区别是可以保持相对次序的不变
example:
vector haozi { 14,20,18,14,22};
stable_sort( haozi.begin(), haozi.end());
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印14,14,18,20,22
void stable_sort ( RandomAccessIterator first, RandomAccessIterator last,Compare comp );//前两参迭代范围内的元素按函数体规则排序,函数体双参
example:
vector haozi { 14,20,18,14,22};
stable_sort( haozi.begin(), haozi.end(), [] (int hao1, int hao2){return hao1 > hao2;});
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印22,20,18,14,14,函数体返回真则第一参排在前,否则在后
/
4.3.partial_sort
void partial_sort (RandomAccessIterator first, RandomAccessIterator middle,RandomAccessIterator last);//第一参起始,第二参中点,第三参终点,整个排序后,将起始到中间的排序后的元素放在中间点前面,其余的放在后面
example:
vector haozi { 14,20,18,14,22};
partial_sort( haozi.begin(), haozi.begin() + 2, haozi.end());
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印14,14,20,18,22前两位最小的排序好放在前两位了,剩下的保持原来顺序放在后三位
void partial_sort (RandomAccessIterator first, RandomAccessIterator middle,RandomAccessIterator last, Compare comp);//第一参起始,第二参中点,第三参终点,排序规则由函数体规定,将起始到中间的排序后的元素放在中间点前面,其余的放在后面
example:
vector haozi { 14,20,18,14,22};
partial_sort( haozi.begin(), haozi.begin() + 2, haozi.end(), [] (int hao1, int hao2){return hao1 > hao2 ;} );
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印22,20,14,14,18
/
4.4.partial_sort_copy
RandomAccessIterator partial_sort_copy (InputIterator first,InputIterator last,RandomAccessIterator result_first,RandomAccessIterator result_last);//将前两参的内容排序后放入后两参指定的范围内,返回的迭代器作为遍历终点
example:
vector haozi { 14,20,18,14,22};
vector yang(5);
auto my_iter = partial_sort_copy( haozi.begin(), haozi.end(), yang.begin(), yang.end());
for( auto iter = yang.begin(); iter != my_iter; iter++)
{
cout<< *iter << endl;
}//打印14,14,18,20,22
RandomAccessIterator partial_sort_copy (InputIterator first,InputIterator last,RandomAccessIterator result_first,RandomAccessIterator result_last, Compare comp);//多一参,函数体
example:
vector haozi { 14,20,18,14,22};
vector yang(5);
auto my_iter = partial_sort_copy( haozi.begin(), haozi.end(), yang.begin(), yang.end(),[] (int hao1, int hao2){return hao1 > hao2 ;});
for( auto iter = yang.begin(); iter != my_iter; iter++)
{
cout<< *iter << endl;
}//打印22,20,18,14,14
/
4.5.is_sorted
bool is_sorted (ForwardIterator first, ForwardIterator last);//检查两迭代范围内的元素是不是按升序排序的,如果是,true,不是就false
example:
vector haozi { 14,22};
cout << is_sorted( haozi.begin(), haozi.end()) << endl;//打印1
vector haozi { 14,17,21,12,22};
cout << is_sorted( haozi.begin(), haozi.end()) << endl;//打印0
ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last,Compare comp);//检查两迭代范围内的元素是否按第三参的函数体的顺序升序
example:
vector haozi { 22,14};
cout << is_sorted( haozi.begin(), haozi.end(), [] (int hao1, int hao2){return hao1 > hao2 ;}) << endl;//打印1
vector haozi { 14,17,21,12,22};
cout << is_sorted( haozi.begin(), haozi.end(), [] (int hao1, int hao2){return hao1 > hao2 ;}) << endl;//打印0
/
4.6.is_sorted_until
ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last);//两参内符合升序的从前往后数直到有一个不符合的
example:
vector haozi { 17,14,21,12,22};
cout << *is_sorted_until( haozi.begin(), haozi.end()) << endl;//打印12,说明直到12前一个元素,还符合升序排列,然后21以后不符合,迭代指向12
ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last,Compare comp);//检查是否符合函数体规则,直到有一个不符合的
example:
vector haozi { 14,17,21,12,22};
cout << *is_sorted_until( haozi.begin(), haozi.end(), [] (int hao1, int hao2){return hao1 > hao2 ;}) << endl;//打印21,因为17满足大于14,然后14不满足大于21,指向下一个元素21
/
4.7.nth_element
void nth_element (RandomAccessIterator first, RandomAccessIterator nth,RandomAccessIterator last);//第一参和第三参是整个范围,第二参是指第几大,用迭代形式只是,这个函数的作用是把第几大的元素放在第几位上,其余的随机排列
example:
vector haozi { 17,14,21,12,22,13,18,40};
nth_element( haozi.begin(), haozi.begin() + 3, haozi.end());
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印14,13,12,17,18,22,21,40第四大元素是17,在第四个位置上
void nth_element (RandomAccessIterator first, RandomAccessIterator nth,RandomAccessIterator last, Compare comp);//多一参是指定的函数体,排列规则按函数体指定
example:
vector haozi { 17,14,21,12,22,13,18,40};
nth_element( haozi.begin(), haozi.begin() + 3, haozi.end(),[] (int hao1, int hao2){return hao1 > hao2; });
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout<< *iter << endl;
}//打印22,40,21,18,17,13,14,12第四小元素是18,在第四个位置上
/
5.Binary search(operating on partitioned/sorted ranges)?/二分查找,和查找函数有重叠,主要是大量有序数据的时候使用,会提速
/
5.1.lower_bound
ForwardIterator lower_bound(ForwardIterator first,ForwardIterator last,const T&val);//前两参指定迭代范围,第三参是元素,返回第一个大于等于第三参的元素的位置
example:
vector haozi { 17,14,21,12,22,13,18,40};
cout << "pos:" << lower_bound( haozi.begin(), haozi.end(), 21) - haozi.begin() << endl;//打印pos:2 第二个元素21是满足大于等于21的条件的,所以返回了指向第二个元素的迭代,然后减去begin,得到2
ForwardIterator lower_bound(ForwardIterator first,ForwardIterator last,const T&val,Compare comp);//最后一参指定函数体,传双参,必须是降序排列的向量用
example:
vector haozi { 8,7,6,5,4,3,2,1};
cout << "pos:" << lower_bound( haozi.begin(), haozi.end(), 3, [](int hao1, int hao2){return hao1 > hao2;}) - haozi.begin() << endl;//打印5
/
5.2.upper_bound
ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,const T& val);//和lower_bound区别是大于等于换成大于
example:
vector haozi { 17,14,21,12,22,13,18,40};
cout << "pos:" << upper_bound( haozi.begin(), haozi.end(), 21) - haozi.begin() << endl;//打印pos:4 第四个元素22是满足大于21的条件的,所以返回了指向第三个元素的迭代,然后减去begin,得到4
ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,const T& val, Compare comp);//加入函数体,必须是降序排列的向量用
example:
vector haozi { 8,7,6,5,4,3,2,1};
cout << "pos:" << upper_bound( haozi.begin(), haozi.end(), 3, [](int hao1, int hao2){return hao1 > hao2;}) - haozi.begin() << endl;//打印6
/
5.3.equal_range
pair
example:
vector haozi { 17,14,21,12,22,13,18,40};
auto my_iter = equal_range( haozi.begin(), haozi.end(), 21);
cout << *my_iter.first << endl;//打印21
cout << *my_iter.second << endl;//打印22
pair
example:
vector haozi { 8,7,6,5,4,3,2,1};
auto my_iter = equal_range( haozi.begin(), haozi.end(), 3, []( int hao1, int hao2){ return hao1 > hao2;});
cout << *my_iter.first << endl;//打印3
cout << *my_iter.second << endl;//打印2
/
5.4.binary_search
bool binary_search (ForwardIterator first, ForwardIterator last,const T& val);//前两参指定范围,寻找是否存在第三参
example:
vector haozi { 17,14,21,12,22,13,18,40};
cout << binary_search( haozi.begin(), haozi.end(), 21)<< endl;//打印1
vector haozi { 17,14,21,12,22,13,18,40};
cout << binary_search( haozi.begin(), haozi.end(), 32)<< endl;//打印0
bool binary_search (ForwardIterator first, ForwardIterator last,const T& val, Compare comp);//这块带的函数体没太明白
/
6.Merge (operating on sorted ranges)?/拼接算法(在已排序的范围上操作)
/
6.1.merge
OutputIterator merge (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,OutputIterator result);//前两参指定一个迭代范围,第三参和第四参指定一个迭代范围,将两个范围拼接,拼接时第三四参的范围在前,拼接以后,赋值给第五参位起始位置的空间,返回值是赋值结束的迭代位置
example:
vector haozi { 17,14,21,12,22,13,18,40};
vector yang { 1,2,3,4,5,6,7,8};
vector ronghao(18) ;
auto my_iter = merge( haozi.begin(), haozi.end(), yang.begin(), yang.end(), ronghao.begin());
for( auto iter = ronghao.begin(); iter != my_iter; iter++)
{
cout << *iter <
OutputIterator merge (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp);//带函数体,函数体运行时前参为第一二参的元素,后参为第三四参的元素,函数返回false时写入第三四参元素,true时写入第一二参元素,写入的元素将不再被取参,依次取两范围,直到取完
example:
vector haozi { 17,14,21,12,22,13,18,40};
vector yang { 1,2,3,4,5,18,7,8};
vector ronghao(18) ;
auto my_iter = merge( haozi.begin(), haozi.end(), yang.begin(), yang.end(), ronghao.begin(), []( int hao1, int hao2){ return hao1 < hao2 ;});
for( auto iter = ronghao.begin(); iter != my_iter; iter++)
{
cout << *iter <
/
6.2.inplace_merge
void inplace_merge (BidirectionalIterator first, BidirectionalIterator middle,BidirectionalIterator last);//三个迭代,同一个向量中的三个迭代,第一个为起始,第二个为中间,第三个为结束,起始和中间必须是有序排列,中间到结束也必须是有序排列,两个有序的排列合并成一个有序的
example:
vector haozi { 3,12,17,23,33,45,1,4,16,18,22,34};
inplace_merge ( haozi.begin(), haozi.begin() + 6, haozi.end());
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout << *iter <
void inplace_merge (BidirectionalIterator first, BidirectionalIterator middle,BidirectionalIterator last, Compare comp);
example:
vector haozi { 3,12,17,23,33,45,1,4,16,18,22,34};
inplace_merge ( haozi.begin(), haozi.begin() + 6, haozi.end(), [] (int hao1, int hao2) { return hao1 < hao2;});
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout << *iter <
/
6.3.includes
bool includes ( InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2 );//看第一二参指定的迭代范围内是否存在第三四参范围内的所有元素,并且先后顺序必须对,不然就false
example:
vector haozi { 3,12,17,23,33,45,1,4,16,18,22,34};
vector yang {23,45,4,18};
cout << includes( haozi.begin(), haozi.end(), yang.begin(), yang.end()) << endl;//打印1
vector haozi { 3,12,17,23,33,45,1,4,16,18,22,34};
vector yang {45,23,4,18};
cout << includes( haozi.begin(), haozi.end(), yang.begin(), yang.end()) << endl;//打印0
bool includes ( InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2, Compare comp );//对函数模板有点变化,没弄明白
/
6.4.set_union
OutputIterator set_union (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,OutputIterator result);//前两参的迭代范围,和三四参的迭代范围,取并集,放入最后一参为起始,返回赋值结束的迭代,同位置如果元素相同会省略掉一个,适合有序向量,有序,有序,有序
example:
vector haozi { 3,4,5,6,7};
vector yang {2,7,17,18,23,};
vector canxiu4(20);
auto my_iter = set_union( haozi.begin(), haozi.end(), yang.begin(), yang.end(), canxiu4.begin());
for( auto iter = canxiu4.begin(); iter != my_iter; iter++)
{
cout << *iter <
OutputIterator set_union (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp);//多加函数体
example:
vector haozi { 3,4,5,6,7};
vector yang {2,7,17,18,23,};
vector canxiu4(20);
auto my_iter = set_union( haozi.begin(), haozi.end(), yang.begin(), yang.end(), canxiu4.begin(), [](int hao1, int hao2) {return hao1 < hao2;});//如果把这里改成>号,要配合降序数列来用,需要把haozi和yang变成降序数列
for( auto iter = canxiu4.begin(); iter != my_iter; iter++)
{
cout << *iter <
/
6.5.set_intersection
OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,OutputIterator result);//一二和三四分别是两个迭代范围,有序,一定要有序,然后取交集放入第五参,返回一个赋值结束的迭代指向
example:
vector haozi { 3,4,5,6,7,17,23};
vector yang {2,6,17,18,23,};
vector canxiu4(20);
auto my_iter = set_intersection( haozi.begin(), haozi.end(), yang.begin(), yang.end(), canxiu4.begin());
for( auto iter = canxiu4.begin(); iter != my_iter; iter++)
{
cout << *iter <
OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp);//增加函数体。可以用>来配合降序数组
example:
vector haozi { 3,4,5,6,7,17,23};
vector yang {2,6,17,18,23,};
sort( haozi.rbegin(), haozi.rend());
sort( yang.rbegin(), yang.rend());
vector canxiu4(20);
auto my_iter = set_intersection( haozi.begin(), haozi.end(), yang.begin(), yang.end(), canxiu4.begin(), [](int hao1, int hao2) {return hao1 > hao2;});
for( auto iter = canxiu4.begin(); iter != my_iter; iter++)
{
cout << *iter <
/
6.6.set_difference
OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,OutputIterator result);//一二参中存在,但三四参中不存在的元素,赋值给第五参作为起始,返回赋值结束的迭代,一二三四参一定要是有序序列
example:
vector haozi { 3,4,5,17,17,18,19,23};
vector yang {2,6,17,19,23,};
vector canxiu4(20);
auto my_iter = set_difference( haozi.begin(), haozi.end(), yang.begin(), yang.end(), canxiu4.begin());
for( auto iter = canxiu4.begin(); iter != my_iter; iter++)
{
cout << *iter <
OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp);//第五参函数体>配合降序数列使用
example:
vector haozi { 3,4,5,17,18,19,23};
vector yang {2,6,17,19,23,};
sort( haozi.rbegin(), haozi.rend());
sort( yang.rbegin(), yang.rend());
vector canxiu4(20);
auto my_iter = set_difference( haozi.begin(), haozi.end(), yang.begin(), yang.end(), canxiu4.begin(),[](int hao1, int hao2) {return hao1 > hao2;});
for( auto iter = canxiu4.begin(); iter != my_iter; iter++)
{
cout << *iter <
/
6.7.set_symmetric_difference
OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,OutputIterator result);//第一二参指定的有序序列和第三四参指定的有序序列,两个序列中相同的元素消掉,剩下的全都赋值给第五参的起始,返回第五参的结束迭代
example:
vector haozi { 3,4,5,17,18,19,23,66};
vector yang {2,6,17,19,23};
vector canxiu4(haozi.size() + yang.size());
auto my_iter = set_symmetric_difference( haozi.begin(), haozi.end(), yang.begin(), yang.end(), canxiu4.begin());
for( auto iter = canxiu4.begin(); iter != my_iter; iter++)
{
cout << *iter <
OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp);//带参形式依然配合降序使用
example:
vector haozi { 3,4,5,17,18,19,23,66};
vector yang {2,6,17,19,23};
sort( haozi.rbegin(), haozi.rend());
sort( yang.rbegin(), yang.rend());
vector canxiu4(haozi.size() + yang.size());
auto my_iter = set_symmetric_difference( haozi.begin(), haozi.end(), yang.begin(), yang.end(), canxiu4.begin(),[](int hao1, int hao2) {return hao1 > hao2;});
for( auto iter = canxiu4.begin(); iter != my_iter; iter++)
{
cout << *iter <
/
7.Heap://堆操作,这块主要是针对二叉树之类的数据结构算法了
/
7.1.make_heap()
void make_heap (RandomAccessIterator first, RandomAccessIterator last);//生成一个堆,默认大顶堆
void make_heap (RandomAccessIterator first, RandomAccessIterator last,Compare comp );//生成一个堆,less ()大顶堆,greater ()小顶堆
example:
vector haozi { 3,4,5,17,18,19,23,66};
make_heap( haozi.begin(), haozi.end(), less ());
make_heap( haozi.begin(), haozi.end(), greater ());
/
7.2.pop_heap
void pop_heap (RandomAccessIterator first, RandomAccessIterator last);//将最大的元素下沉到最底下,剩余的元素进行堆排序
void pop_heap (RandomAccessIterator first, RandomAccessIterator last,Compare comp);//less ()大顶堆,greater ()小顶堆
example:
pop_heap( haozi.begin(), haozi.end(), less ());
/
7.3.push_heap
void push_heap (RandomAccessIterator first, RandomAccessIterator last);//堆指定范围内的元素进行堆排序
void push_heap (RandomAccessIterator first, RandomAccessIterator last,Compare comp);//less ()大顶堆,greater ()小顶堆
example:
push_heap( haozi.begin(), haozi.end(), less ());
/
7.4.sort_heap
void sort_heap (RandomAccessIterator first, RandomAccessIterator last);//将堆排列为一个有序数列,必须是堆
void sort_heap (RandomAccessIterator first, RandomAccessIterator last,Compare comp);//less ()大顶堆,greater ()小顶堆
example:
make_heap( haozi.begin(), haozi.end(), less ());
sort_heap( haozi.begin(), haozi.end(), less ());//最后一参必须和make是的一样,不然会排列混乱
/
7.5.is_heap
bool is_heap (RandomAccessIterator first, RandomAccessIterator last);//检查是不是大顶堆
bool is_heap (RandomAccessIterator first, RandomAccessIterator last,Compare comp);//检查是不是指定类型的堆less ()大顶堆,greater ()小顶堆
example:
make_heap( haozi.begin(), haozi.end(), less ());
cout << is_heap( haozi.begin(), haozi.end(),less ()) << endl;//打印1
cout << is_heap( haozi.begin(), haozi.end(),greater ()) << endl;//打印0
/
7.6.is_heap_until
RandomAccessIterator is_heap_until (RandomAccessIterator first,RandomAccessIterator last);//寻找直到还是堆的截至元素,返回其迭代
RandomAccessIterator is_heap_until (RandomAccessIterator first,RandomAccessIterator lastCompare comp);//指定检查的堆类型less ()大顶堆,greater ()小顶堆
example:
vector haozi { 3,4,5,17,18,19,23,66};
make_heap( haozi.begin(), haozi.end(), less ());
haozi.push_back(100);
haozi.push_back(111);
cout < ());
for( auto iter = haozi.begin(); iter != my_iter; iter++)
{
cout << *iter <
/
8.Min/max://求最值
/
8.1.min
template const T& min (const T& a, const T& b);//两个常量直接比较,返回较小的
example:
int num1 = 10, num2 =15;
auto my_min = min( num1, num2);
cout << "my_min:" << my_min << endl;//打印my_min:10
const T& min (const T& a, const T& b, Compare comp);//根据函数体返回比较值,真返回一参,假返回二参
example:
int num1 = 10, num2 =15;
auto my_min = min( num1, num2, []( int n1, int n2){ return n1 > n2;});
cout << "my_min:" << my_min << endl;//打印my_min:15
template T min (initializer_list il);//传入构造组比较,实现多个元素
example:
auto my_min = min( { 3,4,5,17,18,19,23,66} );
cout << "my_min:" << my_min << endl;//打印my_min:3
template
example:
auto my_min = min( { 3,4,5,17,18,19,66,23}, []( int n1, int n2){ return n1 > n2;});
cout << "my_min:" << my_min << endl;//输出了最大值,66
/
8.2.max
template const T& max (const T& a, const T& b);//两个常量直接比较,返回较大的
example:
int num1 = 10, num2 =15;
auto my_max = min( num1, num2);
cout << "my_max:" << my_max << endl;//打印my_max:15
template
example:
int num1 = 10, num2 =15;
auto my_max =max( num1, num2, []( int n1, int n2){ return n1 > n2;});
cout << "my_max:" << my_max << endl;//打印my_min:10
template T max (initializer_list il);//传入构造组比较,实现多个元素
example:
auto my_max = max( { 3,4,5,17,18,19,66,23});
cout << "my_max:" << my_max << endl;//打印66
template
example:
auto my_max = max( { 3,4,5,17,18,19,66,23}, []( int n1, int n2){ return n1 > n2;});
cout << "my_max:" << my_max << endl;//打印3
/
8.3.minmax
template pair
example:
auto my_pair = minmax( 3,16);
cout << "my_max:" << my_pair.second << endl;//my_max:16
cout << "my_min:" << my_pair.first << endl;//my_min:3
template
template pair
example:
auto my_pair = minmax( { 3,4,5,17,18,19,66,23});
cout << "my_max:" << my_pair.second << endl;//my_max:66
cout << "my_min:" << my_pair.first << endl;//my_min:3
template
/
8.4.min_element
ForwardIterator min_element (ForwardIterator first, ForwardIterator last);//迭代指定范围,返回一个最小值的迭代
example:
vector haozi { 3,4,5,17,18,19,23,66};
auto my_iter = min_element( haozi.begin(), haozi.end());
cout << "my_min:" << *my_iter << endl;//打印my_min:3
ForwardIterator min_element (ForwardIterator first, ForwardIterator last,Compare comp);//加了函数体
/
8.5.max_element
ForwardIterator max_element (ForwardIterator first, ForwardIterator last);//迭代指定范围,返回一个最大值的迭代
example:
vector haozi { 3,4,5,17,18,19,23,66};
auto my_iter = max_element( haozi.begin(), haozi.end());
cout << "my_max:" << *my_iter << endl;//打印my_max:66
ForwardIterator max_element (ForwardIterator first, ForwardIterator last,Compare comp);//加了函数体
/
8.6.minmax_element
pair
example:
vector haozi { 3,4,5,17,18,19,23,66};
auto my_pair = minmax_element( haozi.begin(), haozi.end());
cout << "my_max:" << *my_pair.second << endl;//my_max:66
cout << "my_min:" << *my_pair.first << endl;//my_min:3
pair
/
9.Other:
/
9.1.lexicographical_compare
bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2);//字典序比较,前小返回1,前大返回0
example:
vector haozi { 3,4,5,17,18,19,23,66};
vector yang { 9,2,7,8,10};
cout << lexicographical_compare( haozi.begin(), haozi.end(), yang.begin(), yang.end()) << endl;//打印1
bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2, Compare comp);//加了函数体
/
9.2.next_permutation
bool next_permutation (BidirectionalIterator first,BidirectionalIterator last);//按字典序随机排序,指导升序排列返回0,需要配合循环使用
example:
vector haozi { 4,3,999,17,18,19,23,66};
while(next_permutation( haozi.begin(), haozi.end()));
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout << *iter << endl;
}//打印3,4,17,18,19,23,66,999
bool next_permutation (BidirectionalIterator first,BidirectionalIterator last, Compare comp);//加了函数体
/
9.3.prev_permutation
bool prev_permutation (BidirectionalIterator first,BidirectionalIterator last );//按字典序随机排序,指导降序排列返回0,需要配合循环使用
example:
vector haozi { 4,3,999,17,18,19,23,66};
while(prev_permutation( haozi.begin(), haozi.end()));
for( auto iter = haozi.begin(); iter != haozi.end(); iter++)
{
cout << *iter << endl;
}//打印999,66,23,19,18,17,4,3
bool prev_permutation (BidirectionalIterator first,BidirectionalIterator last, Compare comp);//加了函数体
///