http://classfoo.com/ccby/article/jnevK

Vector的存储空间是连续的,list不是连续存储的

vector初始化

    vector<int>v;            //不能使用下标索引赋值,因为还没有空间
vector<int>v1(, -);//初始化10个,初值为-1 int a[] = { , , , };
vector<int>v2(a, a + );

如果先定义,后赋值,使用assign

    vector<int> foo1(, );
    vector<int> foo2;
    vector<int> foo3;
    vector<int> foo4;
    //foo1.assign(7, 100); // 填充赋值(1),将7个值为100的整数赋值给foo1
    std::vector<int>::iterator it;
    it = foo1.begin() + ;
    foo2.assign(it, foo1.end() - ); // 范围赋值(2),将foo1中不包括头尾的元素赋值给foo2     int fooarray[] = { , , };
    foo3.assign(fooarray, fooarray + ); //范围赋值同样适用于数组
    foo4.assign({ , , , }); // 初始化列表赋值(3)

添加、删除、插入、清空、交换

vector <int>a;
a.push_back();   //在末尾加一个元素 size+1
a.pop_back();    //删除最后一个元素,size-1
---------------------------------------------------------------------

std::vector<int> foo1(3, 9);
std::vector<int> foo2(3, 9);
std::vector<int> foo3(3, 9);

std::vector<int>::iterator it;

// single element (1)
it = foo1.begin();
foo1.insert(it, 11); // {11,9,9,9}

// fill (2)
it = foo2.begin();
foo2.insert(it, 2, 7); // {7,7,9,9,9}

// range (3)
int fooarray[] = { 1, 2, 3, 4, 5 };
it = foo3.begin();
foo3.insert(it, fooarray + 1, fooarray + 4); // { 2,3,4,9,9,9}

---------------------------------------------------------------------

// 移除第2个元素
foo3.erase(foo3.begin() + 1);//{2,4,9,9,9}

// 移除前三个元素
foo3.erase(foo3.begin(), foo3.begin() + 3);//{9,9}

foo3.clear();//清空

------------------------------------------------

std::vector<int> foo(3, 100); 
std::vector<int> bar(5, 200); 
foo.swap(bar);

遍历

std::vector<int> foo(5);
std::vector<int>::reverse_iterator rit = foo.rbegin();
int i = 0;
for (rit = foo.rbegin(); rit != foo.rend(); ++rit)
*rit = ++i;
for (unsigned i = 0; i<foo.size(); i++)
std::cout << ' ' << foo[i];
for (std::vector<int>::iterator it = foo.begin(); it != foo.end(); ++it)
std::cout << ' ' << *it;
std::cout << std::endl;

vector 基础的更多相关文章

  1. vector基础

    //STL基础 //容器 //vector #include "iostream" #include "cstdio" #include "vecto ...

  2. vector基础操作

    //vector< T> vec; //构造一个名为vec的储存数据类型为T的动态数组.其中T为需要储存的数据类型 //初始时vec为空 //push_back 末尾添加一个元素 //po ...

  3. vector 基础2

    size  :返回有效元素个数 max_size  :返回 vector 支持的最大元素个数 resize  :改变有效元素的个数 capacity  :返回当前可使用的最大元素内存块数(即存储容量) ...

  4. 66)vector基础总结

    基本知识: 1)vector 样子  其实就是一个动态数组: 2)vector的基本操作: 3)vector对象的默认构造 对于类  添加到  容器中  要有  拷贝构造函数---> 这个注意 ...

  5. Android Vector曲折的兼容之路

    Android Vector曲折的兼容之路 两年前写书的时候,就在研究Android L提出的Vector,可研究下来发现,完全不具备兼容性,相信这也是它没有被广泛使用的一个原因,经过Google的不 ...

  6. java数据结构-Vector

    1 Vector基础实现为数组 object[] synchronized线程安全 2 扩容使用  System.arraycopy(original, 0, copy, 0,Math.min(ori ...

  7. C++【vector】用法和例子

    /*** * vector 基础api复习 * 8 AUG 2018 */ #include <iostream> #include <vector> using namesp ...

  8. Faster-RCNN tensorflow 程序细节

    tf-faster-rcnn github:https://github.com/endernewton/tf-faster-rcnn backbone,例如vgg,conv层不改变feature大小 ...

  9. [Java] 集合框架原理之一:基本结构与源码分析

    一.Collection Collection 接口定义了一些基本的方法: int size(); boolean isEmpty(); boolean add(E e); boolean addAl ...

随机推荐

  1. zabbix配置报警媒介-用户-动作-邮件脚本触发mailx邮件报警

    2018-09-16更新,新版本zabbix不需要使用脚本发送邮件,在zabbix web界面直接配置就可以 配置邮件参数,测试发送邮件 确认安装相关服务,centos7默认安装 [root@VM_1 ...

  2. docker理论基础

    Namespaces 命名空间(namespaces)是 Linux 为我们提供的用于分离进程树.网络接口.挂载点以及进程间通信等资源的方法.在日常使用 Linux 或者 macOS 时,我们并没有运 ...

  3. admin添加用户时报错:(1452, 'Cannot add or update a child row: a foreign key constraint fails (`mxonline`.`django_admin_l

    在stackoverflow找到答案: DATABASES = { 'default': { ... 'OPTIONS': { "init_command": "SET ...

  4. linux安装python并安装pip

    因为最近要在linux环境下进行python编程,所以就试着去安装了一下,但是网上关于python以及pip的安装说实话有点混乱,所以我今天就把前辈的经验再次总结一下,希望可以给大家提供帮助. pyt ...

  5. 【財務会計】BS科目とは・PL科目とは

    「BS科目」「PL科目」という言葉がありますが.聞いたことあるけどよくわからん!っていう人は多いと思います.なので.簡単にご説明を. BS科目は「いくらあるか」 「BS科目」は.「B/S科目」と書くこ ...

  6. MVC4+EF 列表数据不能绑定

    最新准备使用.net 的mvc+Ef来写个项目,开始一切顺利,到了数据绑定时出现了问题. 我的mvc视图引擎是Razor,后台提取数据的是Linq来处理,发现不管怎么样都不能绑定列表数据,可以将后台的 ...

  7. vue2018年5月报错No parser and no file path given

    mac电脑直接: rm -rf node_modules rm package-lock.json npm install npm install prettier@~1.12.1 执行完这四个命令, ...

  8. 3,SQL语句及数据库优化

       1,统一SQL语句的写法 对于以下两句SQL语句,程序员认为是相同的,数据库查询优化器认为是不同的. 所以封装成复用方法,用标准模板来控制. select*from dual select*Fr ...

  9. Spring框架中ModelAndView、Model、ModelMap的区别

    转自:http://blog.csdn.net/liujiakunit/article/details/51733211 1. Model Model 是一个接口, 其实现类为ExtendedMode ...

  10. CS229 1

    1.机器学习 机器学习是工具,具体应用到某个实际场景下,才是目的. 2.分类 a 监督学习,包括回归(regression),分类(classification).回归问题,数据可以是连续或者离散,分 ...