Vector container of C++stl

Source: Internet
Author: User

Beginner STL, the following content and when they accumulate, and then gradually improve.

Vector containers (vectors) are sequential containers, which are contiguous allocations of memory, support random access, and are very similar in terms of data arrangement.

The difference between an array and a vector is that the array is statically allocated space, and once the size of the space is allocated, it can no longer be changed, for example, int a[6]; vector is dynamically allocating memory, and as the element is constantly inserted, it expands its capacity in accordance with its own set of mechanisms. The capacity growth of the vector container is increased by one times the container's current capacity.

As for iterators, it resembles a pointer to an element in the vector.

------------------------------------------------------------------------

"Front function"

① function Prototypes:

Reference Front ();

Const_reference Front ();

② Features:

Returns a reference to the starting element in the current vector container, which returns the first element in the vector container.

------------------------------------------------------------------------

"Back function"

① function Prototypes:

Reference back ();

Const_reference back ();

② Features:

Returns a reference to the end element in the current vector container, which returns the last element in the vector container.

------------------------------------------------------------------------

The "Begin function"

① function Prototypes:

Iterator begin ();

Const_iterator begin ();

② Features:

Returns an iterator to the starting element in the current vector container.

------------------------------------------------------------------------

"End Function"

① function Prototypes:

Iterator End ();

Const_iterator end ();

② Features:

Returns an iterator to the end element in the current vector container.

------------------------------------------------------------------------

#include <iostream>#include<vector>using namespacestd;intmain () {vector<Char>v; Vector<Char>::iterator Iter1;//iterators 1vector<Char>::iterator Iter2;//iterators 2V.push_back ('A'); V.push_back ('B'); V.push_back ('C'); V.push_back ('D'); V.push_back ('E'); cout<<"v.front () ="<<v.front () <<endl;//Output The first element in a vector containercout<<"v.back () ="<<v.back () <<endl;//Output The last element in a vector containerIter1=V.begin (); cout<<*iter1<<Endl; Iter2= V.end ()-1;//Note that v.end () points to the next position of the last element, so the last element is accessed//the correct operation is: V1.end ()-1cout << *iter2 <<Endl;return 0;}

OUTPUT:

V.front () = A
V.back () = E
A
E

Vector container of C++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.