deque的中find自定义的类型

可以调用find()在deque中查找。

对应值类型如int类型 可以直接使用。

对应自定义的类和结构须添加bool operator == (const MyClass &other) const 成员函数或bool operator ==(const MyClass &one, const MyClass &another)全局函数。

class MyClass
{
public:
	int a;
	int b;
	//bool operator == (const MyClass &other) const
         //{
	//    return (a == other.a) && (b == other.b) ;
         //}
};
bool operator ==(const MyClass &one, const MyClass &another) 
{
    return (one.a == another.a)&& (one.b == another.b);
}


以下是调用代码

MyClass c;
c.a=2;
c.b =2;
deque<MyClass>::iterator pos;
pos = find(clsDeq.begin(), clsDeq.end(), c);
if (pos != clsDeq.end())  
    printf("clsDeq %d success\n", (*pos).a);  
else  
    printf("find failed\n");  


 

关于内存泄露的考虑

如deque中存储的为非指针类型如上deque<MyClass>或deque<int>

如果容器中含有元素,则程序退出前须清空调用deque的clear()或~deque(),否则出现内存泄露。用VS2008调试会看到Detected memory leaks!。

这种泄露可能在程序运行时没有什么影响。

 

如deque中存储的是指针类型deque<MyClass*>

每个指针对应一个堆内存。

则需要使用遍历每一个元素并调用delete *iterator;来释放指针指向内存。

然后在清空容器(deque的clear()或~deque())

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值