vector--c++ STL Learning

Source: Internet
Author: User

The data structure of the vector is an array of dynamic arrays, which means that we do not have to care about the amount of capacity that the array defines beforehand, and its size will grow dynamically. Similar to arrays, we can add and delete elements at the end, as well as random access and modification of element values.

?? Vector is the most common container in STL, which is a sequential container that supports random access. Vectors are contiguous allocations of memory, which are very similar in terms of data arrangement.
?? The difference is that the array is statically allocated space, once the size of the space allocated, it can no longer change, and vector is the dynamic allocation of space, as the elements are constantly inserted, it will continue to expand its capacity according to its own set of mechanisms.
?? Vector expansion mechanism: increases by one times the container's current capacity. The vector container allocates a contiguous memory space, each time the container grows, instead of simply overlaying the original contiguous memory space, it re-applies a larger piece of new memory and copies the elements of the existing container one by one, then destroys the old memory. Iterators that originally point to the old memory space are invalidated, so when the container is manipulated, the iterator is updated in a timely manner.

The first step is to introduce the header file #include

(1) Creation of vectors
vector<int> vec1; // 创建一个空的vector vector<int> vec2(vec1); // 创建一个vector vec2,并用vec1去初始化vec2 vector<int> vec3(10); // 创建一个含有n个数据的vector vector<int> vec4(10,0); // 创建含有10个数据的vector,并全部初始化为0

Constructors are overloaded, and vectors can be created in a variety of ways.

(2) push_back (), Pop_back () add Delete element
// 在vector尾部添加元素  vec1.push_back(4);    // 在vector尾部删除元素  vec1.pop_back();    // 在vector头部添加元素,无法完成,因为vector的数据结构为数组,无法在头部插入元素,否则需要整个数组前移;    // 在vector头部删除元素,无法完成,理由同上。

You can use Push_back () to continuously add elements to the tail of the vector, using pop_back to delete the trailing elements. The operation is very convenient, more convenient than we directly use the array structure.

(3) [],at (), take the element value of a position
// 取vector中某位置的元素值cout << "在1位置的元素值为:" << vec1.at(1) << endl;cout << "在1位置的元素值为:" << vec1[1] << endl;

Because the data structure of a vector is an array, random access is possible.

(4) Begin (), end (), pointer to head element, tail element
void PrintVector(vector<int> ve){cout << "Vector中的数据为:";vector<int>::iterator veIterator;for (veIterator = ve.begin(); veIterator < ve.end(); veIterator++){cout << *veIterator << " ";}cout << endl;}

The iterator can be interpreted as a pointer, and the Begin (), end () is understood as the head and tail pointers respectively. This gives you access to every element in the vector.

(5) Back (), front (), access to the head element and tail element
// 返回尾部数据的引用cout << "尾部数据的值为:" << vec1.back() << endl; // 返回头部数据的引用cout << "头部数据的值为:" << vec1.front() << endl;
(6) Max_size (), Maximum capacity; Size (), the current number of elements.
cout << "vector中的最大容量为:" << vec1.max_size() << endl;cout << "vector中的元素个数为:" << vec1.size() << endl;
(7) empty () to determine if the vector is empty
cout << "vector是否为空:" << vec1.empty() << endl;

If empty, returns 1. Otherwise, 0 is returned.

(8) Swap (): Swaps the values in two vectors. (9) Sort (): Sort vectors in ascending order; reverse (): Sort vectors in descending order.
// 对vector进行升序排序sort(vec1.begin(), vec1.end());// 对vector进行降序排序reverse(vec1.begin(), vec1.end());

The arguments passed are the ranges that need to be sorted, because the entire vector is ordered, so the parameters point to the head and tail, respectively.

(ten) [],at (): Modify Element
// 修改vector中的某个值vec1[2] = 99;vec1.at(3) = 88;
(one) erase (): Delete an element
// 删除数组的某个元素// 为什么要使用iterator来进行定位,因为数组如果要删除一个元素或者插入一个元素,会导致其他元素移动,所以不能直接进行删除vector<int>::iterator vItera = vec1.begin();vItera = vItera + 2;vec1.erase(vItera);

When you delete an element, you are borrowing an iterator.

() Insert (): Insert Element
// vector插入某元素,要使用iterator来定位某个位置vector<int>::iterator vInsert = vec1.begin();vInsert = vInsert + 2;vec1.insert(vInsert, 777);

also use iterators.

() Clear (): Clears all elements
// 清除所有数据vec1.clear();cout << "vector是否为空:" << vec1.empty() << endl;

After execution is an empty vector that contains no elements.

More detailed information about vectors can be found in the documentation: Http://en.cppreference.com/w/cpp/container/vector

vector--c++ 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.