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 the vector either 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 container size by the number of elements removed, which are destroyed.

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


Because vectors use an array as their underlying storage, erasing elements in positions other than the vector end causes 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 as list or forward_list).

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


Parameters

position
Iterator pointing to a single element to be removed from the vector.

Member types iterator and const_iterator are random access iterator types that point to elements.

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

类型为随机访问迭代器。

first, last
Iterators specifying a range within the vector] to be removed: [first,last). i.e., the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.

Member types iterator and const_iterator are random access iterator types 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 the container end if 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 type iterator is a random access iterator type 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 to position (or first) and beyond are invalidated, with all iterators, pointers and references to elements before position (or first) 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 before position (or first) 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 invalid position or range causes undefined 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





  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值