Valid STL -- exchange skills to modify excess capacity, and optimize tivestl --

Source: Internet
Author: User

Valid STL -- exchange skills to modify excess capacity, and optimize tivestl --

Adjust excess capacity through "exchange skills"

Assume that the capacity of a vector is increased during use, but the elements in the vector are deleted in the future. Although the elements in the vector are deleted, the capacity of the vector is not decreased, that is to say, the originally applied memory space is not released. The vector capacity is 10000 at a certain time point, and the number of elements in the last vector may be 10. Therefore, excessive space is a waste, in this case, we can modify the vector size through the swap algorithm.

# Include <iostream>

# Include <vector>

# Include <algorithm>

Using namespace std;

 

Int main ()

{

Vector <int> vc;

Vc. reserve (128 );

For (int I = 0; I <10; I ++)

Vc. push_back (I );

Cout <vc. size () <endl;

Cout <vc. capacity () <endl;

Vector <int> (vc). swap (vc );

Cout <vc. size () <endl;

Cout <vc. capacity () <endl;

System ("pause ");

Return 0;

}

 

String s;

// Increase the size of s and delete all items. The size of s is large, but the size is small.

String (s). swap (s); // "contract to fit" on s ".

 

Avoid using vector <bool>. vector <bool> is not an STL container and does not contain bool. Because vector <bool> is a pseudo container, it does not save the real bool, but packs bool to save space. In a typical implementation, each bool stored in the vector occupies a single bit, and an 8-bit byte will hold eight "bool ". Internally, vector <bool> uses the Equivalent Concept of bitfield to represent the bool it pretends to hold.

However, there are alternatives in STL. Deque <bool> is an STL container that stores the real bool value. The Deque internal memory is not consecutive. Therefore, you cannot pass data in deque <bool> to a c api that wants to obtain the bool array. The second alternative is bitset. bitset is not an STL container, but is part of the C ++ standard library. Unlike the STL container, its size is fixed during the compilation period, so it does not support inserting or deleting elements. Because it is not an STL container, it does not support iterator.

 

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.