effective stl vector with a string of notes

Original link: http://www.cnblogs.com/yang3wei/archive/2012/03/20/2739841.html

Reprinted from: http://hi.baidu.com/hins_pan/blog/item/515b0637f92499d2a3cc2b8a.html

The second big chapter is primarily concerned with the use of both vector and string of stl. Both are stl continue-stored way to organize data, in particular vector is provided and an array of very similar behavior.

    Both can replace the standard C language array, the second author in the first chapter to explain the benefits instead of the standard array with two stl. First, they simplify new and delete [] such an operation, application and release by the system itself to manage the elements of space, thereby reducing the possibility of wasted space and memory leak; secondly, stl offers many features not provided by the standard array in particular we can safely traverse the elements without fear of cross-border issues. In this section of the string problem encountered in the use of multi-threaded programs, because of the use of counting function string (string pointing to the same content in the same area of ​​memory) can be damaged in a multithreaded environment caused by unpredictable consequences, so in case you want to close a multi-threaded environment using string counting function.

    Use push_back, you can insert data into the tail of the vector. When the vector data in memory to the current size of their applications when, by way of self-vector growth to double the capacity to make your own, which means that if you want to insert the N data vector about to increment lnN times. When increment to apply the new space, data transfer, destruction and release the old space, and so the old data. For large N, when this is no small overhead. May be used to reserve command to manually allocate space vector, when the number of data is known in advance, the capacity can be provided directly to the vector, so that a space will be allocated over. If the time is not known, then it can be the vector is now set to the maximum space in the data space and then insertion adjusted to an appropriate size. The remaining space can be calculated using the size () (the number of elements in the vector prior conventional) and Capacity () (the number of elements can be accommodated in the vector).

    Achieve string is the subject of the third debate. Here are four string string implementations meet the functional requirements, but it is different in construction. These differences mainly in the end determination size, capacity and usage count and the position data containing string.

    vecotr and string themselves can also be converted to a pointer, so that the C program by using vector :: operator [] and the string :: c_str ().

    Using as vector <> (v) .swap (v) means may be trimmed in v extra space, and that the iterator does not fail.

    vector <bool> stl is a need attention, because it does not bool variable as a minimum unit to its operation, but all the binary data as a whole to look. Using the vector <bool> v; bool * pb = & v [0]; compilation error occurs, because [not return a bool reference vector <bool> of the operator]. You may be used deque <bool> and bitset achieved set storage bool variable. Just deque <bool> can be converted to an array of pointers, because the chain is stored in deque; bitset not support the insertion and deletion not support iterator (raw stuff).


Reproduced in: https: //www.cnblogs.com/yang3wei/archive/2012/03/20/2739841.html

Guess you like

Origin blog.csdn.net/weixin_30328063/article/details/94782787