​std::vector简介及其使用​

本文中的vector指的是std::vector C++11标准。

Vector概述

template <class T,class Alloc = allocator <T> > class vector; //通用模板

  vector是表示可以改变大小的数组的序列容器。

  就像数组一样,vector使用连续存储空间存储元素,这意味着它们的元素也可以使用指向其元素的指针进行偏移来访问,并与数组一样高效。但与数组不同的是, vector的大小可以动态变化,并且是由容器自动处理的。

  在内部实现上,vector使用动态分配的数组来存储它们的元素。在插入新元素时,vector的大小增大,可能需要重新分配数组,这意味着可能要分配新数组并将原有数组中所有元素移动到这个新数组中。重新分配数组的时间成本相对高昂,因此,vector不会在每次向容器添加元素时都重新分配数组。vector容器可能会分配一些额外的存储空间来适应可能的增长,因此容器的实际容量可能比其包含的元素个数要大。不同库可以实现不同的增长策略以在使用现有内存和 重新分配内容之间取得平衡,但无论如何,重新分配内存时的数组大小应以对数增长,这样在vector末端插入单个元素时就可以得到平摊的常数时间复杂度。

  因此,与数组相比,vector消耗更多内存,以换取以更有效的方式管理存储空间。

  与其他动态序列容器(deques,lists和forward_lists)相比,vector可以非常高效地访问其元素(就像数组一样)并且相对高效地从其末尾添加或删除元素。 对于涉及在末尾以外的位置插入或删除元素的操作,性能比其他序列容器要差,并且与lists和forward_lists相比具有更少的迭代器和引用一致性。

容器属性

  • 顺序存储

   序列容器中的元素按照严格的线性顺序存储。各个元素通过使用它们在这个序列中的位置(index)来访问。

  • 动态数组

   允许直接访问序列中的任何元素,支持指针运算,相对快速的添加/删除序列末尾的元素。

  • 分配器

   容器使用allocator对象来动态处理其存储需求。

模板参数

  • T 

  元素的类型。只有当 T 保证在移动操作时不会抛出异常,实现才会进行优化,即在重新分配数组时移动元素而不是复制它们。

  别名为成员类型 vector :: value_type。

  • Alloc

  用于定义分配模型的分配器对象的类型。默认情况下,使用allocator类模板,该模板定义最简单的内存分配模型,并且与值无关。

  别名为成员类型 vector :: allocator_type。

成员类型

member typedefinitionnotes
value_typeThe first template parameter (T)
allocator_typeThe second template parameter (Alloc)defaults to: allocator<value_type>
referencevalue_type&
const_referenceconst value_type&
pointerallocator_traits<allocator_type>::pointerfor the default allocator: value_type*
const_pointerallocator_traits<allocator_type>::const_pointerfor the default allocator: const value_type*
iteratora random access iterator to value_typeconvertible to const_iterator
const_iteratora random access iterator to const value_type
reverse_iteratorreverse_iterator<iterator>
const_reverse_iteratorreverse_iterator<const_iterator>
difference_typea signed integral type, identical to: iterator_traits<iterator>::difference_typeusually the same as ptrdiff_t
size_typean unsigned integral type that can represent any non-negative value of difference_typeusually the same as size_t

成员函数

  •  (constructor) 构造函数
default (1)
explicit vector (const allocator_type& alloc = allocator_type());
fill (2)
explicit vector (size_type n);
vector (size_type n, const value_type& val,
        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);
vector (const vector& x, const allocator_type& alloc);
move (5)
vector (vector&& x);
vector (vector&& x, const allocator_type& alloc);
initializer list (6)
vector (initializer_list<value_type> il,
       const allocator_type& alloc = allocator_type());

   构造函数示例:

 构造函数示例

  •  (destructor) 析构函数
~vector();
  • operator= 赋值
copy (1)
vector& operator= (const vector& x);
move (2)
vector& operator= (vector&& x);
initializer list (3)
vector& operator= (initializer_list<value_type> il);
  •  迭代器相关
