[Cpp] // P96_example3.cpp: Defines the entry point for the console application.
//
# Include "stdafx. h"
# Include <iostream>
# Include <vector>
Void print (std: vector <int> );
Int _ tmain (int argc, _ TCHAR * argv [])
{
Std: vector <int> vec;
Vec. push_back (1 );
Vec. push_back (6 );
Vec. push_back (6 );
Vec. push_back (3 );
// Delete all 6 in the vec Array
Std: vector <int >:: iterator itor1;
Std: vector <int >:: iterator itor2;
For (itor1 = vec. begin (); itor1! = Vec. end (); itor1 ++)
{
If (6 = * itor1)
{
Itor2 = itor1;
Vec. erase (itor2); // Delete the element at the specified position
}
}
Print (vec );
Return 0;
}
Void print (std: vector <int> v)
{
Std: cout <"vector size is:" <v. size () <std: endl;
Std: vector <int >:: iterator p = v. begin ();
While (p! = V. end ())
{
Std: cout <* p <std: endl;
P ++;
}
}
// P96_example3.cpp: Defines the entry point for the console application.
//
# Include "stdafx. h"
# Include <iostream>
# Include <vector>
Void print (std: vector <int> );
Int _ tmain (int argc, _ TCHAR * argv [])
{
Std: vector <int> vec;
Vec. push_back (1 );
Vec. push_back (6 );
Vec. push_back (6 );
Vec. push_back (3 );
// Delete all 6 in the vec Array
Std: vector <int >:: iterator itor1;
Std: vector <int >:: iterator itor2;
For (itor1 = vec. begin (); itor1! = Vec. end (); itor1 ++)
{
If (6 = * itor1)
{
Itor2 = itor1;
Vec. erase (itor2); // Delete the element at the specified position
}
}
Print (vec );
Return 0;
}
Void print (std: vector <int> v)
{
Std: cout <"vector size is:" <v. size () <std: endl;
Std: vector <int >:: iterator p = v. begin ();
While (p! = V. end ())
{
Std: cout <* p <std: endl;
P ++;
}
}
Resolution:
This is an iterator problem. You can only Delete the first six. Later, the iterator becomes invalid and cannot delete subsequent elements.
Itor2 = itor1; this statement indicates that the two iterators are the same. Vec. erase (itor2); equal to vec. erase (itor1);, the pointer has already pointed to the next element. Itor1 ++; and then auto-increment, pointing to the next element 3, skipping the second 6.
Answer:
Method 1: Use the remove function in the vector Template to modify the code:
[Cpp] // P96_example3.cpp: Defines the entry point for the console application.
//
# Include "stdafx. h"
# Include <iostream>
# Include <vector>
<SPAN style = "COLOR: # ff0000"> # include <algorithm> </SPAN>
Void print (std: vector <int> );
Int _ tmain (int argc, _ TCHAR * argv [])
{
Std: vector <int> vec;
Vec. push_back (1 );
Vec. push_back (6 );
Vec. push_back (6 );
Vec. push_back (3 );
// Delete all 6 in the vec Array
Std: vector <int >:: iterator itor1;
Std: vector <int >:: iterator itor2;
Itor1 = vec. begin ();
<SPAN style = "COLOR: # ff0000"> vec. erase (std: remove (vec. begin (), vec. end (), 6), vec. end (); </SPAN>
Print (vec );
Return 0;
}
Void print (std: vector <int> v)
{
Std: cout <"vector size is:" <v. size () <std: endl;
Std: vector <int >:: iterator p = v. begin ();
While (p! = V. end ())
{
Std: cout <* p <std: endl;
P ++;
}
}
// P96_example3.cpp: Defines the entry point for the console application.
//
# Include "stdafx. h"
# Include <iostream>
# Include <vector>
# Include <algorithm>
Void print (std: vector <int> );
Int _ tmain (int argc, _ TCHAR * argv [])
{
Std: vector <int> vec;
Vec. push_back (1 );
Vec. push_back (6 );
Vec. push_back (6 );
Vec. push_back (3 );
// Delete all 6 in the vec Array
Std: vector <int >:: iterator itor1;
Std: vector <int >:: iterator itor2;
Itor1 = vec. begin ();
Vec. erase (std: remove (vec. begin (), vec. end (), 6), vec. end ());
Print (vec );
Return 0;
}
Void print (std: vector <int> v)
{
Std: cout <"vector size is:" <v. size () <std: endl;
Std: vector <int >:: iterator p = v. begin ();
While (p! = V. end ())
{
Std: cout <* p <std: endl;
P ++;
}
}
Method 2: To avoid skipping the second 6, you can make itor1 -- and return it to the original position. Modify the Code as follows:
[Cpp] // P96_example3.cpp: Defines the entry point for the console application.
//
# Include "stdafx. h"
# Include <iostream>
# Include <vector>
Void print (std: vector <int> );
Int _ tmain (int argc, _ TCHAR * argv [])
{
Std: vector <int> vec;
Vec. push_back (1 );
Vec. push_back (6 );
Vec. push_back (6 );
Vec. push_back (3 );
// Delete all 6 in the vec Array
Std: vector <int >:: iterator itor1;
Std: vector <int >:: iterator itor2;
Itor1 = vec. begin ();
For (itor1 = vec. begin (); itor1! = Vec. end (); itor1 ++)
{
If (6 = * itor1)
{
Itor2 = itor1;
Vec. erase (itor2); // Delete the element at the specified position
Itor1 --;
}
}
Print (vec );
Return 0;
}
Void print (std: vector <int> v)
{
Std: cout <"vector size is:" <v. size () <std: endl;
Std: vector <int >:: iterator p = v. begin ();
While (p! = V. end ())
{
Std: cout <* p <std: endl;
P ++;
}
}
// P96_example3.cpp: Defines the entry point for the console application.
//
# Include "stdafx. h"
# Include <iostream>
# Include <vector>
Void print (std: vector <int> );
Int _ tmain (int argc, _ TCHAR * argv [])
{
Std: vector <int> vec;
Vec. push_back (1 );
Vec. push_back (6 );
Vec. push_back (6 );
Vec. push_back (3 );
// Delete all 6 in the vec Array
Std: vector <int >:: iterator itor1;
Std: vector <int >:: iterator itor2;
Itor1 = vec. begin ();
For (itor1 = vec. begin (); itor1! = Vec. end (); itor1 ++)
{
If (6 = * itor1)
{
Itor2 = itor1;
Vec. erase (itor2); // Delete the element at the specified position
Itor1 --;
}
}
Print (vec );
Return 0;
}
Void print (std: vector <int> v)
{
Std: cout <"vector size is:" <v. size () <std: endl;
Std: vector <int >:: iterator p = v. begin ();
While (p! = V. end ())
{
Std: cout <* p <std: endl;
P ++;
}
}