C++ vector基本用法
转自金河http://www.cnblogs.com/wang7/archive/2012/04/27/2474138.html
1 基本操作
(1)头文件#include<vector>.
(2)创建vector对象,vector<int> vec;
(3)尾部插入数字:vec.push_back(a);
(4)使用下标访问元素,cout<<vec[0]<<endl;记住下标是从0开始的。
(5)使用迭代器访问元素.
vector<int>::iterator it;
for(it=vec.begin();it!=vec.end();it++)
cout<<*it<<endl;
(6)插入元素: vec.insert(vec.begin()+i,a);在第i+1个元素前面插入a;
(7)删除元素: vec.erase(vec.begin()+2);删除第3个元素
vec.erase(vec.begin()+i,vec.end()+j);删除区间[i,j-1];区间从0开始
(8)向量大小:vec.size();
(9)清空:vec.clear();
2
vector的元素不仅仅可以使int,double,string,还可以是结构体,但是要注意:结构体要定义为全局的,否则会出错。下面是一段简短的程序代码:

#include<stdio.h>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std; typedef struct rect
{
int id;
int length;
int width; //对于向量元素是结构体的,可在结构体内部定义比较函数,下面按照id,length,width升序排序。
bool operator< (const rect &a) const
{
if(id!=a.id)
return id<a.id;
else
{
if(length!=a.length)
return length<a.length;
else
return width<a.width;
}
}
}Rect; int main()
{
vector<Rect> vec;
Rect rect;
rect.id=1;
rect.length=2;
rect.width=3;
vec.push_back(rect);
vector<Rect>::iterator it=vec.begin();
cout<<(*it).id<<' '<<(*it).length<<' '<<(*it).width<<endl; return 0; }

3 算法
(1) 使用reverse将元素翻转:需要头文件#include<algorithm>
reverse(vec.begin(),vec.end());将元素翻转(在vector中,如果一个函数中需要两个迭代器,
一般后一个都不包含.)
(2)使用sort排序:需要头文件#include<algorithm>,
sort(vec.begin(),vec.end());(默认是按升序排列,即从小到大).
可以通过重写排序比较函数按照降序比较,如下:
定义排序比较函数:
bool Comp(const int &a,const int &b)
{
return a>b;
}
调用时:sort(vec.begin(),vec.end(),Comp),这样就降序排序。
4代码验证
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool Comp(const int &a,const int &b)
{
return a>b;
}
int main()
{
vector<int> vc;
int arr[]={,,,};
vc.push_back();
vc.push_back();
vc.push_back();
vc.push_back();
vc.insert(vc.begin()+,);
vc.erase(vc.begin()+);
reverse(vc.begin(),vc.end());
vector<int>::iterator it;
sort(vc.begin(),vc.end(),Comp);
sort(arr,arr+,Comp);
for(it=vc.begin();it!=vc.end();it++)
cout<<*it<<endl;
for(int i=;i<;i++)
{
cout<<arr[i]<<endl;
}
return ;
}
C++ vector基本用法的更多相关文章
- c++中vector的用法详解
c++中vector的用法详解 vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间 ...
- STL中的Vector相关用法
STL中的Vector相关用法 标准库vector类型使用需要的头文件:#include <vector>. vector 是一个类模板,不是一种数据类型,vector<int> ...
- C++学习二 vector的用法(使用sort对于vector排序)
一.vector的介绍 vector是C++里面的一个容器,也是我们数学上面理解的向量,有一些比较常见的操作. 二.vector的定义 #include<vector> using nam ...
- STL vector常见用法详解
<算法笔记>中摘取 vector常见用法详解 1. vector的定义 vector<typename> name; //typename可以是任何基本类型,例如int, do ...
- POJ 1230 Pass-Muraille#贪心+vector迭代器用法
(- ̄▽ ̄)-* (注意下面代码中关于iterator的用法,此代码借鉴某大牛) #include<iostream> #include<cstdio> #include< ...
- C++:vector的用法详解
原文地址:http://blog.csdn.net/hancunai0017/article/details/7032383 vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于 ...
- STL————vector的用法
一.什么是vector? 向量(Vector)是一个封装了动态大小数组的顺序容器(Sequence Container).跟任意其它类型容器一样,它能够存放各种类型的对象.可以简单的认为,向量是一个能 ...
- C++序列容器之 vector常见用法总结
一.关于vector 本文默认读者具有一定的c++基础,故大致叙述,但保证代码正确. vector是一个动态的序列容器,相当于一个size可变的数组. 相比于数组,vector会消耗更多的内存以有效的 ...
- C++ STL(二)vector的用法
##### vector的定义 ```#include <iostream>#include <string>#include <vector>using name ...
- vector基本用法
Vector作为STL容器中的一员,使用频率非常高,因此对其基本用法和实用技巧进行记录,便于后期查询使用. 基本用法 #include <iostream> #include <ve ...
随机推荐
- python 用法测试
Python 3.5.6 1.js风格的回调测试 def b(): ') def a(fn): if callable(fn): fn() a(b) class Sample: def q(self) ...
- 博客模板更新CSS
采用了作者#a的模板BlueSky进行了些许修改 在原有基础上加了三个样式,使页面显示风格统一些 #home{ background-color:#fff; } #main{ background-c ...
- 使用yum方式安装mysql5.6
1.新开的云服务器,需要检测系统是否自带安装mysql # yum list installed | grep mysql 2.如果发现有系统自带mysql,果断这么干 # yum -y remove ...
- 旋转VR相机不头晕:一个反直觉的发现
旋转VR相机不头晕:一个反直觉的发现 本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/deta ...
- 第九章 Servlet API
第九章 Servlet API Servlet API 定义了Servlet和服务器之间的一个标准接口,这使得Servlet具有跨应用服务器的特性,通过使用Servlet API,开发人员不必关心服务 ...
- echarts 柱状图和饼状图动态获取后台数据
运用echarts来实现图表 1.首先下载echarts包 http://echarts.baidu.com/echarts2/doc/example.html,在这里我下载的是 2.将echart ...
- [Tools] Using mobile device for debugging your mobile web site
1. First you have enable "Developer mode" on your mobile device. (Different device might b ...
- Windows下ElasticSearch及相关插件的安装
(1)在官网下载ElasticSearch压缩包.这里我下载的是elasticsearch-1.7.1(下载地址:https://download.elastic.co/elasticsearch/e ...
- [Struts2] No result defined for action ... and result input & Invalid field value for field ...
"No result defined for action ... and result input"错误一般发生在Struts2的拦截器拦截时遇到了问题时.Struts2会将跳转 ...
- simple_strtoul()分析
此函数有以下几点值得注意:1.第一个参数中的const.一般在函数的形参中,如果我们只是希望调用者使用该参数,而不会去改变该参数内容(一般是指针指向的内容),则可以声明为const.2.第二个参数.C ...