Stl-vector explanation, stlvector

Source: Internet
Author: User

Stl-vector explanation, stlvector

Stl-vector is the most widely used container. Similar to array, it stores data in a continuous space and supports random access. Compared with array, vector is very convenient and efficient for space applications. The iterator makes vector more flexible and secure. The design is based on the vector, and the vector is used under the keyboard.

1 vector nature

The data structure of the vector is as follows. Through the public interfaces of three iterators: start, finish, and end_of_storage, the data storage and overflow judgment (iter> = iv. end (), size, capacity (capacity and size, so as not to constantly apply for space to consume resources), heavy load operators [], empty judgment, the first element, the last element, and so on.

class vector{

protected:
iterator start ;
iterator finish;
iterator end_of_storage;
public:
iterator begin () { return start ; }
iterator end() { return finish; }
size_type size() const { return size_type( end() - begin()); }
size_type capacity() const { return size_type(end_of_storage - begin()); }
bool empty () const { return begin() == end(); }
reference operator[] (size_type n) { return *(begin() + n); }
reference front () { return *begin(); }

}

Because vector uses continuous space to store data, continuous expansion will lead to a large number of new space applications, copying elements, and releasing the original space, which is very time-consuming. When vector applies for space, all parts of the space will be applied for backup, as shown in [finish, end_of_storage. Only when finish = end_of_storage, apply for a new space (2 * capacity (). Figure 2 is based on figure 1, data storage scenarios when elements are inserted to change the space.

Figure 1 vector Data Storage-1

In Figure 1 and figure 2, start no longer points to the same address. Expanding space is a new and larger space, so it is not only a start iterator, other iterators that point to space changes before vector will become invalid. This can easily cause bugs.

Vector <int> iv (3,-1 );
Iv. reserve (10 );
Iv. resize (10,-1 );Constructors are not described;

Reserve (n). The application space is equivalent to expanding [finish, end_of_storage). If n <= capacity () is invalid, it can be understood as a major change to end_of_storage (or capacity) -- reserve () matches capacity ();

Resize (10): Apply for a space and assign a value, which is equivalent to changing [start, finish). It can be understood as mainly changing finish (or size () -- resize () to match size ().

2. Space release

Erase (), resize (), and clear () only change finish (or size () without changing end_of_storage.

Swap () releases space -- clears and changes end_of_storage:

Vector <int> iv (10,-1 );
Iv. reserve (500 );
Vector <int> (). swap (iv); // swap space
Cout <iv. capacity () <endl; // output 0

Swap () releases space -- releases extra space and changes end_of_storage:

Vector <int> iv (10,-1 );
Iv. reserve (500 );
Vector <int> (iv). swap (iv); // swap space
Cout <iv. capacity () <endl; // output 10

3. resize () and operator []

Operator [] makes vector and array Processing completely similar, but operator [] can easily cause exceptions and errors, as shown below:

Vector <int> iv ();
Iv. reserve (500 );
Iv [300] =-1;
Cout <iv. capacity () <endl; // output 500
Cout <iv. size () <endl; // output 0
Here, the iterator does not meet the expectation and does not know what is expected. It is recommended that resize () and operator [] be used together to make the operation completely equivalent to array and secure:
Vector <int> iv ();
Iv. resize( 500, 0 );
Iv [300] =-1;
Cout <iv. capacity () <endl; // output 500
Cout <iv. size () <endl; // output 500

4. vector iterator

The vector iterator is very simple, equivalent to pointers, ++, --, + n,-n,>, <,! = And other operations can be applied -- random access iterator, basically all stl algorithms can be applied here.

The insert operation of vector may cause Space redistribution and invalidate the original iterator.

PS: we strongly recommend that you do not use comparison iterators such as >,<, ≤, ≥, and so on. You can use them directly over time! = To avoid mixing with other non-random access iterator containers.

 

References:

1. Hou Jie. STL source code analysis;

2. Hou Jie. STL source code analysis comments;

I would like to thank my colleagues for their preaching and teaching.

----

Hard work can only be seen as the necessity of life forever; even if there is no hope of harvest, it can continue cultivation with peace of mind. Classification: stl details, C/C ++ labels: stl, vector

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.