deque的中find自定义的类型
2013-02-23 22:25
183 查看
可以调用find()在deque中查找。
对应值类型如int类型 可以直接使用。
对应自定义的类和结构须添加bool operator == (const MyClass &other) const 成员函数或bool operator ==(const MyClass &one, const MyClass &another)全局函数。
以下是调用代码
关于内存泄露的考虑
如deque中存储的为非指针类型如上deque<MyClass>或deque<int>
如果容器中含有元素,则程序退出前须清空调用deque的clear()或~deque(),否则出现内存泄露。用VS2008调试会看到Detected memory leaks!。
这种泄露可能在程序运行时没有什么影响。
如deque中存储的是指针类型deque<MyClass*>
每个指针对应一个堆内存。
则需要使用遍历每一个元素并调用delete *iterator;来释放指针指向内存。
然后在清空容器(deque的clear()或~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())
相关文章推荐
- std::map find 自定义类型元素
- find_if和函数对象结合实现对自定义类型vector的查找
- find_if和函数对象结合实现对自定义类型vector的查找
- find_if和函数对象结合实现对自定义类型vector的查找 复杂结构体查询某个值
- set find erase 自定义排序类型
- hibernate之自定义数据类型
- core data UIColor转换为 自定义数据类型 (其他类型数据 转换同理)
- 自定义可以存放任意类型(含智能指针)的顺序栈
- JAVA_WEB Struts2学习:struts2自定义类型转换类
- Android中intent如何传递自定义数据类型
- i++ 和 ++i 效率的分析以及自定义类型的自增/自减运算符重载实例
- WordPress 修改自定义文章类型的固定链接结构
- 自定义标签 Unable to find setter method for attribute
- c#自定义类型的转换方式operator,以及implicit(隐式)和explicit (显示)声明的区别
- unity自定义inspector面板(选择不同枚举类型,inspector面板出现不同信息)
- 在自定义Pipeline组件中实现drop-down list类型的Design-Time Property
- 有趣的自定义类型:表
- Hibernate的自定义数据类型实现接口——UserType详解
- enum的秘密<一>:enum是“用户自定义类型”(有意思 不过有的验证不正确)
- OC中构造方法、自定义构造方法、id类型