Vector of C ++

Source: Internet
Author: User
Vector of C ++

1. To use vector, you must include the following code in your header file:

# Include <vector>

2. The vector belongs to the STD naming domain. Therefore, you must use the naming conventions to add

Using STD: vector;

Or

Using namespace STD;

Or add the prefix directly before the code that uses the vector.

Eg:

STD: vector <int> myhouse;

Http://our2848884.blog.163.com/blog/static/1468548342011669513996/

Introduction to Vector

Vector is the most common container in STL. It is an ordered container that supports random access. A vector is a continuously allocated memory. From the perspective of data arrangement, it is very similar to an array. The difference is that an array is statically allocated space. Once the space is allocated, it cannot be changed. Vector dynamically allocates space. As elements are inserted, it will expand its capacity according to its own mechanism.

Vector Expansion Mechanism: Increase by double the current container capacity. The vector container allocates a continuous memory space. The increase of each container does not simply overlay the original continuous memory space, but re-applies for a larger new memory, copy the elements in the existing container one by one, and then destroy the old memory. The iterator that originally points to the old memory space has expired. Therefore, when operating the container, the iterator should be updated in time.

Data Structure of Vector

The vector data structure adopts a continuous linear space, which is a linear storage. He uses three iterators _ first, _ last, and _ end to point to different ranges of the allocated linear space. below is the source code for declaring the three iterators.

template<class _Ty, class _A= allocator< _Ty> > 
class vector{ 
    ... 
    protected: 
    iterator _First, _Last, _End; 
};

_ First points to the header of the space used, _ last points to the tail of the space used (size), and _ end points to the tail of the space used (capacity. For example:

int data[6]={3,5,7,9,2,4}; 
vector<int> vdata(data, data+6); 
vdata.push_back(6); 
...

During vector initialization, the requested space is 6 and six elements in data are stored. When 7th "6" elements are inserted into vdata, the vector uses its own Expansion Mechanism to apply for a new space. The data storage structure 1 is shown in:

Figure 1 Expanded vector Memory Structure

A brief description. When inserting 7th elements "6", the vector found that its space was not enough, so it applied for a new memory size of 12 (doubling ), copy the existing data to the front of the new space and insert 7th elements. In this case, the _ last iterator points to the last valid element, and the _ end iterator points to the last valid space position of the vector. We can use the size function of the vector to obtain the size of the current vector, which is 7 at this time. We can use the Capacity member function to obtain the capacity of the current vector, which is 12 at this time.

Priority_queue

A data structure that is not necessarily FIFO. Priority_queue does not allow traversal of the container. It only allows elements to be operated at both ends of the container. Priority_queue has a data member _ PR comp in the definition, which is a comparison function used to list the column sequence.

Well, this article has been explained so far. If you have any questions, please feel free to ask.

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.