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字符串编码

    python默认编码 python 2.x默认的字符编码是ASCII,默认的文件编码也是ASCII. python 3.x默认的字符编码是unicode,默认的文件编码是utf-8. 中文乱码问题 无 ...

  2. 003---生成器 & 迭代器

    生成器 & 迭代器 列表生成式 现在有个需求,列表[1, 2, 3, 4, 5, 6, 7, 8, 9],将列表里的每个值加1. 二逼青年版 a = [1, 2, 3, 4, 5, 6, 7, ...

  3. Python3.6中PyInstaller不能对文件进行打包问题

    上篇文章<itchat和matplotlib的结合使用爬取微信信息>是用python爬取信息得到微信朋友的信息,并且用matplotlib统计信息进行画图,所以今天想将它打包成.exe可执 ...

  4. c/c++ 随机数生成

    当程序需要一个随机数时有两种情况下使用: 1.程序中只需使用一次随机数,则调用rand()函数即可 2.程序需要多次使用随机数,那么需要使用srand()函数生成随机数种子在调用rand()函数保证每 ...

  5. 基于Mysql-Proxy实现Mysql的主从复制以及读写分离(下)

    基于Mysql-Proxy实现Mysql的主从复制以及读写分离(下) 昨天谈到了Mysql实现主从复制,但由于时间原因并未讲有关读写分离的实现,之所以有读写分离,是为了使数据库拥有双机热备功能,至于双 ...

  6. Windows自带的磁盘填充命令

    一张不用了的SD卡要给别人,之前一直是手机使用的,担心有一些资料被恢复,想要将它内容清空.以前就知道数字公司有一个磁盘填充的工具,后来网上搜一搜发现Windows有一个自带的命令用于磁盘填充. 首先进 ...

  7. Vue学习(二):class与style绑定

    <!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml&q ...

  8. laravel跨域问题

    // 只有同源策略才允许发送cookies // header('Access-Control-Allow-Credentials:true'); 需要要index.php下开启 最近写登录图形验证码 ...

  9. Python全栈 正则表达式(概念、、语法、元字符、re模块)

    前言:        普通人有三件东西看不懂:医生的处方,道士的鬼符,程序员得正则表达式       什么是正则表达式? 正则表达式,又称规则表达式,英文名为Regular Expression,在代 ...

  10. C# 删除文件错误 access denied

    使用以下代码正常删除整个文件夹内容时,报错如下: if (backupPathDir.Exists) { System.IO.DirectoryInfo di = new DirectoryInfo( ...