Vector member function parsing

Source: Internet
Author: User

Vector linear container, whose elements Jenge sorted according to a linear sequence, and a dynamic array is very phase like array, its elements are stored in contiguous storage space, which also means that we can not only use iterators (iterator) to access the elements, but also use a pointer to access the offsets, and it is not the same as the regular array , vector storage elements can be self-active, can expand and reduce their own active storage space,

Vector Advantages:

1. Use subscript to access individual elements

2. Iterators can traverse containers in different ways

3. Ability to add or remove elements at the end of a container

Even though the container consumes a lot of other memory when it actively handles the size of the capacity, the container can provide the same performance as the array, and it can adjust the storage size very well.

Compared to other standard sequential containers (deques or lists), it is more effective to access the elements inside the container and to add and remove elements at the end, add and remove elements in other locations, vectors are less than other sequential containers, and in iterators and references are no better than lists support.

The size of the container and the capacity of the container is different, the size refers to the number of elements, the capacity is allocated memory size, capacity is generally equal to or greater than the size of the container, Vector::size () returns the size of the container, vector::capacity () returns the capacity value, A portion larger than the container size is used to prevent the container from being used, and allocating memory each time will greatly affect the performance of the program, so the general allocation capacity is greater than the size of the container, and to specify the size of the allocated capacity, you can use Vector::reserve (). However, the specified value is greater than the size () value,

1. Construct and copy constructors

Explicit vector (Const allocator& = Allocator ());

Explicit vector (size_type N, const t& value= T (), const allocator& = Allocator ());

Template <class inputiterator>

Vector (Inputiterator First, Inputiterator last, const allocator& = Allocator ());

Vector (const vector<t,allocator>& x);

Explicit: is to prevent implicit conversions, allocator is a memory allocation pattern, usually using the default

Vector<int> A; Create an empty container

Vector<int> B (10,100); Create individual elements, each with an element value of

Vector<int> C (B.begin (), B.end ()); Use iterators to create a new container with partial elements

Vector<int> D (C); Copy constructor, create a container that is identical

2. Destructors

~vector ()

Destroys the container object and reclaims all allocated memory

3. Overloaded with = Symbol

Vector<int> E;

E = B; Use = Symbol

B = vector<int> (); Place B as an empty container

4. Vector::begin () returns an iterator to the first element

Function Prototypes:

Iterator begin (); Returns a variable iterator

Const_iterator begin () const; Returns a constant iterator that is not mutable

5.vector::end () returns the first position after the crossing, which is the next position of the last element

Iterator End ();

Const_iterator end () const;

6.vector::rbegin () The first element of the reverse order, which is the last element of the positive order

Reverse_iterator Rbegin ();

Const_reverse_iterator rbegin () const;

7.vector::rend () Deserializes the last element of the next position, also equivalent to the first element in the positive order of the previous position

Reverse_iterator rend ();

Const_reverse_iterator rend () const;

Same as the vector::end () principle

8.vector::size () returns the number of elements in the container

Size_type size () const;

Note the difference from vector::capacity ()

9.vector::max_size ()

Size_type max_size () const;

Returns the maximum number of elements that a container can store. This is a limit, and when the container expands to this maximum value, it can no longer increase itself actively.

Vector::resize ()

void Resize (Size_type sz, t C = t ());

The number of elements to allocate the container again. This can also change the capacity of the container, assuming that the number of elements allocated again is smaller than the original. The sequence is truncated. The back part is discarded. Assuming that the number is greater than the original, the value of the following is the value of C, default feel 0

Vector::capacity ()

Size_type capacity () const;

Returns the size of the actual storage space of the vector, which is generally greater than or equal to the number of vector elements, noting the difference from the size () function

Vector::empty ()

BOOL empty () const;

False if the number of elements is 0 o'clock returns true. is based on the number of elements, not the size of the container's storage space.

Vector::reserve ()

void Reserve (Size_type n);

The size of the allocated space again. Just this n value is larger than the value returned by the original capacity (). Otherwise the storage space remains the same, the N value is greater than the original actual storage space to allocate space again, but the maximum value can not be greater than the value of max_size, or throw an exception

Vector::operator[]//overloaded [] Symbol

Reference operator[] (size_type N);

Const_reference operator[] (size_type n) const;

Implementation of the Subscript access element

Vector::at ()

Const_reference at (size_type N) const;

Reference at (size_type N);

The function is the same as the subscript access element. The difference is that when this function crosses out, an exception is thrown Out_of_range

Vector::front ()

Reference Front ();

Const_reference Front () const;

Returns the value of the first element, which differs from the Begin () function. The Begin () function returns an iterator to the first element

Vector::back ()

Reference back ();

Const_reference back () const;

Same, returns the value of the last element, noting the difference from the end () function

Vector::assign ()

Template <class inputiterator> void assign (Inputiterator first, Inputiterator last);

void Assign (size_type n, const t& u);

