vector 基础

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

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

vector初始化

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

    int a[4] = { 1, 2, 3, 4 };
    vector<int>v2(a, a + 4);

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

    vector<int> foo1(7, 100);
    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() + 1;
    foo2.assign(it, foo1.end() - 1); // 范围赋值(2),将foo1中不包括头尾的元素赋值给foo2

    int fooarray[] = { 1, 2, 3 };
    foo3.assign(fooarray, fooarray + 3); //范围赋值同样适用于数组
    foo4.assign({ 1, 9, 8, 13 }); // 初始化列表赋值(3)

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

vector <int>a;
a.push_back(1);   //在末尾加一个元素 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;

时间: 2024-07-28 14:56:05

vector 基础的相关文章

vector基础

1 //STL基础 2 //容器 3 //vector 4 5 #include "iostream" 6 #include "cstdio" 7 #include "vector"//向量 8 #include "iterator"//迭代器 9 #include "numeric"//accunulate()求和算法需要 10 #include "algorithm"//revers

vector基础知识点整理(含盲点 以解决)

#include<iostream> using namespace std; #include"vector" //输出(放在main函数里面出错 为什么????????) //因为函数里面不能定义函数!!! void printV(vector<int> &obj) { for (int i = 0; i < obj.size(); i++) { cout << obj[i] << " "; } co

vector 基础2

size  :返回有效元素个数 max_size  :返回 vector 支持的最大元素个数 resize  :改变有效元素的个数 capacity  :返回当前可使用的最大元素内存块数(即存储容量) empty  :判断是否为空 reserve  :请求改变存储容量 shrik_to_fit  :请求移除未使用的存储空间 std::vector<int> foo; for (int i = 0; i<100; i++) foo.push_back(i); std::cout <&

STL— vector(基础)

基本摘抄自百度百科 1,使用条件 头文件   <vector>     和   using namespace std;  (因为他是属于 C++ 的) 2,  定义:简单地说,vector是一个能够存放任意类型的动态数组,能够增加和压缩数据.(度娘) 3,使用 : vector <int> c;     // 长度未知,未初始化 vector <int> c(10);     // 长度为 10 ,未初始化 vector <int> c(10,1); //

Android Vector曲折的兼容之路

Android Vector曲折的兼容之路 两年前写书的时候,就在研究Android L提出的Vector,可研究下来发现,完全不具备兼容性,相信这也是它没有被广泛使用的一个原因,经过Google的不懈努力,现在Vector终于迎来了它的春天. 在文章后面,会给出本文的Demo和效果图,并开源在Github Vector Drawable Android 5.0发布的时候,Google提供了Vector的支持.Vector Drawable相对于普通的Drawable来说,有以下几个好处: Ve

C++【vector】用法和例子

1 /*** 2 * vector 基础api复习 3 * 8 AUG 2018 4 */ 5 6 #include <iostream> 7 #include <vector> 8 using namespace std; 9 10 // 打印vec向量 11 void print(vector<int> vec, int index) 12 { 13 std::cout << "vec" << index <<

【转载】设计模式大集合

原文: 设计模式大集合 最近要做架构了,得把设计模式整理一遍.因为这玩意,设计时能用就用,所以得明白啥时候用.不过要是成本太高了,或者根本低耦合没意义,就没必要用.这里列出使用场合(仅限于设计阶段),而且这文章不是给没学过的人看的,复习用的. 另外,并行设计模式不在本文范围内. Creational Pattern:(创建类,描述怎样低耦合的创建对象) Abstract Factory: 抽象工厂.当程序中对象分为并行的不同族(例如gnome和KDE),创建特定族的对象用此模式(例如创建一个窗口

java web 开发三剑客 -------电子书

Internet,人们通常称为因特网,是当今世界上覆盖面最大和应用最广泛的网络.根据英语构词法,Internet是Inter + net,Inter-作为前缀在英语中表示“在一起,交互”,由此可知Internet的目的是让各个net交互.所以,Internet实质上是将世界上各个国家.各个网络运营商的多个网络相互连接构成的一个全球范围内的统一网,使各个网络之间能够相互到达.各个国家和运营商构建网络采用的底层技术和实现可能各不相同,但只要采用统一的上层协议(TCP/IP)就可以通过Internet

小猪猪逆袭成博士之C++基础篇(四)数组、指针、vector、迭代器

小猪猪逆袭成博士之C++基础篇(四) 关键词:数组,Vector. 一.数组与指针 数组相信大家学过C语言或者其他的语言都不陌生,简单的就是同一个变量类型的一组数据.例如:int a[10],意思就是从a开始有10个连续的int大小的空间.我们还是从初始化说起. 我们以数据类型int为例,当然也可由有很多的数据类型,可以是像int,double这种,也可以是自定义的类,一般的初始化方法有: int a[10]; int a[10]={0}; int a[3]={0,1,2}; 在前面的文章中,有