您的位置:首页 > 其它

STL中Vector容器Find的使用(泛型)

2013-12-19 19:54 477 查看
#include <list>
#include <algorithm>
#include <iostream>

using namespace std;

class CTest
{
public:
CTest(int x, int y) { this->x = x; this->y = y; };
~CTest() { };

bool operator == (const CTest &other);

public:
int x;
int y;
};

bool CTest::operator == (const CTest &other)
{
if(this->x == other.x && this->y == other.y)
return true;
else
return false;
}

int main()
{
list < CTest > Test;
list < CTest > :: iterator iter;

Test.push_back(CTest(10, 20));
Test.push_back(CTest(30, 40));
Test.push_back(CTest(50, 60));
iter = find(Test.begin(), Test.end(), CTest(30, 40));
if(iter != Test.end())
cout << iter->x << "\t" << iter->y << endl;
else
cout << "Can't find it!" << endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: