vector(1)

Original address: http://www.cnblogs.com/BeyondAnyTime/archive/2012/08/08/2627666.html

1. A brief introduction to vector

As one of the standard containers provided by STL, vector is often used, has a very important position, and is very convenient to use. Vector is also called a vector. A vector can be vividly described as an array whose length can be changed dynamically. Its function is similar to that of an array. In fact, a more professional description is: vector is a multi-functional template class and function library that can operate a variety of data structures and algorithms. The reason why vector is considered a container is that it can store various types like a container. The object, simply put, a vector is a dynamic array that can store any type, and can increase and compress data. (Note: STL containers can be said to be class templates from an implementation perspective.)

So what is the main difference between vector and array? ? This is very helpful for understanding vector~~~~~

Array: Static space is allocated. Generally, it cannot be changed after it is allocated. Just like the well-known definition of an array, the length of the array cannot be changed, and we cannot perform out-of-bounds access, but the compiler does not check for out-of-bounds access. , which should be paid special attention to when we are programming (many may be bothered by such mistakes!!). The length of the generally applied array cannot meet our requirements. We have to reapply for a larger array, and then copy the data in the original array.

Vector: Dynamic space is allocated, that is, we found that we can not specify the size of the container when declaring the vector container. The space of the vector is automatically expanded with the addition of elements. However, we must be responsible for confirming that the space allocated by the vector is continuous, that is, it supports random access to subscripts in the array. In fact, the implementation mechanism of the vector is: a part of the space is reserved, and the size of the reserved space is determined by a certain amount. The ratio increases. If the space is not enough, to ensure continuity, you must re-create a new space, then move the original elements to the new space, and reserve new space at the same time (and the newly allocated space is larger than the original allocated space), Finally free up the original space. The advantage of reserving space in this way is that you don't have to reallocate space every time you add an element to the vector.

2. Commonly used functions in vecotr containers

2.1. Constructor of the vector container

The declaration methods of vector containers mainly include the following:


vector<Elem> v , which creates an empty vector.

vector <Elem> v1(v) , copies a vector.

vector <Elem> v(n) Create a vector containing n data, all of which have been constructed by default.

vector <Elem> v(n, elem) , creates a vector containing n copies of elem.

vector <Elem> v(beg,end) , creates a vector with the interval [beg;end) .

v.~ vector <Elem>() , destroy all data, free memory.


The following uses a piece of code to demonstrate several common ways of declaring a vector:


小结:注意这种:vector <Elem> c(beg,end)声明方式,创建一个和[beg;end)区间元素相同的vector,一定要注意是左闭右开区间,同时需要说的是,STL中不论是容器还是算法都是采用的这种左闭右开区间办事的,包括v.end()函数也是返回的vector末端的下位置,相当于int a[n]的a[n],并不能访问~~~

2.2.vector中其他常用的函数用法


v.assign(beg,end)  , 将[beg; end)区间中的数据赋值给v。

v.assign(n,elem)    ,  将n个elem的拷贝赋值给v。

v.at(idx)                ,  传回索引idx所指的数据,如果idx越界,抛出out_of_range。

v.begin()               ,  传回迭代器重的可一个数据。

v.capacity()           ,  返回容器中数据个数。

v.clear()                ,  移除容器中所有数据。

v.empty()              ,  判断容器是否为空。

v.end()                  ,  指向迭代器中的最后一个数据地址。


用上面提到的函数写一个程序演练一下吧:

View Code

运行结果:


小结:关于assign函数,对vector变量进行赋值,并且能够自动完成vector大小的修改。

v.capacity()      返回容器中数据个数。

v.size()        返回容器中实际数据的个数。

v.reserve()     保留适当的容量。 

v.resize(num)    重新指定队列的长度。

v.max_size()       返回容器中最大数据的数量。


View Code

输出结果:


 


v.insert(pos,elem)         在pos位置插入一个elem拷贝,传回新数据位置(位置指传回地址值)。

v.insert(pos,n,elem)      在pos位置插入在[beg,end)区间的数据。无返回值

v.insert(pos,beg,end)       在pos位置插入n个elem数据。无返回值

v.erase(pos)          删除pos位置的数据,传回下一个数据的位置

v.erase(beg,end)       删除[beg,end)区间的数据,传回下一个数据的位置。


看看vector中的元素的插入和删除操作吧:

View Code

运行结果:


小结:vector 的reserve增加了vector的capacity,但是它的size没有改变!而resize改变了vector的capacity同时也增加了它的size!这是因为:(1)reserve是为容器预留空间,但在空间内不真正创建元素对象,所以在没有添加新的对象之前,不能引用容器内的元素。加入新的元素时,要调用push_back()/insert()函数。(2)resize则是改变容器的大小,且在创建对象,因此,调用这个函数之后,就可以引用容器内的对象了,因此当加入新的元素时,用operator[]操作符,或者用迭代器来引用元素对象。此时再调用push_back()函数,是加在这个新的空间后面的。

 


c.rbegin()       传回一个逆向队列的第一个数据。

c.rend()          传回一个逆向队列的最后一个数据的下一个位置。

c.pop_back()      删除最后一个数据。

c.push_back(elem)   在尾部加入一个数据。

c.front()          传回地一个数据。

c.back()           传回最后一个数据,不检查这个数据是否存在。

c1.swap(c2)        将c1和c2元素互换。

swap(c1,c2)        同上操作。




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325681849&siteId=291194637