public member function
<vector>
std::vector::pop_back
Delete last element
Removes the last element in thevector, effectively reducing the containersizeby
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 thevectorafter they are added up in the sum. Output:
The elements of myvector add up to 600
|
Complexity
Constant.
Iterator validity
Theend iteratorand 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 notempty, the function never throws exceptions (no-throw guarantee).
Otherwise, it causesundefined 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
分享到:
相关推荐
c++ STL source code, hash and vector etc
用于三维光学形貌扫描完成后,生成的stl文件的读取,并形成俯视投影云图
标准模板库中map、vector、以及sort等的用法讲解
C++读取STL文件,输出所有三角形的顶点坐标
stlshow_stl分层_STL分层_stlmatlab_STL切片_stl分层.zip
C++中标准模板库中vector容器的基本概念与基本操作
stlshow_stl分层_STL分层_stlmatlab_STL切片_stl分层_源码.rar.rar
STL课件_C++_课件stl_c++课件_源码.zip
09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板...
STL学习教程,
STL 最新源代码
STL vector 知识详解 STL vector 知识详解 STL vector 知识详解 STL vector 知识详解 STL vector 知识详解
C++ STL 参考手册Cpp_STL_ReferenceManual ,对学习STL 有帮助
stl部分题答案 有以下数字:11,12,13,2,3,4,5,6,7,16,17,18.将以上数据依次压入容器deque中,选用一种排序算法排序后,连续弹出3个数据(pop_front)后,对剩余数据进行累加并输出;而后删除所有大于11的数,并输出
vc显示stl,读取顶点坐标,并显示在独立窗口中
Depue_Vector_Compare.rar
SGI STL是由SGI公司开发的STL库.这份STL可以用在VC上.
在MATLAB中完成STL数据的渲染,用PATCH函数完成加颜色和亮度等功能
最新的STL源码,最新的STL源码,最新的STL源码
stl的入门ppt 很好用哦,分为导入 STL的概念与组成 Iterator(迭代器) Container(容器) Algorithm(算法) Adaptors(配接器)