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. python正则表达式+正则大量实例

    正则表达式 正则表达式内部函数详解http://www.runoob.com/python/python-reg-expressions.html 正则表达式是一个特殊的字符序列,它能帮助你方便的检查 ...

  2. ansible自动化运维入门

    1.ansible的安装 1)使用源码安装Python3.5 安装支持包 yum -y install lrzsz vim net-tools gcc gcc-c++ ncurses ncurses- ...

  3. PHP中文乱码分类及解决办法大全

    PHP+MYSQL做网站开发通常都会碰到浏览器输出中文字符时乱码,这个问题的原因主要是因为HTML内容编码,PHP文件编码和MySQL数据库编码这三者不一致造成的.下面我们以UTF-8为例简述一下如何 ...

  4. java程序——两数的加减乘除

    import javax.swing.JOptionPane; // import class JOptionPane public class Elementary { public static ...

  5. stm32--FatFs移植(SPIFlash)

    前言 硬件: 单片机:stm32f072CB,sram大小16k.(其他单片机只要sram>8k即可通用) SPIFlash:W25Q128FV,16Mbyte,单次擦除最小4k. 程序使用Ke ...

  6. ArcGIS Server远程处理服务器(环境设置)

    当使用ArcGIS Server做远程处理服务器执行影像处理操作时,提示ERROR 999999通用错误代码,如下: Start Time: Mon Jul 03 13:49:06 2017Distr ...

  7. (原)MongoDB在系统中的使用

    序)Nosql并不是要取代原有的数据产品,而是为不同的应用场景提供更多的选择. 一)结构类型 传统数据库的领域在于结构化文档,对于非结构化文档和半结构化文档,它能处理,但是有一定的缺陷,那么什么又是结 ...

  8. Python 3基础教程23-多维列表

    这里简单举例一个多维列表,多维看起来都很晕. # 多维列表 x = [ [5,6],[6,7],[7,2] ,[2,5] ,[4,9]] print(x) # 根据索引引用列表元素,例如打印[6,7] ...

  9. 把python脚本打包成win可执行文件

    前几天有个朋友找我写一点小东西,写好后把代码发他帮他搞了半天,结果愣是没听懂,就找到了这个办法. 1.导入pyinstaller包, pip install pyinstaller 2.进入到你需要打 ...

  10. Python简要标准库(5)

    hashlib Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 基本的生成MD密匙的函数 import hashlib md5 = hashlib.md5() md5.up ...