# Include <vector>
# Include <iostream>
Int main (void)
{
Using namespace STD;
Vector <int> V;
V. push_back (6 );
V. push_back (7 );
V. push_back (8 );
V. push_back (10 );
For (INT I = 0; I <v. Size (); I ++)
Cout <"V [" <I <"] =" <V [I] <Endl;
Cout <Endl;
V. insert (V. Begin () + 3, 9); // insert 9 before element 10
V. insert (V. Begin (), 5); // insert 5 as the first element
V. insert (V. End (), 11); // insert 11 as the last element
Cout <Endl;
For (I = 0; I <v. Size (); I ++)
Cout <"V [" <I <"] =" <V [I] <Endl;
Vector <int>: iterator ITER, iend;
Int J;
Iend = V. End ();
Cout <Endl;
For (iter = V. Begin (), j = 0; iter! = Iend; ITER ++, J ++)
{
Cout <"V [" <j <"] =" <* ITER <Endl;
}
Iter = V. Begin ();
V. Erase (ITER + 2 );// (1)
// V. Erase (ITER + 2, iend );// (2)
Cout <Endl;
For (iter = V. Begin (), j = 0; iter! = Iend; ITER ++, J ++)
{
Cout <"V [" <j <"] =" <* ITER <Endl;
}
Cout <Endl;
For (/* int */I = 0; I <v. Size (); I ++)
{
Cout <"V [" <I <"] =" <V [I] <Endl;
}
Return 0;
}
(1) print information:
V [0] = 6
V [1] = 7
V [2] = 8
V [3] = 10
V [0] = 5
V [1] = 6
V [2] = 7
V [3] = 8
V [4] = 9
V [5] = 10
V [6] = 11
V [0] = 5
V [1] = 6
V [2] = 7
V [3] = 8
V [4] = 9
V [5] = 10
V [6] = 11
V [0] = 5
V [1] = 6
V [2] = 8
V [3] = 9
V [4] = 10
V [5] = 11 // do not know why
V [6] = 11
V [0] = 5
V [1] = 6
V [2] = 8
V [3] = 9
V [4] = 10
V [5] = 11
Press any key to continue
(2) print information
V [0] = 6
V [1] = 7
V [2] = 8
V [3] = 10
V [0] = 5
V [1] = 6
V [2] = 7
V [3] = 8
V [4] = 9
V [5] = 10
V [6] = 11
V [0] = 5
V [1] = 6
V [2] = 7
V [3] = 8
V [4] = 9
V [5] = 10
V [6] = 11
V [0] = 5
V [1] = 6
V [2] = 7
V [3] = 8 // do not know why
V [4] = 9
V [5] = 10
V [6] = 11
V [0] = 5
V [1] = 6
Press any key to continue
Only Query Information
MATERIALS:
Http://topic.csdn.net/u/20080909/20/a3b94068-eb76-4963-8bf8-7c9bebe1f9e0.html
(Reprinted)
When many people use vector, they do not pay too much attention to its iterator usage. When the number of elements in the vector changes, some iterator errors may occur. Let's take a look at the following code and try it out, it will make you suddenly enlightened
Void main ()
{
Vector <int> member;
Member. push_back (1 );
Member. push_back (2 );
Member. push_back (2 );
Member. push_back (3 );
Member. push_back (1 );
Member. push_back (2 );
Member. push_back (4 );
Vector <int>: iterator ITER;
For (iter = member. Begin (); iter! = Member. End (); ITER ++)
Cout <* ITER <Endl;
Cout <"Do erase --------" <Endl;
For (iter = member. Begin ();
ITER! = Member. End (); ITER ++)
{
If (* iter = 2)
{
Member. Erase (ITER );
}
}
For (iter = member. Begin ();
ITER! = Member. End (); ITER ++)
Cout <* ITER <Endl;
}
At first glance, isn't this the same as the first method mentioned above? It seems that there is no problem. However, you should note that after calling erase, you can go back to the for loop and continue to use the iterator and execute the ++ operation.
Okay. Let's review the description of the erase function:
Iterator erase (iterator position );
Iterator erase (iterator first, iterator last );
Now we only focus on the side effects and return values after function execution. After the function is called, all iterators pointing to position and first become invalid. The returned value is an iterator pointing to the elements following the last deleted element. Therefore, the ITER in the code above becomes invalid after erase is called. I tested it in vs2005 and did crash in the ++ operation.
To solve this problem, we only need to discard the original iterator and use the return value. The Code is as follows:
For (iter = member. Begin (); iter! = Member. End ();)
{
If (* iter = 2)
{
Iter = member. Erase (ITER );
}
Else
{
ITER ++;
}
}
1 /**//*
2 (c) C ++ stl_study: http://hi.baidu.com/2008rightman
3
4 filename: vectorfindandrease. cpp
5 Compiler: Visual C + + 8.0
6 Description: Demo how to erase iterator in a right way.
7 release: 11/14/2006
8 */
9 # include <iostream>
10 # include <vector>
11 # include <algorithm>
12
13int main (){
14 const int iasize = 11;
15 int Ia [] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
16
17 STD: vector <int> ivec;
18 ivec. insert (ivec. End (), IA, Ia + iasize );
19
20 // compile OK, but run-time error !!
21 // For (STD: vector <int >:: iterator iter = ivec. Begin ();
22 // iter! = Ivec. End (); ++ ITER ){
23 // If (* iter = 8 ){
24 // ivec. Erase (ITER );
25 //}
26 //}
27
28 // This is a right way to do it.
29 STD: vector <int >:: iterator iter = find (ivec. Begin (), ivec. End (), 8 );
30 if (ITER! = Ivec. End ()){
31 ivec. Erase (ITER );
32}
33
34 // cout the result
35 For (STD: vector <int >:: const_iterator iter = ivec. Begin ();
36 iter! = Ivec. End (); ++ ITER ){
37
38 STD: cout <* ITER <STD: Endl;
39}
40
41 return 0;
42}