vector 移除与某值想得的所有元素
2014-09-16 18:34
190 查看
#include "stdafx.h" #include <iostream> #include <vector> #include <algorithm> using namespace std; typedef vector<int> vecInt; typedef vector<int>::iterator vecIterator; void print(vecInt& vInt) { vecIterator itBeg = vInt.begin(); vecIterator itEnd = vInt.end(); for (NULL; itBeg != itEnd; ++itBeg) { cout << *itBeg << " "; } cout << endl; } // 删除容器的指定值 template<class T> bool DelSpecValue(std::vector<T>& coll, T val) { // 注意算法 remove 只是移除元素,但未缩减容器的大小,所以得调用 coll.erase coll.erase(remove(coll.begin(), coll.end(), val), coll.end()); return true; } // 删除容器第一个找到的值 template<class T> bool DelFindFirstValue(std::vector<T>& coll, T val) { std::vector<T>::iterator pos = find(coll.begin(), coll.end(), val); if (pos != coll.end()) { coll.erase(pos); } return true; } int _tmain() { vecInt vInt; for (int i = 0; i < 10; ++i) { vInt.push_back(i); vInt.push_back(i); } print(vInt); cout << endl << " ============================ " << endl; DelSpecValue(vInt, 2); DelFindFirstValue(vInt, 5); print(vInt); return 0; }
相关文章推荐
- 练习2-6:修改Fillvector.cpp使它能把vector中的所有元素连接成单独的一个字符串,并打印,但不要加上行号。
- detach() 方法移除被选元素,包括所有文本和子节点
- Swift - 移除页面视图上的所有元素
- 用下标求vector<double>中所有元素的和
- 移除数组 arr 中的所有值与 item 相等的元素,直接在给定的 arr 数组上进行操作,并将结果返回
- 1.2.3 List.clear方法——从列表中移除所有元素
- 简单实现遍历vector 删除所有相同的元素
- swift - 移除界面上的所有元素
- 移除数组 arr 中的所有值与 item 相等的元素。不要直接修改数组 arr,结果返回新的数组
- vector容器中所有元素求和-accmulate
- 标准库容器怎样移除与某一值相等的所有元素
- 移除vector中重复的元素
- 删除 vector / list 中所有指针元素
- ObsoleteAttribute 可适用于除程序集、模块、参数或返回值以外的所有程序元素。 将元素标记为过时可以通知用户:该元素在产品的未来版本中将被移除。
- 动态创建一个与该 vector 对象大小一致的数组,把 vector 对象的所有元素复制给新数组_初学vector对象4
- //编写程序求 vector<double> 对象中所有元素之和
- 3 移除数组arr中所有与item相等的元素,不更改原数组。
- 编写程序将一个list容器的所有元素赋值给一个vector容器。
- js移除元素所有click绑定事件
- 编写程序从标准输入设备读入的元素数据建立一个int型vector对象,然后动态创建与该vector大小一致的数组,把vector对象的所有元素复制给该数组