The original element is discarded and then the element is allocated again, and the first function is to use an iterator. The second function uses n elements, and each element has a value of U.

Vector::p ush_back ()

void push_back (const t& x);

Insert element x at the last position of the container, assuming that the size value is greater than the capacity value, the space will be allocated again

Vector::p op_back ()

void Pop_back ();

Delete last Element

Vector::insert ()

Iterator insert (iterator position, const t& x);

void Insert (iterator position, Size_type N, const t& x);

Template <class inputiterator>

void Insert (iterator position, Inputiterator first, Inputiterator last);

Inserts a new element.

The first function inserts an element with a value of x before the position specified by the iterator

The second function, inserting an n element with an X value before the position specified by the iterator

A third function, a sequence iterator that inserts another container before the position specified by the iterator first to last

If the number of elements is greater than capacity after inserting a new element, the space is allocated again

Vector::erase ()

Iterator Erase (iterator position);

Iterator Erase (iterator first, iterator last);

Delete an element or a sequence

Vector::swap ()

void swap (vector<t,allocator>& VEC);

Swaps the contents of these two containers. This involves another allocation of storage space

Vector::clear ()

void Clear ();

Empty the contents of the container, the size value is 0, but the storage space does not change

#include <vector> #include <iostream>using namespace std;int _tmain (int argc, _tchar* argv[]) {//constructor, copy  Constructors (element types to be consistent),vector<int> A; Create an empty container vector<int> B (10,100); Create a 10 element, each with a value of 100vector<int> C (B.begin (), B.end ()); Using iterators, the ability to take some elements to create a new container vector<int> D (C); Copy the constructor to create a completely identical container//reload =vector<int> E; E = B; Vector::begin (), which returns an iterator vector<int> F (10);          Create a container with 10 elements for (int i = 0; i < x; i++) {f[i] = i; }/*vector<int> F;         Create an empty container for (int i = 0; i < i++) {f.push_back (i);} */vector<int>::iterator beginiter = F.begin () cout << *beginiter << Endl; Output 0//vector::end () returns an iterator Vector<int>::iterator Enditer = F.end (); enditer--; Move backward one position cout << *enditer << Endl; Output 9//vector::rbegin () returns the first element in reverse, equivalent to the last element vector<int>::reverse_iterator Reverbeiter = F.rbegin (); cout < < *reverbeiter << Endl; Output 9//vector::rend () The last of the reverse orderThe next position of an element is also equivalent to the first element of the positive order before a position vector<int>::reverse_iterator Revereniter = F.rend (); Revereniter--;cout << *revereniter << Endl; Output 0//vector::size () returns the number of elements cout << f.size () << Endl; Output 10//vector::max_size () cout << f.max_size () << Endl; Output 1073741823. This is the Limit element number//vector::resize () cout << f.size () << Endl; Output 10f.resize (5); for (int k = 0; k < f.size (); k++) cout << f[k] << ""; Output 0 1 2 3 4 cout << endl;//vector::capacity () cout << f.size () << Endl; 5cout << f.capacity () << Endl; 10//vector::empty () b.resize (0), cout << b.size () << Endl; 0cout << b.capacity () << Endl; 10cout << b.empty () << Endl; True//vector::reserve ()//Allocate storage size again cout << c.capacity () << Endl; 10c.reserve (4); cout << c.capacity () << Endl; 10c.reserve, cout << c.capacity () << Endl; 14//vector::operator []cout << f[0] << Endl; The first element is 0//vector::at () try{cout << "f.size =" << f.size () << Endl; 5 cout << f.at (6) << Endl; Throw exception}catch (out_of_range) {cout << "at () Cross-border" << Endl;} Vector::front () Returns the value of the first element cout << F.front () << Endl; 0//vector::back () cout << f.back () << Endl; 4//vector::assign () cout << a.size () << Endl; 0vector<int>::iterator first = C.begin (); Vector<int>::iterator End = C.end ()-2; A.assign (first,end); cout << a.size () << Endl; 8cout << a.capacity () << Endl; 8a.assign (5,3); All the original elements will be discarded and then assigned again cout << a.size () << Endl; 5cout << a.capacity () << Endl; 8//vector::p ush_back () cout << * (F.end ()-1) << Endl; 4f.push_back, cout << * (F.end ()-1) << Endl; 100//vector::p op_back () cout << * (F.end ()-1) << Endl; 100f.pop_back (); cout << * (F.end ()-1) << Endl; 4//vector::swap () F.swap (D); Swap the contents of these two containers for (int f = 0; F < f.size (); f++) cout << f[f] << ""; cout << endl;for (int d = 0; D < d.size ();         d++) cout << d[d] << "";     cout << endl;//vector::clear () f.clear () cout << f.size () << Endl; 0cout << f.capacity () << Endl; 10return 0;}

Link resolution in English

http://www.cplusplus.com/reference/stl/


Vector member function parsing

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.