C + + STL vector (vector)

Source: Internet
Author: User

Transfer from the blog garden: https://www.cnblogs.com/zhonghuasong/p/5975979.html

Introduced

    1. A vector is a sequence container that represents a variable-sized array.
    2. Like arrays, vectors also use contiguous storage spaces to store elements. That means you can use the subscript to access the elements of the vector, as well as the arrays. But unlike an array, its size can be changed dynamically, and its size will be handled automatically by the container.
    3. Essentially, vectors use a dynamically allocated array to store its elements. When the new element is inserted, the array needs to be redistributed to increase the storage space. The practice is to assign a new array and then move all the elements to the array. In terms of time, this is a relatively expensive task because the vector does not reassign the size every time a new element is added to the container.
    4. Vector allocation space strategy: vectors allocate some extra space to accommodate potential growth because storage space is larger than the actual storage space required. Different libraries take different strategies to weigh the use and redistribution of space. However, redistribution should be a logarithmic growth interval, so that when an element is inserted at the end, the complexity of the constant time is completed.
    5. As a result, vectors take up more storage space, to gain the ability to manage storage space, and to grow dynamically in an efficient way.
    6. Compared to other dynamic sequence containers (deques, lists and forward_lists), vectors are more efficient at accessing elements, and adding and removing elements at the end is relatively efficient. For other delete and insert operations that are not at the end, it is less efficient. is better than lists and Forward_lists unified iterators and references.

Usage

1. Header files

#include<vector>

2. Vector Declaration and initialization

vector<int> vec;        //声明一个int型向量vector<int> vec(5);     //声明一个初始大小为5的int向量vector<int> vec(10, 1); //声明一个初始大小为10且值都是1的向量vector<int> vec(tmp);   //声明并用tmp向量初始化vec向量vector<int> tmp(vec.begin(), vec.begin() + 3);  //用向量vec的第0个到第2个值初始化tmpint arr[5] = {1, 2, 3, 4, 5};   vector<int> vec(arr, arr + 5);      //将arr数组的元素用于初始化vec向量//说明:当然不包括arr[4]元素,末尾指针都是指结束元素的下一个元素,//这个主要是为了和vec.end()指针统一。vector<int> vec(&arr[1], &arr[4]); //将arr[1]~arr[4]范围内的元素作为vec的初始值

3. Vector Basic operation
(1). Capacity

    • Vector size: vec.size ();
    • Vector Maximum Capacity: vec.max_size ();
    • Change vector size: vec.resize ();
    • Vector true Size: vec.capacity ();
    • Vector empty: Vec.empty ();
    • Reduce the size of the vector to meet the size of the storage space occupied by the element: Vec.shrink_to_fit (); Shrink_to_fit

(2). Modify

    • Multiple element Assignment: Vec.assign (); Array-like assignment similar to initialization
    • add element at the end: Vec.push_back ();
    • Delete element at end: Vec.pop_back ();
    • Insert element Anywhere: Vec.insert ();
    • Delete elements Anywhere: vec.erase ();
    • Swaps the elements of two vectors: Vec.swap ();
    • Empty vector elements: vec.clear ();

(3) iterators

    • Start pointer: Vec.begin ();
    • End pointer: Vec.end (); Point to the next position of the last element
    • Start pointer to constant: Vec.cbegin (); This means that you cannot modify what you refer to by this pointer, but you can still modify it in other ways, and the pointer is movable.
    • Pointer to the end of the constant: Vec.cend ();

(4) Access to elements

    • Subscript access: vec[1]; Does not check if it is out of bounds
    • At Method Access: vec.at (1); The difference between the two is that the at will check if it is out of bounds and is thrown out of range exception
    • Access the first element: Vec.front ();
    • Access the last element: Vec.back ();
    • Returns a pointer: int* p = vec.data (); The reason for this is that the vector is an array of contiguous storage in memory, so you can return a pointer to the array. This is a feature of c++11.

(5) algorithm

    • Traversing elements
vector<int>::iterator it;for (it = vec.begin(); it != vec.end(); it++)    cout << *it << endl;//或者for (size_t i = 0; i < vec.size(); i++) {    cout << vec.at(i) << endl;}
    • Element rollover
#include <algorithm>reverse(vec.begin(), vec.end());
    • Ordering elements
#include <algorithm>sort(vec.begin(), vec.end()); //采用的是从小到大的排序//如果想从大到小排序,可以采用上面反转函数,也可以采用下面方法:bool Comp(const int& a, const int& b) {    return a > b;}sort(vec.begin(), vec.end(), Comp);

Reference documents:
Vector website
Blog Park Vector Blog

C + + STL vector (vector)

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.