#include <iostream>
#include<list>
#include<vector>
#include<algorithm>
using namespace std;
struct Int {
int x;
Int(int x_=0):x(x_){}
friend ostream& operator<<(ostream& os, const Int& x) {
cout << x.x << endl;
return os;
}
bool operator==(const Int& d)const {//通过自定义的相等来判断是否查找到指定元素
return x == d.x;
}
};
int main()
{
Int L(4);
list<Int> l;
l.push_back(Int(3));
l.push_back(Int(4));
l.push_back(Int(1));
l.push_back(Int(33));
cout << *(find(l.begin(), l.end(), L));//重载了<<所以find返回对象的指针后就能直接输出
}
STL使用C++的find查找指定元素
最新推荐文章于 2024-04-19 09:00:00 发布