Vector. Resize? And? What is the difference between vector. Reserve ?. XML

Source: Internet
Author: User
Pre {Line-Height: 1; color: #9f1d66; Background-color: # a0ffc0; font-size: 16px ;}. sysfunc {color: # 5d57ff; font-style: italic; font-weight: bold ;}. selffuc {color: #8e0ed3 ;}. bool {color: #008000 ;}. condition {color: #008000; font-weight: bold ;}. key {color: #440080 ;}. vaR {color: #008000; font-style: italic ;}. digit {color: #000080; font-weight: bold ;}. includepre {color: # 661d9f ;}. operator? {Color: # fd1a53; font-weight: bold ;}

Reserve 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 a new element, you must use push_back () /insert () function. Resize is used to change the container size and create objects. Therefore, after calling this function, you can reference objects in the container. Therefore, when a new element is added, use the operator [] operator or the iterator to reference element objects.

Furthermore, there are differences between the two functions. The reserve function is followed by a parameter, that is, the space of the container to be reserved. The resize function can have two parameters, the first parameter is the new container size, and the second parameter is to add new elements in the container. If this parameter is omitted, the default constructor of the element object is called. The following are examples of the two functions:

???? Vector <int>? Myvec ;???? Myvec. Reserve (? 100 ?);????? //? The new element has not yet been constructed ,?????????????????????????????? //? In this case, the [] access element ???? For? (Int? I? =? 0 ;? I? <? 100 ;? I ++ ?)????... {????????? Myvec. push_back (? I ?);? // The new element is constructed at this time ????}???? Myvec. Resize (? 102 ?);?????? //? Two new elements are constructed using the default constructor of the element ???? Myvec [2, 100]? =? 1 ;??????????? // Directly operate the new element ???? Myvec [2, 101]? =? 2 ;? The first contact with these two interfaces may be confused. In fact, the interface name is an excellent description of the function, resize is to re-allocate the size, and reserve is to reserve a certain amount of space. The two interfaces have differences and are similar. The following is an analysis of their details .????? To implement the resize semantics, the resize interface provides two guarantees :???????????? First, ensure that the range is [0 ,? New_size) data within the range is valid. If the subscript index is in this range, vector [indext] is valid .????????????? The second is to ensure that the range is [0 ,? Data Out Of The new_size range is invalid. If the subscript index is out of the range, vector [indext] is invalid .????? Reserve only ensures that the space (capacity) of a vector is at least N of the size specified by its parameter. In the range [0 ,? N) in the range, if the subscript is index, the access to vector [Index] may be legal or illegal, depending on the actual situation .????? The common feature of the resize and reserve interfaces is that they both ensure that the space size (capacity) of the vector reaches the minimum size specified by its parameter.

Because the source code of the two interfaces is quite simplified, You can paste them here:

?? Void? Resize (size_type? New_size )? {? Resize (new_size ,? T ());?}?? Void? Resize (size_type? New_size ,? Const? T &? X )? {???? If? (New_size? <? Size ())?????? Erase (begin ()? +? New_size ,? End ());? //? Ensure that data outside the erase range is invalid ???? Else ?????? Insert (end (),? New_size? -? Size (),? X );? //? Fill in the vacant data within the range to ensure that the data within the range is valid

Used in this article? Calligraphy and painting novel software? Released, the content has nothing to do with the software, and the painting and calligraphy novel software? More comfortable reading, more comfortable writing, and easier Publishing.

Vector. Resize? And? What is the difference between vector. Reserve ?. XML

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.