1.vector::pop_back()
删除vector的最后一个元素,vector的大小减一,删了的元素被销毁。
2.vector::erase()
iterator erase (iterator position); iterator erase (iterator first, iterator last);//这个删除的范围是左闭右开 //C++11要求形参是const迭代器 iterator erase (const_iterator position); iterator erase (const_iterator first, const_iterator last);//这个删除的范围是左闭右开
// example// erasing from vector #include <iostream> #include <vector> int main () { std::vector<int> myvector; // set some values (from 1 to 10) for (int i=1; i<=10; i++) myvector.push_back(i); // erase the 6th element myvector.erase (myvector.begin()+5); // erase the first 3 elements: myvector.erase (myvector.begin(),myvector.begin()+3); std::cout << "myvector contains:"; for (unsigned i=0; i<myvector.size(); ++i) std::cout << ‘ ‘ << myvector[i]; std::cout << ‘\n‘; return 0; }
时间: 2024-10-03 21:00:39