Detection vector space automatic allocation strategy

Source: Internet
Author: User

Read the "STL source analysis" can know that vector space automatic allocation strategy is the space doubling.
Because the STL version is more, for the common two do under detection, vs2012 and GCC 5.3.0
Size (): Gets the current usage size
Capacity (): Gets the current container size

Allocate 1 million times to check.
Code

#include <vector>
#include <iostream>

void Vectortest ()
{
    std::vector<int> vTest ;
    for (int i = 0; i < 1000000; ++i)
    {
        vtest.push_back (1);
        int nSize = Vtest.size ();
        int ncapacity = Vtest.capacity ();
        if (nSize = = ncapacity)
        {
            std::cout<<nsize<< "\ T" <<nCapacity<<std::endl;
        }
    }
    std::cout<<std::endl;
};

Experimental results:
vs2012 each new 1/2 of the current space

g++ 5.3.0 each time the current space size is added, that is, the multiplier

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.