标签:clear tor _each ack 创建 std push cout for_each
1、创建:
二维数组
2、使用:
3、操作
#include<iostream> #include<vector> #include<string> #include<algorithm> using namespace std; struct Play{ Play(string t) : str(t){} bool operator()(string s){ // str += s; cout << str << s << endl; } string str; }; //第一种 void print(string &s){ cout << s << endl; } //第二种 vector<string>v{"0","123","456","789","结束"}; int main(){ for_each(v.begin(),v.end(),Play("name=")); for_each(v.begin(),v.end(),print); return 0; }
#include<iostream> #include<vector> #include<algorithm> #include<cstring> using namespace std; struct student{ int grade; char name[10]; bool operator < (const student &s) const{ if(grade != s.grade)return grade < s.grade; else return strcmp(name,s.name) < 0; } //第一种,重载小于号 }; bool cmp(student a, student b){ if(a.grade != b.grade)return a.grade < b.grade; else return strcmp(a.name,b.name) < 0; } //第二种,另外写函数 int main(){ vector<int>v; vector<student>u; sort(v.begin(),v.end()); //从小到大(默认) sort(v.begin(),v.end(),less<int>() ) //从小到大 sort(v.begin(),v.end(),greater<int>() ) //从大到小 sort(u.begin(),u.end()); //第一种 sort(u.begin(),u.end(),cmp); //第二种 }
查找
#include<iostream> #include<vector> #include<string> #include<algorithm> using namespace std; struct student{ int age; string name; }; struct Play{ Play(int t) : age(t){} bool operator()(student &he){ return he.age > age; } int age; }; //第一种 bool cmp(student &s){ return s.age > 18; } //第二种 int main(){ vector<student>v(4); v[1] = {1,"Tom"}; v[2] = {12,"Mark"}; v[3] = {19,"Rose"}; auto it = find_if(v.begin()+1,v.end(),Play(18)); auto it = find_if(v.begin()+1,v.end(),cmp); cout << it->name; return 0; }
标签:clear tor _each ack 创建 std push cout for_each
原文地址:https://www.cnblogs.com/Cmathe/p/12256830.html