In practice, the vector series in c ++-C ++ 11 extends the vector member functions (cbegin (), cend (), crbegin (), crend (), and emplace (), data ())
The emplace_back mentioned above is added to C ++ 11.
Therefore, this blog just wants to list how C ++ 11 expands the vector container.
Std: vector: cbegin and std: vector: cend
These two methods correspond to std: vector: begin and std: vector: end. It can be seen from the literal that there is an additional 'C ', as the name implies, it means const.
Therefore:
Std: vector: cbegin:Returns a const_iterator pointing to the first element in the container.
Std: vector: cend:Returns a const_iterator pointing to the past-the-end element in the container.
#include
#include
int main (){ std::vector
myvector = {10,20,30,40,50}; std::cout << "myvector contains:"; for (auto it = myvector.cbegin(); it != myvector.cend(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0;}Output:myvector contains: 10 20 30 40 50
Std: vector: crbegin and std: vector: crend
The two methods are not explained. In comparison to the above, there is an additional 'R', the abbreviation of reverse, and the code is omitted when the iterator is reversed.
Std: vector: emplace
We have discussed emplace_back before. In fact, there is another method called emplace.
What I want to say is that emplace is for emplace_back, just like insert is for push_back.
The English description is intuitive:
Emplace: Construct and insert element
Emplace_back: Construct and insert element at the end
How to use:
#include
#include
int main (){ std::vector
myvector = {10,20,30}; auto it = myvector.emplace ( myvector.begin()+1, 100 ); myvector.emplace ( it, 200 ); myvector.emplace ( myvector.end(), 300 ); std::cout << "myvector contains:"; for (auto& x: myvector) std::cout << ' ' << x; std::cout << '\n'; return 0;}Output:myvector contains: 10 200 100 20 30 300
Std: vector: data
Returns a direct pointer to the memory array used internally by the vector to store its owned elements.
#include
#include
int main (){ std::vector
myvector (5); int* p = myvector.data(); *p = 10; ++p; *p = 20; p[2] = 100; std::cout << "myvector contains:"; for (unsigned i=0; i<myvector.size(); ++i)="" std::cout="" <<="" '="" myvector[i];="" '\n';="" return="" 0;="" }="" output:="" myvector="" contains:="" 10="" 20="" 0="" 100="" 0
Std: vector: shrink_to_fit
Requests the container to reduce its capacity to fit its size.
Is to reduce space
# Include # Include Int main () {std: vector Myvector (100); std: cout <"1. capacity of myvector: "<myvector. capacity () <'\ n'; std: cout <"1. size of myvector: "<myvector. size () <'\ n'; myvector. resize (10); std: cout <"2. capacity of myvector: "<myvector. capacity () <'\ n'; std: cout <"2. size of myvector: "<myvector. size () <'\ n'; myvector. shrink_to_fit (); std: cout <"3. capacity of myvector: "<myvector. capacity () <'\ n'; std: cout <"3. size of myvector: "<myvector. size () <'\ n'; return 0;} // Output 1. capacity of myvector: 1001. size of myvector: 1002. capacity of myvector: 1002. size of myvector: 103. capacity of myvector: 103. size of myvector: 10
At this point, we need to understand the difference between size and capacity, so that we can better understand the difference between resize and reserve!