C++ vector基本操作

一、什么是vector?

向量(Vector)是一个封装了动态大小数组的顺序容器(Sequence Container)。跟任意其它类型容器一样,它能够存放各种类型的对象。可以简单的认为,向量是一个能够存放任意类型的动态数组。

二、容器特性

1.顺序序列

顺序容器中的元素按照严格的线性顺序排序。可以通过元素在序列中的位置访问对应的元素。

2.动态数组

支持对序列中的任意元素进行快速直接访问,甚至可以通过指针算述进行该操作。操供了在序列末尾相对快速地添加/删除元素的操作。

3.能够感知内存分配器的(Allocator-aware)

容器使用一个内存分配器对象来动态地处理它的存储需求。

三、基本函数

1.构造函数

  • vector<int>a(10); //定义具有10个整型元素的向量(尖括号为元素类型名,它可以是任何合法的数据类型)
  • vector<int>a(10,1); //定义具有10个整型元素的向量,且给出的每个元素初值为1
  • vector<int>a(b); //用向量b给向量a赋值,a的值完全等价于b的值
  • vector<int>a(b.begin(),b.begin+3); //将向量b中从0-2(共三个)的元素赋值给a,a的类型为int型
  • int b[7]={1,2,3,4,5,6,7}; vector<int> a(b,b+7); //从数组中获得初值

2.成员函数

#include<vector>
vector<int> a,b;
a.assing(b.begin(),b.begin()+3); //b为向量,将b的0-2个元素赋值给向量a
a.assing(4,2);                   //a含有4个值为2的元素
a.at(i)                          //返回对向量中位置n处元素的引用。
a.back();                        //返回对向量中最后一个元素的引用。
a.begin();                       //返回指向向量中第一个元素的迭代器。  
a.capacity();                    //返回a在内存中总共可以容纳的元素个数   
a.clear();                       //清空a中的元素       。
a.empty();                       //判断a是否为空,空则返回true,非空则返回false
a.end();                         //返回引用向量容器中的past-the-end元素的迭代器。
a.erase(a.begin()+5);            //删除第6个元素
a.erase(a.begin(),a.begin()+3);  //删除前3个元素
a.front();                       //返回向量中第一个元素的引用
a.max_size();                    //返回向量可以容纳的最大元素数
a.pop_back();                    //删除向量中的最后一个元素,从而将容器大小减小1
a.push_back(5);                  //在a的最后一个向量后插入一个元素,其值为5
a.rbegin();                      //返回指向向量中最后一个元素的反向迭代器(即其反向开始)
a.rend();                        //返回一个反向迭代器,指向向量中第一个元素之前的理论元素
a.resize(10);                    //将a的现有元素个数调整至10个,多则删,少则补,其值随机
a.resize(10,2);                  //将a的现有元素个数调整至10个,多则删,少则补,其值为2
a.reserve(100);                  //将a的容量扩充至100
a.shrink_to_fit();               //要求容器减少其容量以适合其尺寸
a.size();                        //返回a中元素的个数
a.swap(b);                       //b为向量,将a中的元素和b中的元素整体交换
a.insert(a.begin()+1,5);         //在a的第一个元素(从第0个算起)位置插入数值5
a.insert(a.begin()+1,3,5);       //在a的第一个元素(从第0个算起)位置插入3个数,其值都为5
//b为数组,在a的第一个元素(从第0个元素算起)的位置插入b的第三个元素到第5个元素(不包括b+6)
a.insert(a.begin()+1,b+3,b+6);

3.其他成员函数

vector::get_allocator

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

vector::operator=

Assigns new contents to the container, replacing its current contents, and modifying its size accordingly.

  • copy (1) vector& operator= (const vector& x);

The copy assignment (1) copies all the elements from x into the container (with x preserving its contents).

      /**
       *  @brief  %Vector assignment operator.
       *  @param  __x  A %vector of identical element and allocator types.
       *
       *  All the elements of @a __x are copied, but any extra memory in
       *  @a __x (for fast expansion) will not be copied.  Unlike the
       *  copy constructor, the allocator object is not copied.
       */
      vector&
      operator=(const vector& __x);
  • move (2) vector& operator= (vector&& x);

The move assignment (2) moves the elements of x into the container (x is left in an unspecified but valid state).

      /**
       *  @brief  %Vector move assignment operator.
       *  @param  __x  A %vector of identical element and allocator types.
       *
       *  The contents of @a __x are moved into this %vector (without copying,
       *  if the allocators permit it).
       *  @a __x is a valid, but unspecified %vector.
       */
      vector&
      operator=(vector&& __x) noexcept(_Alloc_traits::_S_nothrow_move())
      {
        constexpr bool __move_storage =
          _Alloc_traits::_S_propagate_on_move_assign()
          || _Alloc_traits::_S_always_equal();
        _M_move_assign(std::move(__x),
                       integral_constant<bool, __move_storage>());
	return *this;
      }
  • initializer list (3)  vector& operator= (initializer_list<value_type> il);

The initializer list assignment (3) copies the elements of il into the container.

      /**
       *  @brief  %Vector list assignment operator.
       *  @param  __l  An initializer_list.
       *
       *  This function fills a %vector with copies of the elements in the
       *  initializer list @a __l.
       *
       *  Note that the assignment completely changes the %vector and
       *  that the resulting %vector's size is the same as the number
       *  of elements assigned.  Old data may be lost.
       */
      vector&
      operator=(initializer_list<value_type> __l)
      {
	this->assign(__l.begin(), __l.end());
	return *this;
      }

 vector::operator[]

Access element

Returns a reference to the element at position n in the vector container.

A similar member function, vector::at, has the same behavior as this operator function, except that vector::at is bound-checked and signals if the requested position is out of range by throwing an out_of_range exception.

Portable programs should never call this function with an argument n that is out of range, since this causes undefined behavior.

     // element access
      /**
       *  @brief  Subscript access to the data contained in the %vector.
       *  @param __n The index of the element for which data should be
       *  accessed.
       *  @return  Read/write reference to data.
       *
       *  This operator allows for easy, array-style, data access.
       *  Note that data access with this operator is unchecked and
       *  out_of_range lookups are not defined. (For checked lookups
       *  see at().)
       */
      reference
      operator[](size_type __n) _GLIBCXX_NOEXCEPT
      { return *(this->_M_impl._M_start + __n); }

      /**
       *  @brief  Subscript access to the data contained in the %vector.
       *  @param __n The index of the element for which data should be
       *  accessed.
       *  @return  Read-only (constant) reference to data.
       *
       *  This operator allows for easy, array-style, data access.
       *  Note that data access with this operator is unchecked and
       *  out_of_range lookups are not defined. (For checked lookups
       *  see at().)
       */
      const_reference
      operator[](size_type __n) const _GLIBCXX_NOEXCEPT
      { return *(this->_M_impl._M_start + __n); }

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值