`
阿尔萨斯
  • 浏览: 4176857 次
社区版块
存档分类
最新评论

STL vector中的erase方法(26)

 
阅读更多
public member function
<vector>

std::vector::erase

iterator erase (const_iterator position);
iterator erase (const_iterator first, const_iterator last);
Erase elements

Removes from thevectoreither a single element (position) or a range of elements ([first,last)).

从vector中移除单一(position)或者是指定范围内([first,last))的元素。

指定单一位置例子:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc,char **argv)
{
	vector<int> vi={11,22,33};
	cout<<"vector=";
	for_each(vi.begin(),vi.end(),[](int m){cout<<m<<" ";});
	cout<<endl;
	auto it=vi.begin();
	vi.erase(it);
	cout<<"after vi.erase(vi.begin()) vector=";
	for_each(vi.begin(),vi.end(),[](int m){cout<<m<<" ";});
	cout<<endl;
	






}
结果截图:


指定范围例子:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc,char **argv)
{
	vector<int> vi={11,22,33};
	cout<<"vector=";
	for_each(vi.begin(),vi.end(),[](int m){cout<<m<<" ";});
	cout<<endl;
	auto it=vi.begin();
	vi.erase(it,vi.end());
	cout<<"after vi.erase(vi.begin(),vi.end()) vector=";
	for_each(vi.begin(),vi.end(),[](int m){cout<<m<<" ";});
	cout<<endl;
	






}
结果截图:




This effectively reduces the containersizeby the number of elements removed, which are destroyed.

这个方法可以高效地删除固定数目的元素,这些元素将被销毁。


Because vectors use an array as their underlying storage, erasing elements in positions other than thevector endcauses the container to relocate all the elements after the segment erased to their new positions. This is generally an inefficient operation compared to the one performed for the same operation by other kinds of sequence containers (such aslistorforward_list).

因为vector底层的存储是采用array,擦除指定位置的元素会造成容器其他的在被擦除后面的元素要重新迁移到新的位置。这通常会导致很大的开销(相对于另一个更好的序列解决方案,例如list,forward_list)。


Parameters

position
Iterator pointing to a single element to be removed from thevector.

Member typesiteratorandconst_iteratorarerandom access iteratortypes that point to elements.

一个指向要被擦除的元素的迭代器。

类型为随机访问迭代器。

first, last
Iterators specifying a range within thevector] to be removed:[first,last). i.e., the range includes all the elements betweenfirstandlast, including the element pointed byfirstbut not the one pointed bylast.

Member typesiteratorandconst_iteratorarerandom access iteratortypes that point to elements。

指示特定范围内将被移除元素位置的迭代器。该范围包括first到last之间的所有元素,包括first指向的元素,但不包括last指向的元素。

Return value

An iterator pointing to the new location of the element that followed the last element erased by the function call. This is thecontainer endif the operation erased the last element in the sequence.

返回值为一个迭代器,其指向被擦除的最后一个元素的下一个位置的元素。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc,char **argv)
{
	vector<int> vi={11,22,33};
	cout<<"vector=";
	for_each(vi.begin(),vi.end(),[](int m){cout<<m<<" ";});
	cout<<endl;
	auto it=vi.erase(vi.begin()+1);
	cout<<"after erase(vi.begin()+1) /n return is "<<*it<<endl;

}
截图:


如果被擦除的最后一个元素是整个序列的最后一个元素,那么
返回的值为end()迭代器.


Member typeiteratoris arandom access iteratortype that points to elements.

迭代器的类型为随机访问迭代器。


Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// erasing from vector
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myvector;

  // set some values (from 1 to 10)
  for (int i=1; i<=10; i++) myvector.push_back(i);

  // erase the 6th element
  myvector.erase (myvector.begin()+5);

  // erase the first 3 elements:
  myvector.erase (myvector.begin(),myvector.begin()+3);

  std::cout << "myvector contains:";
  for (unsigned i=0; i<myvector.size(); ++i)
    std::cout << ' ' << myvector[i];
  std::cout << '\n';

  return 0;
}

Output:
myvector contains: 4 5 7 8 9 10

Complexity

Linear on the number of elements erased (destructions) plus the number of elements after the last element deleted (moving).

与被擦除元素(析构)的数目加上被擦除元素后面的元素(移动)的数目线性相关。


Iterator validity

Iterators, pointers and references pointing toposition(orfirst) and beyond are invalidated, with all iterators, pointers and references to elements beforeposition(orfirst) are guaranteed to keep referring to the same elements they were referring to before the call.

指向被擦除位置以及其后面位置的迭代器,指针以及引用都将失效,被擦除元素前面的迭代器,指针以及引用依然有效。


Data races

The container is modified.

容器将被修改。

None of the elements beforeposition(orfirst) is accessed, and concurrently accessing or modifying them is safe.

position位置之前的元素不会被访问,同时访问以及修改他们都是安全的。


Exception safety

If the removed elements include the last element in the container, no exceptions are thrown (no-throw guarantee).

如果被擦除的元素包括容器的最后一个元素,不会抛出异常。


Otherwise, the container is guaranteed to end in a valid state (basic guarantee).

An invalidpositionor range causesundefined behavior.

否则,容器只保证最后保持在有效状态。

一个无效的位置或者范围将导致未知的行为。

例子:

<span style="color:#993399;">#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc,char **argv)
{
	vector<int> vi={11,22,33};
	cout<<"vector=";
	for_each(vi.begin(),vi.end(),[](int m){cout<<m<<" ";});
	cout<<endl;
	auto it=vi.erase(vi.end()+1);
	//if(it==vi.end())
	//	cout<<"it==vi.end()"<<endl;
	cout<<"after erase(vi.end()) \n return is "<<*it<<endl;

}</span>
结果截图:



//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

2014-8-17

于GDUT





分享到:
评论

相关推荐

    stl容器set,map,vector之erase用法与返回值详细解析

    在使用 list、set 或 map遍历删除某些元素时可以这样使用,如下所示

    Effective STL(中文)

    永不建立auto_ptr的容器 条款9:在删除选项中仔细选择 条款10:注意分配器的协定和约束 条款11:理解自定义分配器的正确用法 条款12:对STL容器线程安全性的期待现实一些 vector和string 条款13:...

    effective stl stl 技巧

    条款9:在删除选项中仔细选择 条款10:注意分配器的协定和约束 条款11:理解自定义分配器的正确用法 条款12:对STL容器线程安全性的期待现实一些 vector和string 条款13:尽量使用vector和string来代替动态分配...

    effective stl 中文 pdf

    条款26: 尽量使用iterator代替const_iterator,reverse_iterator和const_reverse_iterator 条款27: 使用distance和advance把const_iterators转化成iterators 条款28: 了解如何通过reverse_iterator的base得到...

    Effictive STL CHM中文版

    》灰《《常好的STL教程Effective STL 目录 容器 条款1: 仔细选择你要的容器 条款2: 小心对“容器无关代码”的幻想 条款3: 使容器里对象的拷贝操作轻量而正确 条款4: 用empty来代替检查size是否为0 条款5: ...

    STL源码剖析.pdg

    1.9.1 stl_config.h 中的各种组态 027 组态3:static template member 027 组态5:class template partial specialization 028 组态6:function template partial order 028 组态7:explicit function template ...

    C++STL讲解 PPT版本

    STL(Standard TemplateLibrary),即标准模板库,从根本上说,STL是一些“容器”的集合,这些“容器”有list、vector、set、map等,STL也是算法和其他一些组件的集合。STL的目的是标准化组件,这样就不用重新开发,...

    关于STL的erase()陷阱-迭代器失效问题的总结

    STL中的容器按存储方式分为两类,一类是按以数组形式存储的容器(如:vector 、deque);另一类是以不连续的节点形式存储的容器(如:list、set、map)。在使用erase方法来删除元素时,需要注意一些问题。 1.list,set...

    C++_STL_示例

    C++ STL_示例word 格式 提供例程 string vector list等数据结构示例 #include #include using namespace std; void main() { //用const char * 构造strText对象 string strText("This is a test"); //在strText...

    刷leetcode不用stl-leetcode:leetcode

    erase()做的 122 买卖股票的最佳时机 利润最大化就是每次波谷买入波峰卖出 189 旋转数组 暴力法 一次挪一步 挪k次 超时 STL reverse() 217 判断是否存在重复元素 方法一 暴力法 时间复杂度 : O(n^2) 方法二 排序 ...

    STL 源码剖析(侯捷先生译著)

    1.9.1 stl_config.h 中的各种组态 027 组态3:static template member 027 组态5:class template partial specialization 028 组态6:function template partial order 028 组态7:explicit function template ...

    c++STL基本容器用法带程序详解

    vector用于存储对象数组 常用方法 1.push_back 在数组的最后添加一个数据 2.pop_back 去掉数组的最后一个数据 3.at 得到编号位置的数据 4.begin 得到数组头的指针 5.end 得到数组的最后一个单元+1的指针 6.front ...

    circular_vector:用 C++ 编写的符合 STL 的循环向量(容器)数据结构

    包含与来自 STL 的 C++98 std::vector 相同的所有成员函数,但 vector::insert 和 vector::erase 除外。 新引入的成员函数包括:push_front 和 pop_front,它们不同于 std::vector 中的对应操作是 O(1) 时间(常量...

    C++STL程序员开发指南【可搜索+可编辑】

    1-3-2 在派生类中实现类的基本函数,................... _ ............... 29 1-3-3 内联函数技术,........ ................................... 30 3133 ..... .. .. .. .. .. .. .. .. .. .. .. .. .....

    浅谈c++ stl迭代器失效的问题

    之前看《C++ Primier》的时候,也解到在顺序型窗口里insert/erase会涉及到迭代... vector&lt;int&gt; a(arr,arr+sizeof(arr)/sizeof(*arr));for (auto it = a.begin(); it != a.end();++it ){ if ((*it)&1){ a.erase(it);

    基于C++开发的射击游戏

    子弹的实现可以使用STL中的vector,当按下开火键时发出一颗子弹,就往vector中添加一个结点;当子弹飞出窗口或击中敌机时,再将结点从vector中删除。每帧游戏画面中子弹飞行时只需将vector中的所有子弹进行处理、...

    leetcode跳跃-leetcode:讨论区答案搬运工

    一、stl+math综合 预备知识:map、unordered_map、priority_queue、queue、set、stack、pair、list 1.1 LeetCode533 孤独像素 II 1.2 LeetCode060 第k个排列 vector 删除指定索引的元素 vector nums; auto it = ...

    数据结构与算法分析C描述第三版

     3.3 STL中的向量和表   3.3.1 迭代器   3.3.2 示例:对表使用erase   3.3.3 const_iterator   3.4 向量的实现   3.5 表的实现   3.6 栈ADT   3.6.1 栈模型   3.6.2 栈的实现   3.6.3 ...

    数据结构与算法分析

     3.3 STL中的向量和表   3.3.1 迭代器   3.3.2 示例:对表使用erase   3.3.3 const_iterator   3.4 向量的实现   3.5 表的实现   3.6 栈ADT   3.6.1 栈模型   3.6.2 栈的实现  ...

Global site tag (gtag.js) - Google Analytics