pop_back函数
语法:
1 |
void pop_back(); |
pop_back() 移除最后一个元素
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/** * @brief Removes last element. * * This is a typical stack operation. It shrinks the %vector by one. * * Note that no data is returned, and if the last element's * data is needed, it should be retrieved before pop_back() is * called. */ void pop_back() { --this->_M_impl._M_finish; std::_Destroy(this->_M_impl._M_finish); } |
测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// 梁笔记 // https://zouzhongliang.com #include <iostream> #include <vector> using namespace std; int main() { vector<int> v1; vector<int> v2(10, 2); v1.push_back(1); v1.push_back(2); v1.push_back(3); v1.push_back(4); v1.push_back(5); for(int i=0;i<v1.size();++i){ cout<<v1[i]<<","; } cout<<endl; v1.pop_back(); for(int i=0;i<v1.size();++i){ cout<<v1[i]<<","; } cout<<endl; } |
测试结果:
1 2 |
1,2,3,4,5, 1,2,3,4, |