Vector container 2

Source: Internet
Author: User

[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 ++;
}
}


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.