C ++ STL vector container and stlvector container

Source: Internet
Author: User

C ++ STL vector container and stlvector container

STL for beginners. The following content will be improved later when accumulated and used.

Vector is an ordered container that continuously allocates memory and supports random access. From the perspective of data arrangement, vector containers are extremely similar to arrays.

The difference between an array and a vector is that an array is statically allocated space. Once the space is allocated, it cannot be changed. For example, int a [6]; vector dynamically allocates memory. With the insertion of elements, it will expand its capacity according to its own mechanism, the capacity growth of vector containers is doubled by the current capacity of containers.

 

As for the iterator, it is similar to a pointer pointing to elements in a vector.

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

[Front function]

① Function prototype:

Reference front ();

Const_reference front ();

② Function:

Returns the reference of the starting element in the current vector container, that is, the first element in the vector container.

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

[Back function]

① Function prototype:

Reference back ();

Const_reference back ();

② Function:

Returns the reference of the element at the end of the current vector container, that is, the last element in the vector container.

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

[Begin function]

① Function prototype:

Iterator begin ();

Const_iterator begin ();

② Function:

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

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

[End function]

① Function prototype:

Iterator end ();

Const_iterator end ();

② Function:

Returns an iterator for the elements at the end of the current vector container.

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

# Include <iostream> # include <vector> using namespace std; int main () {vector <char> v; vector <char >:: iterator iter1; // iterator 1 vector <char>: iterator iter2; // iterator 2 v. 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 cout in the vector container <"v. back () = "<v. back () <endl; // output the last element in the vector container, iter1 = v. begin (); cout <* iter1 <endl; iter2 = v. end ()-1; // note v. end () points to the next position of the last element. Therefore, the correct operation for accessing the last element // is: v1.end ()-1 cout <* iter2 <endl; return 0 ;}

OUTPUT:

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

Related Article

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.