Vector容器详细总结

注 : 以 下 所 说 的 函 数 以 及 代 码 都 是 在 C + + 11 下 进 行 的 \color{red}注:以下所说的函数以及代码都是在C++11下进行的 C++11

一:什么是Vector

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

二:C++通过构造函数初始化的方式(常用)
  1. vector():创建一个空vector
  2. vector(int nSize):创建一个vector,元素个数为nSize
  3. vector(int nSize,const t& t):创建一个vector,元素个数为nSize,且值均为t
  4. vector(const vector&):复制构造函数
  5. vector(begin,end):复制[begin,end)区间内另一个数组的元素到vector中
#include <iostream>
#include <vector>
using namespace std;
int main(){
    vector<int>first;   //定义一个向量,大小为0。
    vector<int>second(100);  //定义一个向量,大小为100,默认值为0
    vector<int>third(4,100);   //定义一个向量,大小为4,值都是100
    vector<int>forth(third.begin()+1,third.end());  //将第三个数组区间[begin+1,end]复制给forth
    vector<int>fifth(forth); //将第四个数组复制构造给fifth

    int array[] = {1,2,3,4};
    vector<int>sixth(array,array+sizeof(array));  //通过数组复制构造
    vector<int>seventh = {1,2,3,4,5,6};  //ilist 初始化为列表中元素的拷贝,列表中元素必须与ilist的元素类型相容,本例中必须是与整数类型相容的类型,整形会直接拷贝,其他类型会进行类型转换。
    
    return 0;
}

其他的构造方法详见官方文档:C++vector

三:vector常用方法

首先粗略的看下官方文档中所给的方法:
在这里插入图片描述
下面具体讲解其中比较常用的方法

  1. assign
    1. void assign (InputIterator first, InputIterator last);
    2. void assign (size_type n, const value_type& val);
    3. void assign (initializer_list<value_type> il);
第一种:
vector<int>first;
vector<int>second(100);
first.assign(second.begin(),second.end());
ouput:1000

第二种:
vector<int>first;
first.assign(10,9);
output:9999999999

第三种:
vector<int>first;
first.assign({1,2,3,4,5,6,7,8,9});
output:123456789
  1. vector.begin();

Returns an iterator pointing to the first element in the vector.
返回指向向量中第一个元素的迭代器。

  1. vector.end();

Returns an iterator referring to the past-the-end element in the vector container.

  1. vector.push_back();

Adds a new element at the end of the vector, after its current last element. The content of val is copied (or moved) to the new element.
在向量的最后一个元素后面添加一个新元素。val的内容被复制(或移动)到新元素中。

vector<int>a;       //a现在为空
a.push_back(1)// a现在为{1};
  1. vector.pop_back();

Removes the last element in the vector, effectively reducing the container size by one.
This destroys the removed element.
移除向量中的最后一个元素,将容器大小减少一个。
移除的元素将会被销毁

vector<int>a() = {1,2,3,4};
a.pop_back();   //a现在为{1,2,3}
  1. vector.size()

返回容器的大小

vector<int>a() = {1,2,3,4};
a.size();   //4
  1. size,capacity,resize,reserve,max_size函数的理解
  2. vector.clear();

清空向量,向量大小size设为0,capacity不变

vector<int>a() = {1,2,3,4};
vector.clear();
a.size();   //4
  1. vector.swap(vector& x)

Exchanges the content of the container by the content of x, which is another vector object of the same type. Sizes may differ.
通过x的内容交换容器的内容,x是另一个相同类型的向量对象。尺寸不一定要相同。

vector<int>a(3,20)  //a现在是{20,20,20}
vector<int>b(2,30)  //b现在是{30,30}
a.swap(b);  //a现在是{30,30},b现在是{20,20,20}
  1. vector::operator=

Assigns new contents to the container, replacing its current contents, and modifying its size accordingly.
将新内容指派给容器,替换其当前内容,并相应地修改其大小。

vector<int>a = {1,2,3,4,5}; a通过“=”获得了值{12345}
vector<int>b = a;   b通过“=”获得了a的值{12345}
  1. vector.empty();

Returns whether the vector is empty (i.e. whether its size is 0).
判断是否为空,是就返回“true",不是就返回"false"

这个并不是清空vector,清空是vector.clear();

vector<int>a;
a.empty(); //true;
a.push_back(1);
a.empty(); //false;
  1. vector.at(n)

Returns a reference to the element at position n in the vector.
返回向量在n位置的元素

与” [ ] "不同的地方是,at会自动判定n是否越界,并会抛出out of range的异常,“ [ ] "不会。

vector<int>a;   //a现在为空
for (unsigned i=0; i<10; i++)
    a.at(i)=i;
 //a现在为{1,2,3,4,5,6,7,8,9}
  1. vector.rease(n) / vector.rease(begin,last)
    删除向量的第n个元素或者是删除向量的[begin,last]之间的元素。

Removes from the vector either a single element (position) or a range of elements ([first,last)).

This effectively reduces the container size by the number of elements removed, which are destroyed.

Because vectors use an array as their underlying storage, erasing elements in positions other than the vector end causes the container to relocate all the elements after the segment erased to their new positions. This is generally an inefficient operation compared to the one performed for the same operation by other kinds of sequence containers (such as list or forward_list).

  1. vector.front();

返回向量中第一个元素的引用,更改vector.front()的值可以直接改变向量的值

vector<int> myvector;

myvector.push_back(78);
myvector.push_back(16);
// now front equals 78, and back 16,myvector为{78,16}
myvector.front() -= myvector.back();
// now front equals 62, and back 16,myvector为{62,16}
  1. vector.back();

返回向量最后一个元素的引用,更改vector.back()的值可以直接改变向量的值

begin(),end(),front(),back()的区别
16.insert

  1. iterator insert (const_iterator position, const value_type& val);
  2. iterator insert (const_iterator position, size_type n, const value_type& val);
  3. iterator insert (const_iterator position, InputIterator first, InputIterator last);
  4. iterator insert (const_iterator position, value_type&& val);
  5. iterator insert (const_iterator position, initializer_list<value_type> il)

注意:insert的返回值是一个指向所插入的元素的第一个元素的迭代器

// inserting into a vector
#include <iostream>
#include <vector>

int main ()
{
    std::vector<int> myvector (3,100);
    std::vector<int>::iterator it;

    it = myvector.begin();
    it = myvector.insert ( it , 200 );
    std::cout<<*it;
    myvector.insert (it,2,300);

    // "it" no longer valid, get a new one:
    it = myvector.begin();

    std::vector<int> anothervector (2,400);
    myvector.insert (it+2,anothervector.begin(),anothervector.end());

    int myarray [] = { 501,502,503 };
    myvector.insert (myvector.begin(), myarray, myarray+3);

    std::cout << "myvector contains:";
    for (it=myvector.begin(); it<myvector.end(); it++)
        std::cout << ' ' << *it;
    std::cout << '\n';

    return 0;
}
  1. To be continue…
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值