Vector of STL Learning

Source: Internet
Author: User

Vector is a linear sequential structure. Equivalent to an array, but its size can be unspecified and automatically expanded. It can be manipulated like a pointer, and because of its nature we can completely view vectors as dynamic arrays.

Characteristics:

1. Specify a piece of contiguous storage as an array, but the space can be dynamically expanded. That is, it can operate like an array, and it can be manipulated dynamically.

2. Convenient random access, it is accessed like an array.

3, save space, because it is continuous storage, in the storage area is not wasted, of course, the storage area is actually wasted.

4, in the internal insertion delete efficiency is very low, such operation is basically forbidden. Vectors are designed to append and delete operations on the backend only because the internal implementation of the vector is based on the principle of the sequential table.

5, when the dynamically added data operation vector By default allocation of the size of the memory to be re-allocated (multiply), copy and release, this operation is very performance-consuming.

The basic operations of vectors are:

S[i] accesses the elements in the container directly, in the following way.

S.front () returns the first element.

S.back () returns the tail element.

S.push_back (x) inserts an element x at the end of the table.

S.size () returns the table length.

S.empty () returns True when the table is empty, otherwise false.

S.pop_back () deletes the footer element.

S.begin () returns a random-access iterator that points to the first element.

S.end () returns a random-access iterator that points to the next position in the tail element.

S.insert (it, x) inserts a new element x before the element to which it points to the iterator.

S.insert (it, n, x) inserts n x before the element to which it points to the iterator.

S.insert (it, first, last) inserts the sequence [first, last] specified by the iterator first and last into the front of the element that the iterator it points to.

S.erase (IT) removes the element pointed to by the iterator it.

S.erase (First, last) deletes the sequence [first, last] specified by the iterator first and last.

S.clear () deletes all the elements in the container.

S.swap (v) exchanges s with another vector object v.

Vector of STL Learning

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.