//输出尾巴的元素
cout<<vec.back();
//定义vector迭代器
vector<int>::iterator ite=vec.begin();
for(ite;ite!=vec.end();ite++)
cout<<*ite<<endl;
//增加元素
vec.insert(vec.begin()+,);//下标为2的位置添加12.
vec.insert(vec.begin()+,,);//下标为2的位置往后添加5个2;
//尾删除
vec.pop_back();
//删除某个特定元素
vec.erase(vec.begin()+)//删除下标为3的元素
//全部删除
vec.clear();
//改
*ite=;//通过迭代器修改
vec[]=;//通过下标修改
for(int i=;i<vec.size();i++)
cout<<vec[i]<<endl;
//调用sort
sort(vec.begin(),vec.end());//放vec的头迭代器与尾迭代器,从头排到尾
//随机函数
srand((unsigned int) time());//种种子
#include<iostream>
#include<vector>
using namespace std;
int main()
{
//申明6个有效元素,类似a[6]。
vector<int> vec();
//初始化,用6去初始化vec当中的5个元素
vector<int> vec(,);
//放一个结构体进去
struct stu{ };
vector<struct stu> vec;
//输出,vector本质是一个动态的数组,所以可以下标运算
cout<<vec[]<<endl; //输出为0;
//用另一个vector去初始化
vector<int> vec2(vec);
for(int i=;i<;i++)
cout<<vec2[i]<<endl;
vector的迭代器
vector<int>::iterator ite;
ite=vec.begin();
for(int i=;ite!=vec.end();i++)
cout<<*ite++<<endl;
//增加容器容量
vector<int> vec3();//容量为0
vec3.push_back();//增加一个值为2的元素进去,设原先容量为D,现在容量为2*D(devc++);
cout<<vec3.capacity()<<endl;
//容器当中有效元素的个数
vec3.size();
//判断容器当中是否有元素
vec3.empty();//非空返回0,空返回1 return ;
}
还没刷题,赶紧溜了

c++ vector常见用法的更多相关文章

  1. STL vector常见用法详解

    <算法笔记>中摘取 vector常见用法详解 1. vector的定义 vector<typename> name; //typename可以是任何基本类型,例如int, do ...

  2. C++序列容器之 vector常见用法总结

    一.关于vector 本文默认读者具有一定的c++基础,故大致叙述,但保证代码正确. vector是一个动态的序列容器,相当于一个size可变的数组. 相比于数组,vector会消耗更多的内存以有效的 ...

  3. c++ 中vector 常见用法(给初学者)

    c++ 中 vector vector有两个参数,一个是size,表示当前vector容器内存储的元素个数,一个是capacity,表示当前vector在内存中申请的这片区域所能容纳的元素个数. ca ...

  4. vector常见用法

    #include <boost/foreach.hpp> #include <iostream> #include <vector> #include <bo ...

  5. C++标准模板库(STL)——vector常见用法详解

    vector的定义 vector<typename> name; 相当于定义了一个一维数组name[SIZE],只不过其长度可以根据需要进行变化,比较节省空间,通俗来讲,vector就是& ...

  6. PAT A1039、A1047——vector常见用法

    vector 常用函数实例 (1)push_back() (2)pop_back() (3)size() (4)clear():清空vector中所有元素 (5)insert():insert(it, ...

  7. C++学习二 vector的用法(使用sort对于vector排序)

    一.vector的介绍 vector是C++里面的一个容器,也是我们数学上面理解的向量,有一些比较常见的操作. 二.vector的定义 #include<vector> using nam ...

  8. STL priority_queue 常见用法详解

    <算法笔记>学习笔记 priority_queue 常见用法详解 //priority_queue又称优先队列,其底层时用堆来实现的. //在优先队列中,队首元素一定是当前队列中优先级最高 ...

  9. STL string 常见用法详解

    string 常见用法详解 1. string 的定义 //定义string的方式跟基本数据类型相同,只需要在string后跟上变量名即可 string str; //如果要初始化,可以直接给stri ...

随机推荐

  1. 笔记 : 将本地项目上传到GitHub

    一.准备工作 1. 注册github账号https://github.com, 安装git工具 https://git-for-windows.github.io/ 2. 创建SSH KEY(由于本地 ...

  2. DataGridView常用属性和方法

    DataGridView常用属性: 只读属性设定    datagridview.ReadOnly = True 行自动追加    datagridview.AllowUserToAddRows = ...

  3. MongoDB下,启动服务

    D:\MongoDB>mongod --dbpath D:\MongoDB\Data --logpath D:\MongoDB\Log\MongoDB.log --logappend --ser ...

  4. caffe中的caffemodel参数提取方法

    需要的文件为:deploy.prototxt caffemodel net = caffe.Net(deploy.txt,caffe_model,caffe.TEST)具体代码: import caf ...

  5. Java IO/NIO教程

    Java IO教程 http://tutorials.jenkov.com/java-io/index.html Java NIO教程 英文版: http://tutorials.jenkov.com ...

  6. 八 原型prototype和__proto__

    先来看一个实例 function Foo() { } var foo = new Foo(); console.log(foo.prototype);// undefined console.log( ...

  7. 18. 4Sum(双指针)

    Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...

  8. Keep On Movin (贪心)

    #include<bits/stdc++.h> using namespace std; int main(){ int T, n, a;scanf("%d",& ...

  9. libvirt_python

    一.Connections 连接函数接口libvirt.open(name); //可读写方式连接上QEMU 参数说明: name:连接名称libvirt.openAuth(uri, auth, fl ...

  10. oracle查询每隔5分钟区间内的数据量

    SELECT COUNT (DISTINCT tmp.PLATE) totalNum, tmp.newTime FROM ( SELECT T .LICENSE_PLATE plate, TO_CHA ...