STL vector中的pop_back方法(22)

public member function
<vector>

std::vector::pop_back

void pop_back();
Delete last element
Removes the last element in the vector, effectively reducing the container size by one.
可以高效地移除vector中的最后一个元素.

This destroys the removed element.

将销毁并移除该元素。

例子:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc,char **argv)
{
	vector<int> vi={10,20,30};
	cout<<"vi is :"<<endl;
	for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";});
	vi.pop_back();
	cout<<endl<<"after the pop_back():"<<endl;
	for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";});
	cout<<endl<<"try to access vi[2]="<<vi[2]<<endl;
	



}


结果截图:


果然又是逗我呢,还是没有销毁数据阿。



Parameters

none

Return value

none

Example

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

int main ()
{
  std::vector<int> myvector;
  int sum (0);
  myvector.push_back (100);
  myvector.push_back (200);
  myvector.push_back (300);

  while (!myvector.empty())
  {
    sum+=myvector.back();
    myvector.pop_back();
  }

  std::cout << "The elements of myvector add up to " << sum << '\n';

  return 0;
}

In this example, the elements are popped out of the vector after they are added up in the sum. Output:
The elements of myvector add up to 600

Complexity

Constant.

Iterator validity

The end iterator and any iterator, pointer and reference referring to the removed element are invalidated.

通过end()获得的iterator以及其他指向被移除的元素的迭代器,指针,引用都将失效。

例子:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc,char **argv)
{
	vector<int> vi={10,20,30};
	cout<<"vi is :"<<endl;
	for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";});
	auto it=vi.end();
	int &ri=vi.back();
	int *p=&vi[2];
	cout<<endl<<"vi.end()="<<*it<<endl;
	cout<<"ri="<<ri<<endl;
	cout<<"*p="<<*p<<endl;
	vi.pop_back();
	cout<<endl<<"after the pop_back():"<<endl;
	for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";});
	cout<<endl<<"try to access vi[2]="<<vi[2]<<endl<<endl;
	cout<<"vi.end()="<<*it<<endl;
	cout<<"ri="<<ri<<endl;
	cout<<"*p="<<*p<<endl;
	



}


其他的迭代器,指针以及引用,只要不是指向被移除的那个元素,都有效。


Data races

The container is modified.
The last element is modified. Concurrently accessing or modifying other elements is safe, although iterating ranges that include the removed element is not.

容器将被修改。

最后一个元素将被修改。访问以及修改其他元素都是安全的,但是不包括范围在被移除那个元素的例外。

Exception safety

If the container is not empty, the function never throws exceptions (no-throw guarantee).

Otherwise, it causes undefined behavior.

如果容器非空,不会抛出异常,否则,将会导致未知的错误。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc,char **argv)
{
	vector<int> vi;
	cout<<"vi.pop_back()"<<endl;
	vi.pop_back();
	



}
在我这里似乎没有什么反应啊,难道真的是编译器问题?




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

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

2014-8-16

于GDUT



  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值