zoukankan      html  css  js  c++  java
  • C++ STL vector(向量容器)的使用(附完整程序代码)

    一、简单介绍

    Vectors 包括着一系列连续存储的元素,其行为和数组类似。

    訪问Vector中的随意元素或从末尾加入元素都能够在O(1)内完毕,而查找特定值的元素所处的位置或是在Vector中插入元素则是O(N)。

    Constructors 构造函数
    Operators 对vector进行赋值或比較
    assign() 对Vector中的元素赋值
    at() 返回指定位置的元素
    back() 返回最末一个元素
    begin() 返回第一个元素的迭代器
    capacity() 返回vector所能容纳的元素数量(在不又一次分配内存的情况下)
    clear() 清空全部元素
    empty() 推断Vector是否为空(返回true时为空)
    end() 返回最末元素的迭代器(译注:实指向最末元素的下一个位置)
    erase() 删除指定元素
    front() 返回第一个元素
    get_allocator() 返回vector的内存分配器
    insert() 插入元素到Vector中
    max_size() 返回Vector所能容纳元素的最大数量(上限)
    pop_back() 移除最后一个元素
    push_back() 在Vector最后加入一个元素
    rbegin() 返回Vector尾部的逆迭代器
    rend() 返回Vector起始的逆迭代器
    reserve() 设置Vector最小的元素容纳数量
    resize() 改变Vector元素数量的大小
    size() 返回Vector元素数量的大小
    swap() 交换两个Vector
    二、完整程序代码

    /*请务必执行下面程序后对比阅读*/
    
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <stdexcept>
    using namespace std;
    
    void print(int num)
    {
    	cout << num << " ";
    }
    
    int main()
    {
    	//1. 初始化
    	vector<int> v;
    	vector<int>::iterator iv;
    
    	v.reserve(100);//设置vector最小的元素容纳数量
    	v.assign(10, 2);//将10个值为2的元素赋到vector中
    	cout << v.capacity() << endl; //返回vector所能容纳的元素数量(在不又一次分配内存的情况下)
    	cout << v.size() << endl; //返回Vector实际含有的元素数量
    	cout << endl;
    
    	//2. 加入
    	//注意:push_front()仅仅适用于list和deque容器类型
    	for (int i = 0; i < 10; i++)
    		v.push_back(i);
    	for_each(v.begin(), v.end(), print);//须要#include <algorithm>
    	cout << endl;
    	cout << v.size() << endl;
    	cout << endl;
    
    	//3. 插入及遍历、逆遍历
    	v.insert(v.begin() + 3, 99);
    	v.insert(v.end() - 3, 99);
    	for_each(v.begin(), v.end(), print);
    	cout << endl;
    	for_each(v.rbegin(), v.rend(), print);//在逆序迭代器上做++运算将指向容器中的前一个元素
    	cout << endl;
    
    	//一般遍历写法
    	for(iv = v.begin(); iv != v.end(); ++iv)
            cout << *iv << " ";
        cout << endl;
        cout << endl;
    
    	//4. 删除
    	v.erase(v.begin() + 3);
    	for_each(v.begin(), v.end(), print);
    	cout << endl;
    	v.insert(v.begin() + 3, 99);//还原
    
    	v.erase(v.begin(), v.begin() + 3); //注意删除了3个元素而不是4个
    	for_each(v.begin(), v.end(), print);
    	cout << endl;
    
    	//注意:pop_front()仅仅适用于list和deque容器类型
    	v.pop_back();
    	for_each(v.begin(), v.end(), print);
    	cout << endl;
    	cout << endl;
    
    	//5. 查询
    	cout << v.front() << endl;
    	cout << v.back() << endl;
    
    	//危急的做法,但一般我们就像訪问数组那样操作即可
    	for (int i = 15; i < 25; i++)
    		cout << "Element " << i << " is " << v[i] << endl;
    	//安全的做法
    	int i;
    	try
    	{
    		for (i = 15; i < 25; i++)
    			cout << "Element " << i << " is " << v.at(i) << endl;
    	}
    	catch (out_of_range err)//#include <stdexcept>
    	{
    		cout << "out_of_range at " << i << endl;
    	}
    	cout << endl;
    
    	//6. 清空
    	v.clear();
    	cout << v.size() << endl;//0
    	for_each(v.begin(), v.end(), print); //已经clear。v.begin()==v.end()。不会有不论什么结果。

    return 0; }

    三、补充

    vector应该说是在STL中使用最广泛的容器。

    大家知道。数组是差点儿每一种语言都拥有的底层数据结构,但在我们的工作中,我们会大量的使用数组来表示同一类事物的一个集合。而vector实质上就是一个能够存储不论什么元素的动态数组。

    vector尽管不是一个低级的数据结构,可是它各个操作的效率差点儿是和数组同样的。仅仅是它会使用比普通数组很多其它的空间。由于在vector由于空间不足而须要又一次分配空间的时候。它通常会分配很多其它的空间(可能是当前size的1.5倍,这个是由详细实现定义的),以免每次插入一个新的元素的时候,都会又一次分配空间。

    又一次分配空间是vector里面最没有效率的操作,所以在使用vector的时候要尽量避免又一次分配空间。详细的方法是依据自己的实际须要来设定vector的capacity大小。


    关于vector与list的具体比較。请參考这篇文章


    參考站点:http://www.cplusplus.com/reference/vector/vector/




  • 相关阅读:
    Convolution_model_Application_v1a
    MBSE基于模型的系统工程
    Convolution_model_Step_by_Step_v2a
    深度学习精炼图笔记总结
    TensorFlow_Tutorial_v3b——improving NN performance测验
    maven之安装jar包之本地仓库
    linux之rpm管理
    linux之防火墙
    linux之ntp服务
    linux之chkconfig
  • 原文地址:https://www.cnblogs.com/claireyuancy/p/7382467.html
Copyright © 2011-2022 走看看