STD: traps in Vector

Source: Internet
Author: User

Since the last sort endless loop, I feel it is necessary to review the STL container. Vector is the most common and used container in STL. When I read the source code of vector, I still find many traps. These traps may make your program very ugly if you are not careful about them.

 

1. traps when adding elements

The commonly used methods for adding elements in a vector are pushback and insert. Let's take a look at this program:

 

Int _ tmain (INT argc, _ tchar * argv [])
{
STD: vector <int> VEC;
VEC. push_back (0 );

STD: vector <int >:: iterator iter1 = VEC. Begin ();
VEC. push_back (1 );

Int n = * iter1;

 

Return 0;
}

 

What is n equal? It is easy to say that it must be 0. When you run this program, you will find that the program crashes after it is executed in the second to last line. Why does it crash? Of course, the iter1 iterator is invalid. Why is it invalid? Let's talk about it. First, we need to start with the space configuration method of vector. We all know that vector dynamically allocates memory space. Many people think that the space of a vector increases linearly and continuously with the increase of elements. But on the contrary, the vector is not a new continuous space after the original space, because it cannot guarantee that there is still space available for configuration after the original space, in addition, a large space is configured twice the original size, and then the original content is copied. Then, new elements are constructed after the original content and the original space is released. Therefore, if any operation on the vector causes space reconfiguration, all iterators pointing to the original vector will become invalid.

Looking at the above program, we first constructed an empty vector and then added an element. At this time, the container capacity is 1. If you add more elements, a large space will be configured. When the element size of a vector is the Npower of 1, 2, 4, 8, and 2, adding another element will open up space. At this time, all the iterators in the container will expire.

 

Ii. traps when deleting elements

Let's take a look at the following program:

Int _ tmain (INT argc, _ tchar * argv [])
{
STD: vector <int> VEC;
VEC. push_back (0 );
VEC. push_back (1 );
VEC. push_back (2 );

STD: vector <int >:: iterator iter1 = VEC. Begin () + 2;

VEC. Erase (VEC. Begin () + 1 );

Int n = * iter1;

Return 0;
}

What will happen? Crash like the previous program. The reason is also very simple. After a vector deletes an element, it will invalidate all iterators behind the element.

 

3. traps when reading Elements

The most terrible thing in a vector is that it is out of the border. Every time we read an element, we should check whether it exceeds the size of the vector. In addition, when we call the front and back functions, we must check whether the container is empty or else it will crash.

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.