Effective stl--switching techniques to modify excess capacity

Source: Internet
Author: User
Tags bitset

Correction of excess capacity through "switching techniques"

Suppose a vector is enlarged in the process of use, but in the subsequent process the elements in the vector are deleted, although the elements in the vector are deleted but the capacity of the vector is not smaller, that is, the original application of the memory space is not released, adding at a certain time the vector capacity of 10000, The number of elements in the last vector may be 10, then the excess space is wasted, and this time you can change the size of the vectors by exchanging algorithms.

#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;

In the process of using the s to make large, and then delete all, this use of the capacity of S is very large, but size is very small.

String (s). Swap (s); "Shrink to fit" on S.

Avoid using vector<bool>,vector<bool> is not an STL container, it does not hold bool. Because Vector<bool> is a pseudo-container, it does not save the true bool, but instead packs the bool to save space. Hey, in a typical implementation, each bool saved in a vector occupies a single bit, while a 8-bit byte will hold 8 "bool". The internal,vector<bool> uses an idea that is equivalent to the bit field to represent the bool it pretends to hold.

However, there are alternatives in the STL. The deque<bool> is a STL container that holds the true bool value. Deque internal memory is not contiguous. So it is not possible to pass the data in deque<bool> to a C API that expects to get a bool array. The second alternative is that Bitset,bitset is not an STL container, but it is part of the C + + standard library. Unlike an STL container, its size is fixed at compile time, so it does not support inserting and deleting elements. Because it is not an STL container, it does not support iterator.

Effective stl--switching techniques to modify excess capacity

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.