Vector series--c++11 to vector member functions (Cbegin (), Cend (), Crbegin (), Crend (), Emplace (), data () in combat C + +

Source: Internet
Author: User

The emplace_back mentioned above is the added content of c++11.

So this blog is to list the c++11 to the vector container expansion.

Std::vector::cbegin and Std::vector::cend
These two methods are corresponding to Std::vector::begin and Std::vector::end, from the literal can be seen, more than a ' C ', as the name implies is the meaning of const.
So:
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 <iostream>#include <vector>intMain () {STD:: vector<int>Myvector = {Ten, -, -, +, -};STD::cout<<"Myvector contains:"; for(Autoit = Myvector.cbegin (); It! = Myvector.cend (); ++it)STD::cout<<"'<< *it;STD::cout<<' \ n ';return 0;} Output:myvector contains:Ten  -  -  +  -

Std::vector::crbegin and Std::vector::crend
These two methods do not explain, compared to the above is more than a ' r ', reverse abbreviation, reverse iterator, the code is omitted.

Std::vector::emplace
Emplace_back has been discussed before, in fact, there is a method called Emplace.
What I want to say is that emplace to Emplace_back is like the insert to push_back.
Reading the English description is intuitive:

Emplace:construct and insert Element
Emplace_back:construct and insert element at the end

How to use:

#include <iostream>#include <vector>intMain () {STD:: vector<int>Myvector = {Ten, -, -};Autoit = Myvector.emplace (Myvector.begin () +1, -); Myvector.emplace (IT, $); Myvector.emplace (Myvector.end (), -);STD::cout<<"Myvector contains:"; for(Auto& X:myvector)STD::cout<<"'<< x;STD::cout<<' \ n ';return 0;} Output:myvector contains:Ten  $  -  -  -  -

std::vector::d ata
Returns a direct pointer to the memory array used internally by the vector to store its owned elements.

#include <iostream>#include <vector>intMain () {STD:: vector<int>Myvector (5);int* p = myvector.data (); *p =Ten;  ++p; *p = -; p[2] = -;STD::cout<<"Myvector contains:"; for(unsignedI=0; I<myvector.size (); ++i)STD::cout<<"'<< Myvector[i];STD::cout<<' \ n ';return 0;} Output:myvector contains:Ten  - 0  - 0

Std::vector::shrink_to_fit
Requests the container to reduce their capacity to fit their size.
is to reduce space

#include <iostream>#include <vector>intMain () {STD:: vector<int>Myvector ( -);STD::cout<<"1. Capacity of Myvector: "<< myvector.capacity () <<' \ n ';STD::cout<<"1. Size of Myvector: "<< myvector.size () <<' \ n '; Myvector.resize (Ten);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;}//Output1.Capacity of Myvector: -1.Size of Myvector: -2.Capacity of Myvector: -2.Size of Myvector:Ten3.Capacity of Myvector:Ten3.Size of Myvector:Ten

At this point, it is to understand the difference between size and capacity, will be more understanding of the difference between resize and reserve!

Vector series--c++11 to vector member functions (Cbegin (), Cend (), Crbegin (), Crend (), Emplace (), data () in combat C + +

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.