C++ STL for_each 的用法?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ STL for_each 的用法?相关的知识,希望对你有一定的参考价值。

for_each的第三个参数是什么啊?
Class CStudent

string name;
int studentID;
;
例如,vector<CStuent>ss;
如何用for_each依次输出CStudent中的name,谢啦

for_each第三个参数传入的是函数名称,通过模板生成代码后的函数指针,for_each需要调用,可以看看STL的for_each函数的源码。
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;

void print(int a)

cout << a <<"\\t";


class myInt

public:
void operator()(int x)

cout << x << "\\t";

;

template<int thevalue>
void add(int &ele)

ele += thevalue;


class AddValue

private :
int thevalue;
public:
AddValue(int va) : thevalue(va)



void operator()(int & element)

element += thevalue;


;

int main()

/*
vector<string> ve;

copy(istream_iterator<string>(cin),
istream_iterator<string>(),
back_inserter(ve));

sort(ve.begin(),ve.end());

copy(ve.begin(),ve.end(),
ostream_iterator<string>(cout,"\\t"));
*/
vector<int> ve;
for(int i = 0; i< 9; i++)

ve.push_back(i);


for_each(ve.begin(),ve.end(),print);
cout << endl;

for_each(ve.begin(),ve.end(),myInt());
cout << endl;

cout << "after add --------" << endl;
for_each(ve.begin(),ve.end(),add<10>);

for_each(ve.begin(),ve.end(),print);
cout << endl;
for_each(ve.begin(),ve.end(),AddValue(10));
for_each(ve.begin(),ve.end(),print);
cout << endl;
for_each(ve.begin(),ve.end(),AddValue(*ve.begin()));
for_each(ve.begin(),ve.end(),print);
cout << endl;
参考技术A for_each第三个参数传入的是函数名称
通过模板生成代码后的函数指针,for_each需要调用,可以看看STL的for_each函数的源码
===========================================
// 你的需求
void printName( const CStudent& _s )

std::cout<< _s.name << std::endl;

std::vector<CStudent> stu_list;
std::for_each( stu_list.begin(), stu_list.end(), printName );本回答被提问者和网友采纳
参考技术B for (vector::iterator iter = ss.begin; iter != end; iter++)

cout<<iter->name;
参考技术C #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

class CStudent

public:
CStudent(string Name, int Id)

this->name = Name;
studentID = Id;


string name;
int studentID;
;

void print(CStudent&a)

cout << "姓名:" << a.name << endl;
cout << "学号:" << a.studentID << endl;


void test01()


CStudent p1("张三", 1);
CStudent p2("李四", 2);
CStudent p3("王五", 3);

vector<CStudent>ss;
ss.push_back(p1);
ss.push_back(p2);
ss.push_back(p3);

for_each(ss.begin(), ss.end(), print);



int main(void)

test01();

system("pause");
return 0;

C++中STL学习笔记——常见算法操作演示

常见STL算法操作演示

概述

STL提供了大约100个实现算法的模版函数,比如算法for_each将为指定序列中的每一个元素调用指定的函数,stable_sort以你所指定的规则对序列进行稳定性排序等等。只要我们熟悉了STL之后,许多代码可以被大大的化简,只需要通过调用一两个算法模板,就可以完成所需要的功能并大大地提升效率。

分类

STL中算法大致分为四类:

1.非可变序列算法:指不直接修改其所操作的容器内容的算法。

2.可变序列算法:指可以修改它们所操作的容器内容的算法。

3.排序算法:对序列进行排序和合并的算法、搜索算法以及有序序列上的集合操作。

4.数值算法:对容器内容进行数值计算。

C++中STL学习笔记——常见算法操作演示

下面的代码涉及到几乎全部的STL函数,是小编纯手敲写的,已经在VSCode运行。

