1 获得容器最后一个元素
------ 使用 back或rbegin 取得
1
2
3
4
5
6
7
8
9
|
// back、rbegin 有常量和引用两种形式
myVector.back()=3;
std::vector< int >::reverse_iterator tailIter;
tailIter=myVector.rbegin();
*tailIter=3
|
2 删除某元素
需要删除某位置的元素,应使用iterator遍历, 不应使用at(i) 方式遍历,因为删除元素时,是根据iterator位置进行删除的。
删除元素时,返回值为:位于删除的元素之后的第一个的元素所在的位置
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
30
31
32
|
#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;
}
|
输出:
1
2
3
|
v1 = 10 20 30 40 50
v1 = 20 30 40 50
v1 = 20 50
|
3 两vector 容易赋值时,不能通过=来赋值
而应使用遍历 或assign函数的方式来赋值
1
2
3
4
5
6
7
|
//delNode.vectorNode 是与delPositionVector同类型容器
// vector 两容器不能直接赋值 可通过 遍历每个元素赋值,也可使用 assign赋值
VectorNode delNode;
delNode.numberOfFenkuai=nSelect;
Node nodeTemp;
|
错误赋值方式:
1
2
|
// 这是错误的赋值方式
delNode.vectorNode=delPositionVector ;
|
正确赋值方式一: 遍历
1
2
3
4
5
|
for ( int i=0;i<delPositionVector.size();i++)
{
nodeTemp=delPositionVector.at(i);
delNode.vectorNode.push_back(nodeTemp);
}
|
正确赋值方式二: assign函数
1
|
delNode.vectorNode.assign(delPositionVector.begin(),delPositionVector.end());
|
4 在指定的iterator位置 插入容器元素
插入某元素时,返回值为插入的那个元素所在的位置,原先处于此位置的元素将被顺次后移
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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 更新容器中的某个元素
办法之一: 先搜索到这个元素位置, 在此位置添加更新的元素,删除原先的元素
或搜索到此元素,删除此元素,在此元素位置上添加新元素
应用例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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不是关联性容器,当插入新的元素后,原来取得的迭代器就会失效。
1
2
3
4
5
6
7
8
|
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这样的关联性容器,插入新元素后,原来的迭代器仍然会有效。
例子如下:
1
2
3
4
5
6
7
8
|
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 合并两个顺序容器
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
|
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 排序
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
// 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;
}
|
1
2
3
4
|
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 清空所有元素
1
|
m_itemVector.clear();
|
11 遍历
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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 删除符合条件的项
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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 正序遍历 然后反序遍历
1
2
3
4
5
6
7
8
9
10
11
12
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中查找
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#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;
|