您的位置:首页 > 运维架构

STL vector中的pop_back方法(22)

2014-08-16 11:41 337 查看
原文地址:http://www.cplusplus.com/reference/vector/vector/pop_back/

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;
}

Edit
& Run

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  STL STL vector