C++中vector容器的經常使用操作辦法實例總結。本站提示廣大學習愛好者:(C++中vector容器的經常使用操作辦法實例總結)文章只能為提供參考,不一定能成為您想要的結果。以下是C++中vector容器的經常使用操作辦法實例總結正文
1 取得容器最初一個元素
------ 應用 back或rbegin 獲得
// back、rbegin 有常量和援用兩種情勢 std::vector<int> myVector; myVector.back()=3; std::vector<int>::reverse_iterator tailIter; tailIter=myVector.rbegin(); *tailIter=3
2 刪除某元素
須要刪除某地位的元素,應應用iterator遍歷, 不該應用at(i) 方法遍歷,由於刪除元素時,是依據iterator地位停止刪除的。
刪除元素時,前往值為:位於刪除的元素以後的第一個的元素地點的地位
#include <vector> #include <iostream> int main( ) { using namespace std; vector <int> v1; vector <int>::iterator Iter; v1.push_back( 10 ); v1.push_back( 20 ); v1.push_back( 30 ); v1.push_back( 40 ); v1.push_back( 50 ); cout << "v1 =" ; for ( Iter = v1.begin( ) ; Iter != v1.end( ) ; Iter++ ) cout << " " << *Iter; cout << endl; v1.erase( v1.begin( ) ); cout << "v1 ="; for ( Iter = v1.begin( ) ; Iter != v1.end( ) ; Iter++ ) cout << " " << *Iter; cout << endl; v1.erase( v1.begin( ) + 1, v1.begin( ) + 3 ); cout << "v1 ="; for ( Iter = v1.begin( ) ; Iter != v1.end( ) ; Iter++ ) cout << " " << *Iter; cout << endl; }
輸入:
v1 = 10 20 30 40 50 v1 = 20 30 40 50 v1 = 20 50
3 兩vector 輕易賦值時,不克不及經由過程=來賦值
而應應用遍歷 或assign函數的方法來賦值
//delNode.vectorNode 是與delPositionVector同類型容器 // vector 兩容器不克不及直接賦值 可經由過程 遍歷每一個元素賦值,也可以使用 assign賦值 VectorNode delNode; delNode.numberOfFenkuai=nSelect; Node nodeTemp;
毛病賦值方法:
// 這是毛病的賦值方法 delNode.vectorNode=delPositionVector ;
准確賦值方法一: 遍歷
for (int i=0;i<delPositionVector.size();i++) { nodeTemp=delPositionVector.at(i); delNode.vectorNode.push_back(nodeTemp); }
准確賦值方法二: assign函數
delNode.vectorNode.assign(delPositionVector.begin(),delPositionVector.end());
4 在指定的iterator地位 拔出容器元素
拔出某元素時,前往值為拔出的誰人元素地點的地位,本來處於此地位的元素將被按序後移
iterator insert( iterator _Where, const Type& _Val ); void insert( iterator _Where, size_type _Count, const Type& _Val ); template<class InputIterator> void insert( iterator _Where, InputIterator _First, InputIterator _Last );
5 更新容器中的某個元素
方法之一: 先搜刮到這個元素地位, 在此地位添加更新的元素,刪除本來的元素
或搜刮到此元素,刪除此元素,在此元素地位上添加新元素
運用例子:
delNode.vectorNode.assign(delPositionVector.begin(),delPositionVector.end()); BOOL bInsert=FALSE; std::vector <VectorNode>::iterator iter; for (iter=g_DelVector.begin();iter!=g_DelVector.end();iter++) { if ((*iter).numberOfFenkuai==nSelect) { bInsert=TRUE; //g_DelVector.erase(iter); //g_DelVector.insert(iter,delNode); iter=g_DelVector.insert(iter,delNode); iter=g_DelVector.erase(iter+1); iter--; break; } } if (!bInsert) { g_DelVector.push_back(delNode); }
6 push_back或pop某元素後,迭代器會掉效 須要從新取得
STL中的vector不是聯系關系性容器,當拔出新的元素後,本來獲得的迭代器就會掉效。
std::vector<int> vNum; vNum.push_back(1); vNum.push_back(3); vNum.push_back(5); std::vector<int>::iterator pIt = vNum.begin(); std::cout << "Before insert a new number: " << *pIt << std::endl; vNum.push_back(7); std::cout << "After insert a new number: " << *pIt << std::endl; // Oh! No!
留意最初一句,運轉到最初一句時就會產生瓦解,迭代器拜訪毛病。拔出元素後,要從新獲得迭代器。
而關於map如許的聯系關系性容器,拔出新元素後,本來的迭代器依然會有用。
例子以下:
std::map<int, int> mNum; mNum[0] = 0; mNum[1] = 1; mNum[2] = 2; std::map<int, int>::iterator pIt = mNum.begin(); std::cout << "Before insert a new number: (" << pIt->first << ", " << pIt->second << ")" << std::endl; mNum[3] = 3; std::cout << "After insert a new number: (" << pIt->first << ", " << pIt->second << ")" << std::endl; // OK!
7 歸並兩個次序容器
std::vector<line>::iterator i1 = v1.begin(), i2 = v2.begin(); while(i1 != v1.end() && i2 != v2.end()) { if(i1->index == i2->index) { line t = { i1->index, i1->value1, i2->value2 } v3.push_back(t); ++i1; ++i2; } else if(i1->index > i2->index) { i2->value1 = 0; v3.push_back(*i2); ++i2; } else { i1->value2 = 0; v3.push_back(*i1); ++i1; } } while(i1 != v1.end()) v3.push_back(*(i1++)); while(i2 != v2.end()) v3.push_back(*(i2++));
9 排序
// alg_sort.cpp // compile with: /EHsc #include <vector> #include <algorithm> #include <functional> // For greater<int>( ) #include <iostream> // Return whether first element is greater than the second bool UDgreater ( int elem1, int elem2 ) { return elem1 > elem2; } int main( ) { using namespace std; vector <int> v1; vector <int>::iterator Iter1; int i; for ( i = 0 ; i <= 5 ; i++ ) { v1.push_back( 2 * i ); } int ii; for ( ii = 0 ; ii <= 5 ; ii++ ) { v1.push_back( 2 * ii + 1 ); } cout << "Original vector v1 = ( " ; for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout << *Iter1 << " "; cout << ")" << endl; sort( v1.begin( ), v1.end( ) ); cout << "Sorted vector v1 = ( " ; for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout << *Iter1 << " "; cout << ")" << endl; // To sort in descending order. specify binary predicate sort( v1.begin( ), v1.end( ), greater<int>( ) ); cout << "Resorted (greater) vector v1 = ( " ; for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout << *Iter1 << " "; cout << ")" << endl; // A user-defined (UD) binary predicate can also be used sort( v1.begin( ), v1.end( ), UDgreater ); cout << "Resorted (UDgreater) vector v1 = ( " ; for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout << *Iter1 << " "; cout << ")" << endl; }
Original vector v1 = ( 0 2 4 6 8 10 1 3 5 7 9 11 ) Sorted vector v1 = ( 0 1 2 3 4 5 6 7 8 9 10 11 ) Resorted (greater) vector v1 = ( 11 10 9 8 7 6 5 4 3 2 1 0 ) Resorted (UDgreater) vector v1 = ( 11 10 9 8 7 6 5 4 3 2 1 0 )
10 清空一切元素
m_itemVector.clear();
11 遍歷
vector<ITEM_CHECK>::iterator iter=m_itemVector.begin(); for(i=0;iter!=m_itemVector.end();iter++,i++) { if(iter->flag==-1) { break; } iter->flag=1; } vector<ITEM_CHECK>::iterator iter=m_itemVector.begin(); for(i=0;iter!=m_itemVector.end();iter++,i++) //先全體撤消 { iter->flag=0; }
12 刪除相符前提的項
int CurrentCount=(int)m_itemVector.size(); for(int i=0;i<CurrentCount;i++) { if(m_itemVector.at(i).flag==1) { m_itemVector.erase(m_itemVector.begin()+i); DeleteItem(i); this->Invalidate(); CurrentCount--; i--; //刪除第i地位後,須要從新斷定第i地位能否相符前提,是以須要i--。 } }
13 正序遍歷 然後反序遍歷
vector<ITEM_CHECK>::iterator iter=m_itemVector.begin(); for(i=0;iter!=m_itemVector.end();iter++,i++) //先全體撤消 { iter->flag=0; } for (;i>0;) //從後往前設置 iter 退到Begin() 再減的話 就會湧現成績 { iter--; //停止時,正好退到Begin() i--; iter->flag=1; }
14 在VECTOR中查找
#include<vector> #include<algorithm> //在VECTOR中查找 using namespace std; vector<int> L; L.push_back( 1 ); L.push_back( 2 ); L.push_back( 3 ); L.push_back( 4 ); L.push_back( 5 ); vector<int>::iterator result = find( L.begin( ), L.end( ), 3 ); //查找3 if ( result == L.end( ) ) //沒找到 cout << "No" << endl; else //找到 cout << "Yes" << endl;