STL vector中的pop_back方法(22)
2014-08-16 11:41
337 查看
原文地址:http://www.cplusplus.com/reference/vector/vector/pop_back/
public member function
<vector>
Delete last element
Removes the last element in the vector, effectively reducing the container size by
one.
可以高效地移除vector中的最后一个元素.
This destroys the removed element.
将销毁并移除该元素。
例子:
结果截图:
果然又是逗我呢,还是没有销毁数据阿。
none
none
In this example, the elements are popped out of the vector after they are added up in the sum. Output:
Constant.
The end iterator and any iterator, pointer and reference referring to the removed element are
invalidated.
通过end()获得的iterator以及其他指向被移除的元素的迭代器,指针,引用都将失效。
例子:
其他的迭代器,指针以及引用,只要不是指向被移除的那个元素,都有效。
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.
容器将被修改。
最后一个元素将被修改。访问以及修改其他元素都是安全的,但是不包括范围在被移除那个元素的例外。
If the container is not empty, the function never throws exceptions (no-throw guarantee).
Otherwise, it causes undefined behavior.
如果容器非空,不会抛出异常,否则,将会导致未知的错误。
//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
2014-8-16
于GDUT
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 |
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 areinvalidated.
通过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 vector中的emplace_back方法(24)
- 删除vector容器中的对象元素的三种方法:pop_back, erase与remove算法
- 删除vector容器中的对象元素的三种方法:pop_back, erase与remove算法
- 删除vector容器中的对象元素的三种方法:pop_back, erase与remove算法http://www.xuebuyuan.com/1407838.html
- STL vector中的push_back方法(17)
- 开启@yahoo.cn邮箱POP和SMTP的方法
- 开发工具异常之server2008中页面报错webform_postbackoptions未定义解决方法(原创)
- WebForm_PostBackOptions未定义 解决方法
- java实现的stack pop方法小细节
- 在android的状态栏(statusbar)中增加menu,home和back快捷键的方法
- 理解 Delphi 的类(十) - 深入方法[22] - 指针参数
- 数组的各种方法范例pop,push,unshift,splice,shift
- Js:数组的各种方法范例pop,push,unshift,splice,shift
- .NET button的SendToBack()方法
- WebForm_PostBackOptions未定义 的原因及解决方法
- 编程实现一个堆栈类Stack,要有push()压栈方法,pop()出栈方法,clear()方法
- 给自定义控件(Web Control)添加事件的几种方法。前两种方法可以不实现IPostBackEventHandler
- Js:数组的各种方法范例pop,push,unshift,splice,shift
- 转帖 一个很有用但不常用到的传值方法! http://topic.csdn.net/u/20070124/22/09d43606-4119-4407-bfa6-8b2890ffbac7.html
- BackroundWorker.DoWork方法中访问Word对象导致异常