Difference between vector. Resize and vector. Reserve

Source: Internet
Author: User

STD: Difference Between Reserve and resize of Vector
1. Reserve: Allocate space, change capacity but not size
2.
Resize: Allocate space. Changing the capacity also changes the size.

If you know the vector size, resize it as an array without allocating extra memory.

 

ReserveIt is a container reserved space, but does not actually create element objects. Before creating an object, you cannot reference the elements in the container. Therefore, when adding new elements, you must usePush_back ()/insert ()Function.

ResizeIs to change the container size and create an object. Therefore, after calling this function, you can reference objects in the container. Therefore, when a new element is addedOPERATOR []Or use an iterator to reference element objects.

Furthermore, there are differences between the two functions,ReserveA parameter after the function, that is, the space of the container to be reserved;ResizeThe function can have two parameters. The first parameter is the new size of the container, and the second parameter is the new element in the container. If this parameter is omitted, then, the default constructor of the element object is called. The following are examples of the two functions:

    1. Vector <Int> Myvec;
    2. Myvec. Reserve (100 );// The new element has not been constructed yet,
    3. // The [] access element cannot be used at this time.
    4. For(IntI = 0; I <100; I ++)
    5. ...{
    6. Myvec. push_back (I );// New elements are constructed at this time
    7. }
    8. Myvec. Resize (102 );// Construct two new elements using the default constructor of the element
    9. Myvec [100] = 1;// Directly operate on new elements
    10. Myvec [101] = 2;

 

 

We all know that the vector is continuously distributed in the memory, so we always reserve some space outside of all existing elements in the design. Otherwise, the memory will be allocated again each time a new element is appended, the preparation will be low.

If there may be about 500 elements in a vector, compare the two methods:

1. Vector <int> myvec, and then call myvec. push_back (***) 500 times (****)

2. Vector <int> myvec (500), and then call myvec. push_back (***) 500 times (****)

 

In practice 2, you only need to allocate one or two times of memory, but in practice 1, you do not know how many times the memory is allocated.

 

Now, there are two problems:

Q1. how much space is reserved for the current container (Maximum number of elements that can be accommodated without re-allocating memory)?

Q2. how to reset the reserved size of the current container?

 

A1: Capacity ().

A2: Reserve ().

 

Analysis:

Therefore, resize () and reserve are two different types of elements. Resize affects the number of elements. Reserve only allocates the reserved space.

Therefore, capacity ()> = size () is always established.

 

There are several other problems:

1. vector <int> A (10);. reserve (20); A [10] = 999; // error, because a has no subscript of 10, the current size () = 10, capacity () = 20; to append subscript 10, this element can only be push_back;

 

2. Assume that the vector <int> sample;

If the current size () is 50 and capacity () is 100, perform the following operations:

(1) resize (10 ). // size () = 10; elements from 10 to 49 are deleted. capacity () = 100, unchanged, no memory reallocation.

(2) resize (60 ). // size () = 60; 50 to 59 subscripts are filled with the default constructor. capacity () = 100, unchanged, no memory reallocation.

(3) resize (60,999 9 ). // size () = 60; fill the 50-59 subscript with 9999. capacity () = 100, unchanged, no memory reallocation.

(4) resize (1, 200 ). // size () = 200; fill with the default constructor for downloading from 50 to 199. capacity () = 200, automatic resizing, and memory reallocation.

 

(5) Reserve (10). // size () = 50; unchanged, no elements are deleted, capacity () = 100, unchanged. That is, the reserve call does not work.

(6) Reserve (60). // size () = 50; the element is not changed, capacity () = 100, and remains unchanged. That is, the reserve call does not work.

(7) Reserve (200). // size () = 50; element not changed, capacity () = 200, resize, and re-allocate memory.

 

3. Vector <int> sample (10); // size () = 10; reserve () = 10;

Sample. push_back (999); // size () = 11; reserve () = 15; // automatic resizing. Why isn't reseve 11? This overbalance is not necessarily or 15.

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.