函数函数原型功能描述
beginiterator begin() noexcept;Return iterator to   beginning 
const_iterator   begin() const noexcept;
enditerator end() noexcept;Return iterator to   end 
const_iterator   end() const noexcept;
rbeginreverse_iterator rbegin()   noexcept;Return reverse   iterator to reverse beginning 
const_reverse_iterator   rbegin() const noexcept;
rendreverse_iterator rend()   noexcept;Return reverse   iterator to reverse end 
const_reverse_iterator   rend() const noexcept;
cbegin const_iterator cbegin() const   noexcept;Return const_iterator to   beginning 
cend const_iterator cend() const noexcept;Return const_iterator to   end 
crbegin const_reverse_iterator crbegin() const noexcept;Return const_reverse_iterator to   reverse beginning 
crend const_reverse_iterator crend()   const noexcept;Return const_reverse_iterator to   reverse end 

   迭代器示例:

 迭代器示例

  •  容器大小或容量相关
函数函数原型功能描述
sizesize_type size() const noexcept;Return size 
max_sizesize_type max_size() const   noexcept;Return maximum size 
resizevoid resize (size_type n);Change size 
void   resize (size_type n, const value_type& val);
capacitysize_type capacity() const noexcept;Return size of allocated storage   capacity 
emptybool empty() const noexcept;Test whether vector is   empty 
reservevoid reserve (size_type n);Request a change in   capacity 
shrink_to_fit void shrink_to_fit();Shrink to fit 

  示例:

 示例代码

  • 成员访问相关
函数函数原型功能描述
operator[]reference operator[] (size_type   n);Access element 
const_reference operator[] (size_type n) const;
atreference at (size_type n);Access element 
const_reference at (size_type n) const;
frontreference front();Access first   element 
const_reference front() const;
backreference back();Access last   element 
const_reference back() const;
data value_type* data() noexcept;Access data. Return value: A pointer to the first element in the array used internally by the vector.
const value_type* data() const noexcept;

  成员访问示例:

 成员访问示例

  • 添加、删除等修改相关操作
函数函数原型功能描述
assigntemplate <class InputIterator>
void assign (InputIterator first, InputIterator last);
Assign vector content. Assigns new contents to the vector, replacing its current contents, and modifying its size accordingly.
void assign (size_type n, const value_type& val);
void assign (initializer_list<value_type> il);
push_backvoid push_back (const value_type& val);Add element at the end 
void push_back (value_type&& val);
pop_backvoid pop_back();Delete last element 
insertiterator insert (const_iterator position, const value_type& val);Insert elements. Return value: An iterator that points to the first of the newly inserted elements.
iterator insert (const_iterator position, size_type n, const value_type& val);
template <class InputIterator>
iterator insert (const_iterator position, InputIterator first, InputIterator last);
iterator insert (const_iterator position, value_type&& val);
iterator insert (const_iterator position, initializer_list<value_type> il);
eraseiterator erase (const_iterator position);Erase elements 
iterator erase (const_iterator first, const_iterator last);
swapvoid swap (vector& x);Swap content 
clearvoid clear() noexcept;Clear content 
emplace template <class...   Args>
iterator emplace (const_iterator position, Args&&... args);
Construct and insert   element 
emplace_back template <class...   Args>
void emplace_back (Args&&... args);
Construct and insert element at   the end 

   示例:

 示例代码

  • Allocator相关
函数函数原型功能描述
get_allocatorallocator_type get_allocator()   const noexcept;Get allocator

重载的非成员函数

函数函数原型功能描述
relational operatorstemplate <class T, class Alloc>
bool operator== (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs);
Relational operators for vector
template <class T, class Alloc>
bool operator!= (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs);
template <class T, class Alloc>
bool operator<  (const   vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs);
template <class T, class Alloc>
bool operator<= (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs);
template <class T, class Alloc>
bool operator>  (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs);
template <class T, class Alloc>
bool operator>= (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs);
swaptemplate <class T, class Alloc>
void swap (vector<T,Alloc>& x, vector<T,Alloc>& y);
Exchange contents of vectors

特例

函数函数原型功能描述
vector<bool>template <class T, class   Alloc = allocator<T> > class vector; // generic template
template <class Alloc> class vector<bool,Alloc>;   // bool specialization
Vector of bool

   注:vector<bool>使用不慎可能会出问题,一般都建议不要使用。

翻译、参考:

  http://www.cplusplus.com/reference/vector/vector/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值