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. 洛谷 T51922 父子

    题目描述 对于全国各大大学的男生寝室,总是有各种混乱的父子关系. 那么假设现在我们一个男生寝室有不同的 nn 个人,每个人都至多有一个“爸爸”,可以有多个“儿子”,且有且只有一个人没有“爸爸”(毕竟是 ...

  2. ruby 数据类型Symbol

    一.符号创建 符号是Symbol类的实例,使用冒号加一个标识符即可创建符号 :a :"This is a symno" 二.符号字符串相互转换 p :symbol.to_s #=& ...

  3. python 面向对象 (多态)

    什么是多态?多态就像是人有多种心情,场景不一样心情就会不一样. class Dog: def print_self(self): print('this is dog') class Hsq(Dog) ...

  4. 插头DP(基于连通性状态压缩的动态规划问题)(让你从入门到绝望)

    今天,我,Monkey king 又为大家带来大(ju)佬(ruo)的算法啦!--插头DP 例题(菜OJ上的网址:http://caioj.cn/problem.php?id=1489): 那么,这道 ...

  5. 关于xampp 集成开发包电脑重启mysql无法启动的问题

    关于xampp 集成开发包电脑重启mysql无法启动的问题. 在做php开发时,安装过xampp,也不知道是版本老了还是什么问题,总是出现当天晚上下班关机,第二天上班mysql不能启动,在网上查找些资 ...

  6. php简易实现计划任务

    index.php <?php function ceshi(){ $wan = file_get_contents('./wangt_index.txt',true); $jifen = $w ...

  7. 小议Android多进程以致Application多次初始化

    最近遇到一个bug,当应用加了多进程后,比如总共进程数为N,会出现在`startService()`时`onStartCommand()`方法会被重复调用`(N-1)`次的奇怪现象. ***## 祸起 ...

  8. 子串查询(二维前缀数组) 2018"百度之星"程序设计大赛 - 资格赛

    子串查询 Time Limit: 3500/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Subm ...

  9. 图的同构 (Graph Isomorphism)

    整理摘自:https://www.jianshu.com/p/c33b5d1b4cd9 同构是在数学对象之间定义的一类映射,它能揭示出在这些对象的属性或者操作之间存在的关系.若这两个数学结构之间存在同 ...

  10. js保留两位小数,不四舍五入

    //不进行四舍五入,保留两位小数 function getKeepTwoDecimals(val) { var newVal = (parseInt(val * 100) / 100).toFixed ...