#define SIZE 1000#include<iostream>#include<vector>#include<algorithm>#include<functional>#include<string>#include<numeric>using namespace std;
/***************************************1.查找*************************************************///在iterator对标识元素范围内,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的ForwardIterator,否则返回lastvoid Fadjacent_find(vector<int>vec){ vec.push_back(999); vector<int>::iterator it=adjacent_find(vec.begin(),vec.end()); cout<<"The adjacent_find result:"<<*it<<endl;}
//在有序序列中查找value,找到返回truevoid Fbinary_search(vector<int>vec,int value){ bool result=binary_search(vec.begin(),vec.end(),value); cout<<"The binary_search result:"<<result<<endl;}
//利用等于操作符,把标志范围内的元素与输入值比较,返回相等元素个数。void Fcount(vector<int>vec,int value){ int result=count(vec.begin(),vec.end(),value); cout<<"The count result:"<<result<<endl;}
//利用输入的操作符,对标志范围内的元素进行操作,返回结果为true的个数bool cmp1(int num){ return num%1000;}void Fcount_if(vector<int>vec){ int result=count_if(vec.begin(),vec.end(),cmp1); cout<<"The count_if result:"<<result<<endl;}
//功能类似equal,返回一对iterator,第一个表示lower_bound,第二个表示upper_boundvoid Fequal_range(vector<int>vec,int value){ pair<vector<int>::iterator,vector<int>::iterator> result=equal_range(vec.begin(),vec.end(),value); cout<<"The equal_range result:"<<*result.first<<" "<<*result.second<<endl;}
//利用底层元素的等于操作符,对指定范围内的元素与输入值进行比较。当匹配时,结束搜索,返回该元素的Iteratorvoid Ffind(vector<int>vec,int value){ vector<int>::iterator result=find(vec.begin(),vec.end(),value); cout<<"The find result:"<<&*result<<endl;}
//在指定范围内查找"由输入的另外一对iterator标志的第二个序列"的最后一次出现。找到则返回最后一对的第一个ForwardIterator,//否则返回输入的"另外一对"的第一个ForwardIterator。重载版本使用用户输入的操作符代替等于操作。void Ffind_end(){ string str="aacaabbcddefacss"; string substr="ac"; string::iterator result=find_end(str.begin(),str.end(),substr.begin(),substr.end()); for(int i=0;i<str.length();i++){ cout<<&str[i]<<" is: "<<str[i]<<endl; } cout<<"The find_end result:"<<&*result<<" is: "<<*result<<endl;}
//在指定范围内查找"由输入的另外一对iterator标志的第二个序列"中任意一个元素的第一次出现。//第一个序列中的元素第一次出现在第二个序列中的元素void Ffind_first_of(){ string str="mnblkjhgfdsac"; string substr="ca"; string::iterator result=find_first_of(str.begin(),str.end(),substr.begin(),substr.end()); cout<<"The find_first_of result:"<<*result<<endl;}
//它接收一个函数对象的参数作为参数,并使用它来做更复杂的评价对象是否和给出的查找条件相符。bool cmp2(int value){ return value>=996;}void Ffind_if(vector<int>vec){ vector<int>::iterator result=find_if(vec.begin(),vec.end(),cmp2); cout<<"The find_if result:"<<*result<<endl;}
//返回的是第一个不小于给定元素value的位置void Flower_bound(vector<int>vec,int value){ vector<int>::iterator result=lower_bound(vec.begin(),vec.end(),value); cout<<"The lower_bound result:"<<*result<<endl;}
//返回的是第一个大于给定元素value的位置void Fupper_bound(vector<int>vec,int value){ vector<int>::iterator result=upper_bound(vec.begin(),vec.end(),value); cout<<"The upper_bound result:"<<*result<<endl;}
//search算法在一个序列中找另一个序列的第一次出现的位置void Fsearch(){ string str="mnblkjhgfdsca"; string substr="ca"; string::iterator result=search(str.begin(),str.end(),substr.begin(),substr.end()); cout<<"The search result:"<<*result<<endl;}
//search_nbool cmp3(int i,int j){ return i==j;}void Fsearch_n(){ int array[]={1,996,996,996,5,5,996,996,6,6,6,7,8,8,9,10}; vector<int>vec(array,array+16); //search_n(s,e,cnt,num);找区间内[s,e)连续cnt个的num返回的是找到的第一个num的迭代器 vector<int>::iterator result=search_n(vec.begin(),vec.end(),2,996); for(int i=0;i<vec.size();i++){ cout<<"vec["<<i<<"].address:"<<&vec[i]<<" "<<"the num is:"<<vec[i]<<endl; } cout<<"The first search_n result:"<<&*result<<" "<<*result<<endl; //search_n(s,e,cnt,num,cmp);找区间内[s,e)连续cnt个的num符合cmp()条件的,返回的是找到的第一个num的迭代器 result=search_n(vec.begin(),vec.end(), 3, 996, cmp3); cout<<"The second search_n result:"<<&*result<<" "<<*result<<endl;}
/***************************************2.排序*************************************************///inplace_merge(vi.begin(),vi.begin()+5,vi.end()),第一个参数为起始,最后一个为末尾,中间为第一个有序序列的元素个数void Finplace_merge(){ vector<int> vi{1,5,8,19,200,1,8,56,74,91,100,101,102,103}; cout<<"before inplace_merge,vi="; for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";}); cout<<endl; inplace_merge(vi.begin(),vi.begin()+5,vi.end()); cout<<"after inplace_merge,vi="; for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";}); cout<<endl; }/**merge函数的作用是:将两个已经排好序的序列合并为一个有序的序列。 * 函数参数:merge(first1,last1,first2,last2,result,compare); * firs1t为第一个容器的首迭代器,last1为第一个容器的末迭代器; * first2为第二个容器的首迭代器,last2为容器的末迭代器; * result为存放结果的容器,comapre为比较函数(可略写,默认为合并为一个升序序列)。 */bool cmp4(int iter1,int iter2){ return iter1<iter2;}void Fmerge(){ vector<int>vec1{1,5,8,19,200}; vector<int>vec2{1,8,56,74,91,100,101,102,103,996}; vector<int>vec3(vec1.size()+vec2.size()); merge(vec1.begin(),vec1.end(),vec2.begin(),vec2.end(),vec3.begin(),cmp4); cout<<"The merge result:"<<endl; for_each(vec3.begin(),vec3.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
//寻找第k位置的元素 第一个参数为起始地址,第二个为k,第三个为末尾地址void Fnth_element(){ vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996}; nth_element(vec.begin(),vec.end()-3,vec.end()); cout<<"The nth_element result:"<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
//排序出前k小的元素,第一个参数为起始地址,第二个k,最后一个为末位地址,cmp可以不写 默认升序bool cmp5(int a,int b){ return a>b;}void Fpartial_sort(){ vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996}; partial_sort(vec.begin(),vec.begin()+5,vec.end(),cmp5); cout<<"The partial_sort result:"<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
//与partial_sort类似 只是复制进一个新的容器void Fpartial_sort_copy(){ vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996}; vector<int>vec1(vec.size()); partial_sort_copy(vec.begin(),vec.end(),vec1.begin(),vec.end()); cout<<"The partial_sort_copy result:"<<endl; for_each(vec1.begin(),vec1.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
//将元素按照某种规定划分为两个集合 分组结果是乱序bool cmp6(int a){ return a%2==0;}void Fpartition(){ vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996}; partition(vec.begin(),vec.end(),cmp6); cout<<"The partition result:"<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
//stable_partition函数和partition函数差不多,分组之后只是顺序并没有被打乱。void Fstable_partition(){ vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996}; stable_partition(vec.begin(),vec.end(),cmp6); cout<<"The stable_partition result:"<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
//用来对一个元素序列进行重新排序(随机的)void Frandom_shuffle(){ vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996}; random_shuffle(vec.begin(),vec.end()); cout<<"The random_shuffle result:"<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
//将指定范围内元素重新反序排序void Freverse(){ vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996}; reverse(vec.begin(),vec.end()); cout<<"The reverse result:"<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
//与reverse类似 增加了复制操作void Freverse_copy(){ vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996}; vector<int>vec1(vec.size()); reverse_copy(vec.begin(),vec.end(),vec1.begin()); cout<<"The reverse_copy result:"<<endl; for_each(vec1.begin(),vec1.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
//将元素从mid截断 后面的放前面 前面的放后面void Frotate(){ vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996}; rotate(vec.begin(),vec.begin()+5,vec.end()); cout<<"The rotate result:"<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
//和rotate类似,多了复制操作void Frotate_copy(){ vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996}; vector<int>vec1(vec.size()); rotate_copy(vec.begin(),vec.begin()+5,vec.end(),vec1.begin()); cout<<"The rotate_copy result:"<<endl; for_each(vec1.begin(),vec1.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
//sortbool cmp7(int a,int b){ return a>b;}void Fsort(){ vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996}; sort(vec.begin(),vec.end(),cmp7); cout<<"The sort result:"<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
//stable_sortvoid Fstable_sort(){ vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996}; stable_sort(vec.begin(),vec.end(),cmp7); cout<<"The stable_sort result:"<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
/***************************************3.删除*************************************************///复制序列void Fcopy(){ vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996}; vector<int>vec1(vec.size()); copy(vec.begin(),vec.end(),vec1.begin()); cout<<"The copy result:"<<endl; for_each(vec1.begin(),vec1.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
//与copy相同,不过元素是以相反顺序被拷贝。切记:最后一个参数是新容器的尾地址void Fcopy_backward(){ vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996}; vector<int>vec1(vec.size()); copy_backward(vec.begin(),vec.end(),vec1.end()); cout<<"The copy_backward result:"<<endl; for_each(vec1.begin(),vec1.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
//交换存储在两个对象中的值struct stu{ string name; float score;};void Fswap(){ stu A,B; A.score=1;A.name="A"; B.name="B";B.score=2; swap(A,B); cout<<"A.name="<<A.name<<" "<<"A.score="<<A.score<<endl; cout<<"B.name="<<B.name<<" "<<"B.score="<<B.score<<endl;}
//swap函数有一个缺点,就是无法交换2个迭代器的值 iter_swap可以交换void Fiter_swap(){ vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996}; iter_swap(vec.begin(),vec.end()-1); cout<<"The iter_swap result:"<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
//交换两个区域中的数据 三个参数,第一个是第一个序列的起始,第二个是第一个序列的结束,第三个是第二个序列的起始void Fswap_ranges(){ vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996}; swap_ranges(vec.begin(),vec.begin()+vec.size()/2,vec.begin()+vec.size()/2); cout<<"The swap_ranges result:"<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
/** * 就像所有算法,remove接收指定它操作的元素区间的一对迭代器。 * 它不接收一个容器,所以remove不知道它作用于哪个容器。 * 此外,remove也不可能发现容器,因为没有办法从一个迭代器获取对应于它的容器。 * 想想怎么从容器中除去一个元素。 * 唯一的方法是调用那个容器的一个成员函数,几乎都是erase的某个形式, * (list有几个除去元素的成员函数不叫erase,但它们仍然是成员函数。) * 因为唯一从容器中除去一个元素的方法是在那个容器上调用一个成员函数, * 而且因为remove无法知道它正在操作的容器,所以remove不可能从一个容器中除去元素。 * 这解释了另一个令人沮丧的观点——从一个容器中remove元素 不会改变容器中元素的 个数 * **********remove并不“真的”删除东西,因为它做不到。*************/void Fremove(){ vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996}; cout<<"The remove result:"<<endl; cout<<"before remove:"; cout<<"The vec.size():"<<vec.size()<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl; remove(vec.begin(),vec.end(),1); cout<<"after remove:"; cout<<"The vec.size():"<<vec.size()<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
//附加拷贝操作,原理不再过多解释void Fremove_copy(){ vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996}; vector<int>vec1(vec.size()); remove_copy(vec.begin(),vec.end(),vec1.begin(),1024); cout<<"The remove_copy result:"<<endl; for_each(vec1.begin(),vec1.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
//附加判定,原理不再解释bool cmp8(int i){ return i%2!=0;}void Fremove_if(){ vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996}; remove_if(vec.begin(),vec.end(),cmp8); cout<<"The remove_if result:"<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
//判定加拷贝void Fremove_copy_if(){ vector<int>vec{995,1024,1,8,56,74,91,100,101,102,103,996}; vector<int>vec1(vec.size()); remove_copy_if(vec.begin(),vec.end(),vec1.begin(),cmp8); cout<<"The remove_copy_if result:"<<endl; for_each(vec1.begin(),vec1.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
//用法较多 此处仅用一种//replace_copy replace_if replace_copy_if同上略过void Freplace(){ string str="You are beautiful!"; cout<<"before replace:"<<str<<endl; str=str.replace(str.begin(),str.begin()+1,"$"); cout<<"after replace:"<<str<<endl;}
//清除序列中重复元素(需要提前有序,否则结果不符合预期),和remove类似,它也不能真正删除元素//unique_copy不再赘述void Funique(){ vector<int>vec{995,1024,1,1,1,8,56,74,91,100,8,996,1024,101,102,103,996}; sort(vec.begin(),vec.end()); cout<<"before unique:"; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl; unique(vec.begin(),vec.end()); cout<<"after unique:"; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
/***************************************4.排列组合*************************************************///按照字典全排列 next_permutation()会取得[first,last)所标示之序列的下一个排列组合,如果没有下一个排列组合,便返回false;否则返回truevoid Fnext_permutation(){ vector<int>vec{1,2,3,4}; sort(vec.begin(),vec.end()); cout<<"The next_permutation result:"<<endl; do{ for(int i=0;i<vec.size();i++) cout<<vec[i]<<" "; cout<<endl; }while(next_permutation(vec.begin(),vec.end()));}
//同上bool cmp9(int a,int b){ return a>b;}void Fprev_permutation(){ vector<int>vec{1,2,3,4}; sort(vec.begin(),vec.end(),cmp9); cout<<"The prev_permutation result:"<<endl; do{ for(int i=0;i<vec.size();i++) cout<<vec[i]<<" "; cout<<endl; }while(prev_permutation(vec.begin(),vec.end()));}
/***************************************5.算术算法*************************************************///accumulate定义在#include<numeric>中,作用有两个,一个是累加求和,另一个是自定义类型数据的处理(需要仿函数)void Faccumulate(){ vector<int>vec{1,2,3,4}; vector<string>vec1{"You","are","beautiful!"}; int result=accumulate(vec.begin(),vec.end(),0); string str=accumulate(vec1.begin(),vec1.end(),string("*****")); cout<<"The accumulate result:"<<endl; cout<<"From 1 to 4:"<<result<<endl; cout<<str<<endl;}
//partial_sum创建一个新序列,其中每个元素值代表指定范围内该位置前所有元素之和void Fpartial_sum(){ vector<int>vec{1,2,3,4}; vector<int>vec1(vec.size()); partial_sum(vec.begin(),vec.end(),vec1.begin()); cout<<"The partial_sum result:"<<endl; for_each(vec1.begin(),vec1.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
//inner_product 第一参数起始,第二个终止,第三个起始,第四个内积和初始值 内积和为返回值void Finner_product(){ vector<int>vec{1,2,3,4}; vector<int>vec1(vec); int result=inner_product(vec.begin(),vec.end(),vec1.begin(),0); cout<<"The inner_product result:"<<endl; cout<<result<<endl;}
//adjacent_difference 创建一个新序列,新序列中每个新值代表当前元素与上一个元素的差void Fadjacent_difference(){ vector<int>vec{1,2,3,4}; vector<int>vec1(vec.size()); adjacent_difference(vec.begin(),vec.end(),vec1.begin()); cout<<"The adjacent_difference result:"<<endl; for_each(vec1.begin(),vec1.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
/***************************************6.生成和异变算法*************************************************///将输入值赋给标志范围内的所有元素void Ffill(){ vector<int>vec(10); fill(vec.begin(),vec.end(),996); cout<<"The fill result:"<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
//fill_n将输入值赋给first到first+n范围内的所有元素。void Ffill_n(){ vector<int>vec(10); fill_n(vec.begin(),5,996); cout<<"The fill_n result:"<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
//for_each 用指定函数依次对指定范围内所有元素进行迭代访问,返回所指定的函数类型。该函数不得修改序列中的元素。void Ffor_each(){ vector<string>vec(10); fill(vec.begin(),vec.end(),"996"); cout<<"The for_each result:"<<endl; for_each(vec.begin(),vec.end(),[](string i){cout<<i<<" ";}); cout<<endl;}
/***************************************7.关系算法*************************************************///equal 判断两个序列是否相同void Fequal(){ vector<int>vec1(10); fill(vec1.begin(),vec1.end(),1024); vector<int>vec2(vec1); bool result=equal(vec1.begin(),vec1.end(),vec2.begin()); cout<<"The equal result:"<<endl; cout<<"first:"<<result<<endl; vec1.push_back(999); result=equal(vec1.begin(),vec1.end(),vec2.begin()); cout<<"second:"<<result<<endl;}
//includes判断第2个指定范围内的所有元素是否都被第1个范围包含,成功返回truevoid Fincludes(){ vector<int>vec1{1,2,3,4,5,6,7}; vector<int>vec2{1,2,3,4,5}; bool result=includes(vec1.begin(),vec1.end(),vec2.begin(),vec2.end()); cout<<"The includes result:"<<endl; cout<<"first:"<<result<<endl; vec2.push_back(8); result=includes(vec1.begin(),vec1.end(),vec2.begin(),vec2.end()); cout<<"second:"<<result<<endl;}
//max minvoid maxmin(){ int a=1; int b=2; int result=max(a,b); cout<<"The max&&min result:"<<endl; cout<<"The max:"<<result<<endl; result=min(a,b); cout<<"The min:"<<result<<endl;}
//max_element min_elementvoid Fmaxandmin_element(){ vector<int>vec{995,1024,1,1,1,8,56,74,91,100,8,996,1024,101,102,103,996}; vector<int>::iterator result; result=max_element(vec.begin(),vec.end()); cout<<"The max_element&&min_element result:"<<endl; cout<<"The max:"<<*result<<endl; result=min_element(vec.begin(),vec.end()); cout<<"The min:"<<*result<<endl;}
//mismatch 并行比较两个序列,指出第一个不匹配的位置,返回一对iterator,标志第一个不匹配元素位置。如果都匹配,返回每个容器的last。void Fmismatch(){ vector<int>vec1{1,2,3,4,5,6,7}; vector<int>vec2{1,2,3,4,5,8}; pair<vector<int>::iterator,vector<int>::iterator>result; result=mismatch(vec1.begin(),vec1.end(),vec2.begin()); cout<<"The mismatch result:"<<endl; cout<<"first:"<<*result.first<<endl; cout<<"second:"<<*result.second<<endl;}
/***************************************8.集合算法*************************************************///set_union构造一个有序序列,包含两个序列中所有的不重复元素 并集//set_intersection构造一个有序序列,其中元素在两个序列中都存在 全集(可重复出现)//set_difference构造一个有序序列,该序列仅保留第一个序列中存在的而第二个中不存在的元素 A减去交集//set_symmetric_difference构造一个有序序列,该序列取两个序列的对称差集(并集-交集)void Fset_unionset_intersectionset_differenceset_symmetric_difference(){ vector<int>vec1{1,2,3,4,5,6,7}; vector<int>vec2{1,2,3,4,5,8}; vector<int>vec; set_union(vec1.begin(),vec1.end(),vec2.begin(),vec2.end(),inserter(vec,vec.begin())); cout<<"The set_union result:"<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl; vec.resize(0); set_intersection(vec1.begin(),vec1.end(),vec2.begin(),vec2.end(),inserter(vec,vec.begin())); cout<<"The set_intersection result:"<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl; vec.resize(0); set_difference(vec1.begin(),vec1.end(),vec2.begin(),vec2.end(),inserter(vec,vec.begin())); cout<<"The set_difference result:"<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl; vec.resize(0); set_symmetric_difference(vec1.begin(),vec1.end(),vec2.begin(),vec2.end(),inserter(vec,vec.begin())); cout<<"The set_symmetric_difference result:"<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
/***************************************9.堆算法*************************************************//**make_heap: 根据指定的迭代器区间以及一个可选的比较函数,来创建一个heap. O(N) * push_heap: 把指定区间的最后一个元素插入到heap中. O(logN) * pop_heap: 弹出heap顶元素, 将其放置于区间末尾. O(logN) * sort_heap:堆排序算法,通常通过反复调用pop_heap来实现. N*O(logN) */void Fheap(){ vector<int>vec{6, 1, 2, 5, 3, 4}; cout<<"before make_heap result:"<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl; make_heap(vec.begin(),vec.end()); cout<<"After make_heap result:"<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl; vec.push_back(999); push_heap(vec.begin(),vec.end()); cout<<"After push_heap result:"<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl; pop_heap(vec.begin(),vec.end()); cout<<"After pop_heap result:"<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl; sort_heap(vec.begin(),vec.end()); cout<<"After sort_heap result:"<<endl; for_each(vec.begin(),vec.end(),[](int i){cout<<i<<" ";}); cout<<endl;}int main(){ vector<int>vec; for(int i=0;i<SIZE;i++){ vec.push_back(i); } //1.查找算法(13个):判断容器中是否包含某个值 Fadjacent_find(vec); Fbinary_search(vec,1); Fcount(vec,1); Fcount_if(vec); Fequal_range(vec,996); Ffind(vec,996); Ffind_end(); Ffind_first_of(); Ffind_if(vec); Flower_bound(vec,996); Fupper_bound(vec,996); Fsearch(); Fsearch_n();
//2.排序和通用算法(14个):提供元素排序策略 Finplace_merge(); Fmerge(); Fnth_element(); Fpartial_sort(); Fpartial_sort_copy(); Fpartition(); Fstable_partition(); Frandom_shuffle(); Freverse(); Freverse_copy(); Frotate(); Frotate_copy(); Fsort(); Fstable_sort();
//3.删除和替换算法(15个) Fcopy(); Fcopy_backward(); Fswap(); Fiter_swap(); Fswap_ranges(); Fremove(); Fremove_copy(); Fremove_if(); Fremove_copy_if(); Freplace(); Funique();
//4.排列组合算法(2个) Fnext_permutation(); Fprev_permutation();
//5.算术算法(4个) Faccumulate(); Fpartial_sum(); Finner_product(); Fadjacent_difference();
//6.生成和异变算法(6个)generate generate_n transform暂不实现 Ffill(); Ffill_n(); Ffor_each();
//7.关系算法(8个)lexicographical_compare暂不实现 Fequal(); Fincludes(); maxmin(); Fmaxandmin_element(); Fmismatch();
//8.集合算法 Fset_unionset_intersectionset_differenceset_symmetric_difference();//四合一
//9.堆算法 Fheap(); getchar(); return 0;}

运行结果如下:

The adjacent_find result:999The binary_search result:1The count result:1The count_if result:999The equal_range result:996 997The find result:0x3a920aacaabbcddefacss is: aacaabbcddefacss is: acaabbcddefacss is: caabbcddefacss is: aabbcddefacss is: abbcddefacss is: bbcddefacss is: bcddefacss is: cddefacss is: ddefacss is: defacss is: efacss is: facss is: acss is: css is: ss is: sThe find_end result:acss is: aThe find_first_of result:aThe find_if result:996The lower_bound result:996The upper_bound result:997The search result:cvec[0].address:0x31870 the num is:1vec[1].address:0x31874 the num is:996vec[2].address:0x31878 the num is:996vec[3].address:0x3187c the num is:996vec[4].address:0x31880 the num is:5vec[5].address:0x31884 the num is:5vec[6].address:0x31888 the num is:996vec[7].address:0x3188c the num is:996vec[8].address:0x31890 the num is:6vec[9].address:0x31894 the num is:6vec[10].address:0x31898 the num is:6vec[11].address:0x3189c the num is:7vec[12].address:0x318a0 the num is:8vec[13].address:0x318a4 the num is:8vec[14].address:0x318a8 the num is:9vec[15].address:0x318ac the num is:10The first search_n result:0x31874 996The second search_n result:0x31874 996before inplace_merge,vi=1 5 8 19 200 1 8 56 74 91 100 101 102 103after inplace_merge,vi=1 1 5 8 8 19 56 74 91 100 101 102 103 200The merge result:1 1 5 8 8 19 56 74 91 100 101 102 103 200 996The nth_element result:103 102 1 8 56 74 91 100 101 995 996 1024The partial_sort result:1024 996 995 103 102 1 8 56 74 91 100 101The partial_sort_copy result:1 8 56 74 91 100 101 102 103 995 996 1024The partition result:996 1024 102 8 56 74 100 91 101 1 103 995The stable_partition result:1024 8 56 74 100 102 996 995 1 91 101 103The random_shuffle result:103 1024 102 1 995 996 100 8 56 91 101 74The reverse result:996 103 102 101 100 91 74 56 8 1 1024 995The reverse_copy result:996 103 102 101 100 91 74 56 8 1 1024 995The rotate result:74 91 100 101 102 103 996 995 1024 1 8 56The rotate_copy result:74 91 100 101 102 103 996 995 1024 1 8 56The sort result:1024 996 995 103 102 101 100 91 74 56 8 1The stable_sort result:1024 996 995 103 102 101 100 91 74 56 8 1The copy result:995 1024 1 8 56 74 91 100 101 102 103 996The copy_backward result:995 1024 1 8 56 74 91 100 101 102 103 996A.name=B A.score=2B.name=A B.score=1The iter_swap result:996 1024 1 8 56 74 91 100 101 102 103 995The swap_ranges result:91 100 101 102 103 996 995 1024 1 8 56 74The remove result:before remove:The vec.size():12995 1024 1 8 56 74 91 100 101 102 103 996after remove:The vec.size():12995 1024 8 56 74 91 100 101 102 103 996 996The remove_copy result:995 1 8 56 74 91 100 101 102 103 996 0The remove_if result:1024 8 56 74 100 102 996 100 101 102 103 996The remove_copy_if result:1024 8 56 74 100 102 996 0 0 0 0 0before replace:You are beautiful!after replace:$ou are beautiful!before unique:1 1 1 8 8 56 74 91 100 101 102 103 995 996 996 1024 1024after unique:1 8 56 74 91 100 101 102 103 995 996 1024 995 996 996 1024 1024The next_permutation result:1 2 3 41 2 4 31 3 2 41 3 4 21 4 2 31 4 3 22 1 3 42 1 4 32 3 1 42 3 4 12 4 1 32 4 3 13 1 2 43 1 4 23 2 1 43 2 4 13 4 1 23 4 2 14 1 2 34 1 3 24 2 1 34 2 3 14 3 1 24 3 2 1The prev_permutation result:4 3 2 14 3 1 24 2 3 14 2 1 34 1 3 24 1 2 33 4 2 13 4 1 23 2 4 13 2 1 43 1 4 23 1 2 42 4 3 12 4 1 32 3 4 12 3 1 42 1 4 32 1 3 41 4 3 21 4 2 31 3 4 21 3 2 41 2 4 31 2 3 4The accumulate result:From 1 to 4:10*****Youarebeautiful!The partial_sum result:1 3 6 10The inner_product result:30The adjacent_difference result:1 1 1 1The fill result:996 996 996 996 996 996 996 996 996 996The fill_n result:996 996 996 996 996 0 0 0 0 0The for_each result:996 996 996 996 996 996 996 996 996 996The equal result:first:1second:0The includes result:first:1second:0The max&&min result:The max:2The min:1The max_element&&min_element result:The max:1024The min:1The mismatch result:first:6second:8The set_union result:1 2 3 4 5 6 7 8The set_intersection result:1 2 3 4 5The set_difference result:6 7The set_symmetric_difference result:6 7 8before make_heap result:6 1 2 5 3 4After make_heap result:6 5 4 1 3 2After push_heap result:999 5 6 1 3 2 4After pop_heap result:6 5 4 1 3 2 999After sort_heap result:1 2 3 4 5 999 6


C++中STL学习笔记——常见算法操作演示
C++中STL学习笔记——常见算法操作演示

祝快乐学习,快乐生活!

C++中STL学习笔记——常见算法操作演示


C-J1586886


“在看”我吗?


以上是关于C++ STL for_each 的用法?的主要内容,如果未能解决你的问题,请参考以下文章

STL常用算法

for_each用法

C++中STL常用算法

find_if(),plus,for_each()的用法

STL vector用法介绍

STL经常使用遍历算法for_each和transform的比較