C + + vector and array distinguish between reserve and resize __c++

Source: Internet
Author: User


The vector is a "dynamic" array:

Observe the address change of a fixed element:

#include <vector> #include <iostream> using namespace std;
        int main (int argc,char * argv[]) {vector<int> Myvec;
        cout << "Size:" << myvec.size () << Endl;
        cout << "Capacity:" << myvec.capacity () << Endl;
        Myvec.push_back (1);
        Myvec.push_back (2);
        cout << "Size:" << myvec.size () << Endl;
        cout << "Capacity:" << myvec.capacity () << Endl;
        cout << "Address of myvec[2] is:" << &myvec[2] << Endl;
        Myvec.push_back (3);
        cout << "Size:" << myvec.size () << Endl;
        cout << "Capacity:" << myvec.capacity () << Endl;
        cout << "Address of myvec[2] is:" << &myvec[2] << Endl;
        Myvec.push_back (4);
        cout << "Size:" << myvec.size () << Endl;
       cout << "Capacity:" << myvec.capacity () << Endl; cout << "Address of myvec[2] is:" << &myvec[2] << Endl;
        Myvec.push_back (5);
        cout << "Size:" << myvec.size () << Endl;
        cout << "Capacity:" << myvec.capacity () << Endl;
        cout << "Address of myvec[2] is:" << &myvec[2] << Endl;
        Myvec.push_back (6);
        cout << "Size:" << myvec.size () << Endl;
        cout << "Capacity:" << myvec.capacity () << Endl;
        cout << "Address of myvec[2] is:" << &myvec[2] << Endl;
        Myvec.reserve (100);
        cout << "after reserve (m)" << Endl;
        cout << "Size:" << myvec.size () << Endl;
        cout << "Address of myvec[2] is:" << &myvec[2] << Endl;
        cout << "Capacity:" << myvec.capacity () << Endl;

return 0;
 }

Each capacity extension means that a new space is applied, and a larger array is declared, so the position of the fixed element myvec[2] changes with capacity.

There are two ways to trigger space redistribution, one is to trigger automatically via Push_back (), and one is reserve () active triggering. If you frequent and delete elements, automatic trigger will look like a stupid way, it will be slow (c + + anything slow, compile slowly, slow). Therefore, a certain manual algorithm should be used to reduce the cost of vector automatic application space, which is between the pure manual application array and the fully automatic vector.

When inserting elements, the capacity will adaptively change from 2 to 4 to 8. Extra, do a erase operation, size () can clear zero, capacity will not retract 4 and 2. Correspondingly, those elements are not actually "deleted", can be "accessed" to, but at this time has been illegal unreliable data (in fact, it will not be like the general release of that too unreliable, after all capacity () has not changed, that is, this piece, such as malloc (), space, still in the hands, The other way is not to modify its content, but from the vector () interface, these should be waste of garbage data.


PS: There are four elements, resize (100), not 104, is 100, see some error examples of the for loop still when 104 to traverse, do not manually operation, with a size () to get the length bar.


The difference between reserve () and resize (). The former only reserves space, the latter is equal to the allocation of "0" element (default construction bar), the difference is that with the for () loop, press Capacity (), will print to the data should not be printed (because the encapsulation of the automatic initialization of the reason, such as Integer, perhaps 0).

Someone fraught, first resize (100), then Push_back (). Size () is an increase on the basis of 100, capacity () is still automatic jump, such as direct from 100 to 200, it is impossible to allow size () and capacity () synchronous growth, there is no reason, the former is the number of specific elements, the latter is the length of the array, It's like the difference between strlen () and sizeof () for a character array (for example, don't tangle with '/0 ' interference).

In fact, you can guess for yourself an overload of resize (), resize (size,value), and initialize the element with the size value.










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.