vector插入/删除元素
插入操作:
理論知識
2?vector.insert(pos,elem); ??//在pos位置插入一個elem元素的拷貝,返回新數據的位置。
2?vector.insert(pos,n,elem); ??//在pos位置插入n個elem數據,無返回值。
2?vector.insert(pos,beg,end); ??//在pos位置插入[beg,end)區間的數據,無返回值?
簡單案例
vector<int> vecA;
vector<int> vecB;
?
vecA.push_back(1);
vecA.push_back(3);
vecA.push_back(5);
vecA.push_back(7);
vecA.push_back(9);
?
vecB.push_back(2);
vecB.push_back(4);
vecB.push_back(6);
vecB.push_back(8);
?
vecA.insert(vecA.begin(), 11); //{11, 1, 3, 5, 7, 9}
vecA.insert(vecA.begin()+1,2,33); //{11,33,33,1,3,5,7,9}
vecA.insert(vecA.begin() , vecB.begin() , vecB.end() ); //{2,4,6,8,11,33,33,1,3,5,7,9}
測試代碼:
?
[cpp]?view plain?copy
?
?
vector的刪除
理論知識
2?vector.clear(); //移除容器的所有數據
2?vec.erase(beg,end); ?//刪除[beg,end)區間的數據,返回下一個數據的位置。
2?vec.erase(pos); ???//刪除pos位置的數據,返回下一個數據的位置。
簡單案例:
刪除區間內的元素
vecInt是用vector<int>聲明的容器,現已包含按順序的1,3,5,6,9元素。
vector<int>::iterator itBegin=vecInt.begin()+1;
vector<int>::iterator itEnd=vecInt.begin()+2;
vecInt.erase(itBegin,itEnd);
//此時容器vecInt包含按順序的1,6,9三個元素。
?
假設?vecInt?包含1,3,2,3,3,3,4,3,5,3,刪除容器中等于3的元素
for(vector<int>::iterator it=vecInt.being(); it!=vecInt.end(); ) ???//小括號里不需寫 ?++it
{
???if(*it == 3)
???{
????????it ?= ?vecInt.erase(it); ??????//以迭代器為參數,刪除元素3,并把數據刪除后的下一個元素位置返回給迭代器。
?????????//此時,不執行 ?++it; ?
???}
???else
???{
???????++it;
???}
}
?
//刪除vecInt的所有元素
vecInt.clear(); //容器為空
?
測試代碼:
?
[cpp]?view plain?copy
?
以下是擴展部分:
?
實戰c++中的vector系列--C++11對vector成員函數的擴展(cbegin()、cend()、crbegin()、crend()、emplace()、data())
一蓑煙雨1989 2015-12-19 18:07:00 ?6934 ?收藏 3
分類專欄: C++ 實戰c++中的vector系列
版權
前面提到的emplace_back就是C++11所增加的內容。
所以這篇博客就是想羅列一下C++11對vector容器的擴充。
std::vector::cbegin和std::vector::cend
這兩個方法是與std::vector::begin和std::vector::end相對應的,從字面就能看出來,多了一個’c’,顧名思義就是const的意思。
所以:
std::vector::cbegin: Returns a const_iterator pointing to the first element in the container.
std::vector::cend: Returns a const_iterator pointing to the past-the-end element in the container.
?
std::vector::crbegin和std::vector::crend
這兩個方法就不解釋了,與上面的相比就是多了個’r’, reverse的縮寫,反轉迭代器,代碼就省略了。
std::vector::emplace
之前已經對emplace_back進行了討論,其實還有一個方法叫emplace。
我想說的就是,emplace之于emplace_back就像insert之于push_back。
看英文描述就直觀:
emplace:Construct and insert element
emplace_back:Construct and insert element at the end
如何使用:
#include <iostream> #include <vector>int main () {std::vector<int> myvector = {10,20,30};auto it = myvector.emplace ( myvector.begin()+1, 100 );myvector.emplace ( it, 200 );myvector.emplace ( myvector.end(), 300 );std::cout << "myvector contains:";for (auto& x: myvector)std::cout << ' ' << x;std::cout << '\n';return 0; }
Output:
myvector contains: 10 200 100 20 30 300
std::vector::data
Returns a direct pointer to the memory array used internally by the vector to store its owned elements.
std::vector::shrink_to_fit
Requests the container to reduce its capacity to fit its size.
就是減少空間
?
#include <iostream> #include <string> #include <vector> using namespace std;struct Foo{ public:Foo () = default;Foo(int n,double x):id(n),price(x){}void print()const{cout << "id is:" << id;cout << ";price is:" << price << ".";} private:int id = 110;double price = 1.123; }; typedef vector<Foo> VECTORFOO; void print(const VECTORFOO &); int main() {VECTORFOO v;v.emplace(v.begin(),42,3.1416);v.emplace(v.begin(),Foo(43,3.1417));// v.insert(v.begin(),{44,3.1418});v.emplace_back(Foo(44,3.1418));v.emplace_back(Foo(45,3.1419));v.emplace(v.end(),Foo(46,3.1420));print(v);return 0; }void print(const VECTORFOO & vec) { int i = 1;for(VECTORFOO::const_iterator it = vec.begin() ; it != vec.end(); ++it){cout << "Elem " << i <<" is:";(*it).print();cout << endl;++i;}}r@r-Sys:~/9/delete_and_add_element$ ./123 Elem 1 is:id is:43;price is:3.1417. Elem 2 is:id is:42;price is:3.1416. Elem 3 is:id is:44;price is:3.1418. Elem 4 is:id is:45;price is:3.1419. Elem 5 is:id is:46;price is:3.142.
?
總結
以上是生活随笔為你收集整理的vector插入/删除元素的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ list用法总结
- 下一篇: deque插入/删除元素