Vector & string

Source: Internet
Author: User

When you need to dynamically allocate arrays, consider using vector or string instead of array. In most cases, vector or string can completely replace array. However, when there are performance requirements, the string implemented based on the reference count may not meet the requirements. In a multi-threaded environment, the performance of the string implemented based on the reference count is worrying. you can disable the reference base or use the vector <char> instead.

Use reserve (size_t N) to reduce frequent reallocation overhead.
For vector:

1 vector <int> V;
2 V. Reserve (1000 );
3 For (INT I = 0; I <1000; ++ I)
4 V. push_back (I );

Of course, this method can only be used when it is possible to accurately or roughly predict how much capacity is required. However, if you cannot predict it in advance, you can allocate as much space as possible to accommodate the required elements. After all elements are added, use "swap trick" to cut off extra space (shrink-to-fit: direction in capacity ).

Swap trick:

1 vector <int> V;
2
3 vector <int> (V). Swap (v );

Vector <int> (v) creates a temporary vector that contains all int elements in v. Create a temporary vector to call the copy constructor of vector, and copy only the number of int elements actually contained in v. Therefore, the temporary vector capacity is exactly the space required to accommodate all elements. Then, swap (...) is used to exchange the content in the two containers. After switching, the V capacity is the actual int element capacity, and the excess space in V is reduced. After this sentence is executed, the temporary vector is released. String is similar to vector.
You can also use "swap trick" to clear a vector or string:

Vector <int> (). Swap (v );

Unlike resize (...), "swap trick" is used to modify the capacity of a vector or string to make it capacity = size.


Avoid using vector <bool>. Vector <bool> is a pseudo container. To save space, vector <bool> does not contain the actual bools. For details, see item18 (keep Itve STL).
There are multiple implementations of string. You need to know how to implement the current string and its features before using it. There are six differences in these implementations in objective STL:
1> whether to use reference count for string
2> the size of the string object may be 1 to 7 times the size of the char * pointer.
3> when creating a new string value, it may need to be dynamically allocated 0, 1, or 2 times.
4> If the values of multiple strings are the same, their size and capacity information may not be shared among these string objects.
5> does string support pre-object allocators?
6> different implementations adopt different policies in consideration of the minimum allocation Character Buffer

To pass the data of the vector and string to the left APIs, & V [0] is used for the vector.

1 // for Vector
2 void dosomething (const int * pints, size_t numints );
3
4 If (! V. Empty ()){
5 dosomething (& V [0], V. Size (); // never replace & V [0] by V. Begin ();
6}

For string, a pointer to container data cannot be obtained using a method similar to a vector. Cause: 1> the data stored in the string is not necessarily continuous; 2> the data in the string cannot end with null characters. Therefore, S. c_str () and c_str () are used to return the copy of data in string, read-only

1 // for string
2 void dosomething (const char * pstrings );
3
4 dosomething (S. c_str ());

Modify the value of S. c_str () in legacy APIs. In most cases, modifying the vector value is no problem. However, when the vector itself is ordered and the API modifies the value of the vector, potential problems may occur, especially when the API tries to add elements to the vector.
Only vector can be initialized through the legacy API, provided that the maximum capacity required for vector Initialization is known.

1 // c api: This function takes a pointer to an array of at most arraysize
2 // doubles and writes data to it. It returns the number of doubles written,
3 // which is never more than maxnumdoubles.
4 size_t fillarray (double * parray, size_t arraysize );
5 vector <double> VD (maxnumdoubles); // create a vector whose
6 // size is maxnumdoubles
7 VD. Resize (fillarray (& VD [0], VD. Size (); // have fillarray write data
8 // into VD, then resize VD
9 // to the number
10 // elements fillarray wrote

If you need to use the legacy API to initialize other containers, you can use vector as the transition:

1 // for string
2 size_t fillarray (double * parray, size_t arraysize );
3 vector <double> VD (maxnumdoubles );
4 size_t charswritten = fillstring (& VC [0], VC. Size ());
5 string S (VC. Begin (), VC. Begin () + charswritten );
6
7 // for other containers
8 size_t fillarray (double * parray, size_t arraysize );
9 vector <double> VD (maxnumdoubles );
10 VD. Resize (fillarray (& VD [0], VD. Size ()));
11
12 deque <double> D (VD. Begin (), VD. End ());
13 list <double> L (VD. Begin (), VD. End ());
14 set <double> S (VD. Begin (), VD. End ());

Similarly, you can use initialization methods to pass the elements saved in other containers to legacy APIs.

1 set <int> intsets;
2
3 vector <int> V (intsets. Begin (), intsets. End ());
4 If (! V. Empty ())
5 dosomething (& V [0], V. Size ());

Reference: Negative STL

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.