查找vector容器中指定数据
程序员文章站
2022-03-21 16:28:43
...
#include<iostream>
using namespace std;
#include<vector>
#include<string>
#include<algorithm>
class Person
{
public:
Person(string name, int age)
{
this->m_name = name;
this->m_age = age;
}
string m_name;
int m_age;
};
class Great20
{
public:
bool operator()(Person& p)
{
return p.m_age = 20;
}
};
class Great3
{
public:
bool operator()(int& val) //这里传入的数据必须要应用不然就报错;
{
return val = 3;
}
};
void test()
{
vector<Person>v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
vector<Person>::iterator it = find_if(v.begin(), v.end(), Great20()); //第三个参数是谓词;
cout << "name is : " << it->m_name << " are is: " << it->m_age << endl;
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
vector<int>::iterator dit = find_if(v1.begin(), v1.end(), Great3());
cout << *dit << endl;
}
int main()
{
test();
}
上一篇: ubuntu下Go Mod和Go Vendor命令
下一篇: Python学习笔记之线程