The mutual transfer of STL vector and array

Source: Internet
Author: User

C + + STL library vector is known as a dynamic array of reputation, why. I think there are several reasons for this:

1 It can make up for ordinary array (such as "int a[8]") only fixed length defects;

2 It can make up for general heap array (such as int *p = new Int[n]) need to know in advance the length, and the length is immutable defects.

In some cases, such as reading a set of float-type data from a file that is unknown in length.

3 relative to the other containers in the STL, such as List,vector and array closer, its internal data stored in a continuous storage space, access efficiency will be higher; The list is a linked list, the data is not adjacent to the storage space.


Another manifestation of Vector's reputation as a "dynamic array" is that it is flexible with arrays.

An array of vectors

Using the vector's constructor, you can easily convert an array to a vector. For example:

    Float arrheight[] = {1.68,1.72,1.83,2.05,2.35,1.78,2.1,1.96};
    Vector<float> vecheight (Arrheight, arrheight+sizeof (arrheight)/sizeof (float));

Refer to official Documentation: http://www.cplusplus.com/reference/vector/vector/

There are several types of vectors in the structure:

Default (1)
Explicit vector (const allocator_type& alloc = Allocator_type ());
Fill (2)
Explicit vector (size_type n);
         Vector (size_type N, const value_type& val,
                 const allocator_type& alloc = Allocator_type ());
Range (3)
Template <class inputiterator>
  vector (Inputiterator, Inputiterator, last,
          const ALLOCATOR_TYPE & alloc = Allocator_type ());
Copy (4)
Vector (const vector& x);
Vector (const vector& x, const allocator_type& alloc);
Move (5)
Vector (vector&& x);
Vector (vector&& x, const allocator_type& alloc);
Initializer List (6)
Vector (initializer_list<value_type> il,
       const allocator_type& alloc = Allocator_type ());

The example above is the third, range constructor. I experimented under VS2015, and by looking at the memory, I could see that the memory address of the Vecheight data part was the same as the arrheight, which means that in the example above, there is no real copy of the data, and this should be the "write-time copy" technique. Improve the efficiency of data replication.

In addition, you can refer to the blog: http://www.cnblogs.com/mr-wid/archive/2013/01/22/2871105.html


Vector-Array

Since the data inside the vector is stored in contiguous storage space, the vector array actually needs only to obtain the address of the first data in the vector and the length of the data. If you are simply passing the reference, without any action, directly to the address, if you want to replicate data, you can borrow the memory copy function "memcpy". For example:

    float *buffer = new float[sizeof (arrheight)];
    if (!vecheight.empty ())
    {
        memcpy (buffer, &vecheight[0], vecheight.size () *sizeof (float));
    

This paragraph reference blog: http://nianning1981.blog.163.com/blog/static/3083014320103171299619/


Third, the application of the recommendations

1,vector, as a dynamic array, is implemented by allocating a chunk of memory in advance, allocating a larger chunk of memory when it is not enough, and then automatically copying the previous data into the new memory block.

Therefore, for efficiency reasons, if the implementation knows the length of data to be stored, you can use the Resize function to open up enough memory to avoid subsequent memory copies.

2, if the element of an array is a character, it is recommended that you use string instead of vector<char>.


With two blog posts on the efficiency of vectors and other containers:

Http://www.cnblogs.com/smiler/p/4457622.html

Http://www.cppblog.com/sailing/articles/161659.html


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.