std::vector
template <class T , class Alloc = allocator<.T> > class vector; // generic template
vector介绍
vector是一种序列式容器,其实说白了就是一种大小可以变化的数组,因此vector首先是一个数组,也使用连续的内存空间存储数据中的元素,并且可以通过下标去访问和遍历数组中的元素,同时vector和数组的不同之处在于vector可存储的元素的个数是可以动态改变的,所占据的内存空间的大小是可以调整的。
对于vector的空间调整时通过在每次向vector容器中insert元素时,都会对当前的内存空间的占用情况进行一次检查,如果发现内存空间将要占满,那么vector就会重新申请一块比原来更大的一块内存空间,并且将原来内存空间中的元素复制到新申请的内存空间中,完成内存的扩容。那么通过这个过程会发现,vector进行一次内存调整要付出很大的时空代价,因此vector在新分配内存空间时都会分配一个相对比较大的内存空间,尽量少的避免重新分配内存,当然动态的调整大小,不需要vector的使用者关心,我们只需要使用他而已(∩_∩)。
将vector和STL中的其他容器(deques、lists和forward_list)相比,vector的优势在于可以直接通过下标访问元素,而且可以非常高效的在vector的尾部添加和删除元素;vector的劣势在于当在容器的非尾部添加和删除元素时,vector的效率会非常低下!
Member functions成员方法
- constructor(构造函数)
default (1) explicit vector (const allocator_type& alloc = allocator_type());
fill (2) explicit vector (size_type n, const value_type& val = value_type(),
const allocator_type& alloc = allocator_type());
range (3) template <class InputIterator>
vector (InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type());
copy (4) vector (const vector& x);</span>
示例如下:
default (1) explicit vector (const allocator_type& alloc = allocator_type());
fill (2) explicit vector (size_type n, const value_type& val = value_type(),
const allocator_type& alloc = allocator_type());
range (3) template <class InputIterator>
vector (InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type());
copy (4) vector (const vector& x);</span>
// constructing vectors
#include <iostream>
#include <vector>
int main ()
{
// constructors used in the same order as described above:
std::vector<int> first; // empty vector of ints
std::vector<int> second (4,100); // four ints with value 100
std::vector<int> third (second.begin(),second.end()); // iterating through second
std::vector<int> fourth (third); // a copy of third
// the iterator constructor can also be used to construct from arrays:
int myints[] = {16,2,77,29};
std::vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
return 0;
}
- operator=(赋值操作符)
// constructing vectors
#include <iostream>
#include <vector>
int main ()
{
// constructors used in the same order as described above:
std::vector<int> first; // empty vector of ints
std::vector<int> second (4,100); // four ints with value 100
std::vector<int> third (second.begin(),second.end()); // iterating through second
std::vector<int> fourth (third); // a copy of third
// the iterator constructor can also be used to construct from arrays:
int myints[] = {16,2,77,29};
std::vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
return 0;
}
=号操作符用于实现两个vector之间的赋值,注意:使用这个操作符时,=左边的vector会将原来容器的大小和保存的内容完全变成和右边的一致。
示例如下:
// vector assignment
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> foo (3,0);
std::vector<int> bar (5,0);
bar = foo;
foo = std::vector<int>();
std::cout << "Size of foo: " << int(foo.size()) << '\n';
std::cout << "Size of bar: " << int(bar.size()) << '\n';
return 0;
}
output: Size of foo: 0 Size of bar: 3 |
- Iterators
此函数族主要是返回迭代器完成对vector中的数组的遍历工作,函数列表如下:
-
begin
- Return iterator to beginning (public member function )
-
end
- Return iterator to end (public member function )
-
rbegin
- Return reverse iterator to reverse beginning (public member function )
-
rend
- Return reverse iterator to reverse end (public member function )
-
cbegin
- Return const_iterator to beginning (public member function )
-
cend
- Return const_iterator to end (public member function )
-
crbegin
- Return const_reverse_iterator to reverse beginning (public member function )
-
crend
-
Return const_reverse_iterator to reverse end
(public member function )
- begin()/end()
// vector::begin/end
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myvector;
for (int i=1; i<=5; i++) myvector.push_back(i);
std::cout << "myvector contains:";
for (std::vector<int>::iterator it = myvector.begin() ; it != myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
Output: myvector contains: 1 2 3 4 5 |
- rbegin()/rend()
// vector::rbegin/rend
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myvector (5); // 5 default-constructed ints
std::vector<int>::reverse_iterator rit = myvector.rbegin();
int i=0;
for (rit = myvector.rbegin(); rit!= myvector.rend(); ++rit)
*rit = ++i;
std::cout << "myvector contains:";
for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
Output: myvector contains: 5 4 3 2 1 |