关于STL中的查找功能。 想要查找vector中的一个对象,vector的类型为一个类而非普通类型。查找条件为对象的成员变量为特定的一个值,获得对象的指针。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先占一楼,贴代码……#include <vector>#include <algorithm>#include <iostream>class example{public: example(int val) { i = val; } bool operator==(example const & rhs) { return (i == rhs.i) ? true : false; }private: int i;};using namespace std;int main(void){ vector<example> ve; ve.push_back(1); vector<example>::iterator it; example elem(1); it = find(ve.begin(), ve.end(), elem); cout<<boolalpha<<(*it == elem);}最主要的就是写operator==,按照你自己的要求改改就可以了 kugou再一次败北……我们什么时候再战抢一啊? c++如何使用CDO.message发邮件,需要什么头文件和域名! 目前用VS 6以上版本的VS IDE能做出非托管的Windows C/C++程序吗? 怎样判断文件是否sqlite数据库文件 try ... catch...finally 如何实现? Excel 操作问题 如何判定两台机器网络连接是否正常!在线等待 有关pragma 如何在客户端提取访问网站的Cookie?(???) 如何实现在wizard(向导)上, 使按钮next 和 finish 同时有效?如vc 的app wizard 求救串口通讯问题: 请教:对话框之间的消息传递? 向注册表中写id号的问题 急!急!急!
#include <algorithm>
#include <iostream>class example
{
public:
example(int val)
{
i = val;
} bool operator==(example const & rhs)
{
return (i == rhs.i) ? true : false;
}private:
int i;
};
using namespace std;
int main(void)
{
vector<example> ve;
ve.push_back(1);
vector<example>::iterator it;
example elem(1);
it = find(ve.begin(), ve.end(), elem);
cout<<boolalpha<<(*it == elem);
}最主要的就是写operator==,按照你自己的要求改改就可以了