STL—Vector简介
有关C++ STL 中的vector向量的用法(代码示例)
一、 简介
Vector是一个称为向量的顺序容器(不明白顺序容器与关联容器的可以Google)。
二、 特点
1. 动态(相当于一个动态数组);
2. 可用于存放各种数据结构(内置的,自定义的,当然也可以是vector)(当是vector时注意申明格式eg:vector<vector<Information>>与vector< vector<Information> >,前者错误,后者正确,两个‘>’要有空格);
3. 支持对vector元素的随即访问;
4. 以拷贝的方式用一个vector初始化另一个vector时,两个vector类型要一致;
5. 每一个vector的容量要比其大小相对较大或相等(eg:vec.capacity()>=vec.size())。
三、 常用函数接口
Vec.begin();//向量中的首个元素的地址,加n后表示第n个元素的地址(eg:vec.begin()+3);
Vec.end();//向量中最后一个元素的地址;
Vec.rbegin();//指向反序后的向量首元素;
Vec.rend();//指向反序后的尾元素;
vec.front();//返回vector中的第一元素的值
vec.back();//返回vector中的最后一个元素的值
Vec.insert();//向向量中任意位置插入一个或多个元素;
Vec.clear();//向量清空;
Vec.push_back();//将元素压入向量的尾部;
//Vec.push_front();//vector中无此用法(用了之后会造成元素的迁移,与vector设计的初衷相违背);
Vec.erase();//删除向量中任意一个或一段元素;
Vec.pop_back();//删除向量中最后一个元素;
Vec.size();//返回向量的大小
Vec.capacity();//返回向量容量的大小
Vec.empty();//向量为空返回false,不为空返回true;
Vec1.swap(vec2);//交换向量vec1和向量vec2;
四、 代码示例
示例一://vector元素为简单的元素,进行简单地操作
#include<iostream>
#include<iomanip>
#include<vector>
#include<string>
using namespace std;
template <typename T>//一个函数末班,用向量表示函数
void display(vector<T>&vec)
{
static int n=1;//设置一个静态变量,用来标记每次输出
cout<<n++<<'.'<<endl;
cout<<"the size of the vec is "<<vec.size()<<endl;
cout<<"the capacity of the vec is"<<vec.capacity()<<endl;
if(!vec.empty())
{
cout<<"the contents are:"<<endl;
for(int i=0;i<vec.size();i++)
{
cout<<setw(10)<<vec[i];
}
cout<<endl<<endl;
}
else
cout<<"this is a empty vector"<<endl<<endl;
}
const int size=5;
int main()
{
string str[size]={"love","you","my","c++","language"};
vector<string>vec1; vec1.insert(vec1.begin(),str,str+size);//从vec1.begin()(此时等于vec1.end())处插入所有str当中的字符
display(vec1);
cout<<vec1.front()<<endl;//返回第一个元素的值
cout<<vec1.front()+" VB.NET"<<endl;
cout<<vec1.back()<<endl;//返回最后一个元素的值
cout<<vec1.back()+" VB.NET"<<endl;
cout<<*(vec1.begin()+1)<<endl;//用cout流对象输出vecotor的元素
cout<<*(vec1.rbegin()+1)+" c#"<<endl;
cout<<vec1[1]<<endl<<endl;//用下标法输出vecotor元素
vec1.clear();//清空向量vec1
display(vec1);
vec1.insert(vec1.begin(),str+2,str+size-1);//可以插入任意个元素(此处为2个)
display(vec1);
vec1.insert(vec1.begin()+1,3,"java");//从vec1.begin()+1处插入3个“Java”字符串
display(vec1);
vec1.insert(vec1.begin()+3,3,str[2]);//从vec1.begin()+3处插入3个str[2]字符串
display(vec1);
vector <string>vec2(vec1);//调用拷贝构造函数,以vec1初始化vec2
display(vec2);
vec2.clear();
vec2.insert(vec2.end(),vec1.begin(),vec1.end());
display(vec2);
vec2.erase(vec2.begin()+3,vec2.begin()+6);//删除vec2.begin()+3到vec2.begin()+6之间的字符串
display(vec2);
for(int i=0;i<5;i++)
{
vec2.pop_back();//删除最后一个字符串
display(vec2);
}
return 0;
}
示例二:vector元素为struct
#include<iostream>
#include<string>
#include<vector>
#include<iomanip>
using namespace std;
const int n=6;
struct Information
{
string studentName;
string studentSex;
double studentRank;
friend ostream &operator<<(ostream& out,Information &p);
friend istream &operator>>(istream& in,vector<Information>&p);
};
istream &operator>>(istream& in,vector<Information>&p)//重载输入用算符,不是此blog重点,在主函数中未使用
{
for(int i=0;i<n;i++)
in>>p[i].studentName>>p[i].studentSex>>p[i].studentRank;
return in;
}
ostream &operator<<(ostream& out,vector<Information>&p)//重载输出用算符,可以看到有与display()函数同样的效果
{
for(int i=0;i<n;i++)
out<<"name: "<<setw(10)<<p[i].studentName<<" sex: "<<setw(2)<<p[i].studentSex<<" rank: "<<setw(5)<<p[i].studentRank<<endl;
return out;
}
void display(vector<Information>&Student)//显示排名情况
{
if(!(Student.empty()))
{
for(int i=0;i<Student.size();i++)
{
cout<<"name: "<<setw(10)<<Student[i].studentName<<" sex: "<<setw(5)<<Student[i].studentSex<<" rank: "<<setw(5)<<Student[i].studentRank<<endl;
}
}
else
cout<<"this is a empty vector"<<endl;
}
int main()
{
string name[n]={"ZhangSan","liSi","WangMaZi","XiaoMing","XiaoHua","XiaoZhang"};
vector<string>Name;
vector<string>Sex(n,"\0");
vector<int>Rank(n,0);//初始化一个大小为n,元素都为零的向量
Name.insert(Name.begin(),name,name+n);
for(int i=0;i<n;i++)
{
cout<<"Please input "<<Name[i]<<"'s sex: ";
cin>>Sex[i];
cout<<"Please input "<<Name[i]<<"'s rank(0~100): ";
cin>>Rank[i];//用流对象cin向向量赋值
}
vector<Information>Student1;
//cin>>Student;
for(int i=0;i<n;i++)
{
Information temp;
temp.studentName=Name[i];
temp.studentSex=Sex[i];
temp.studentRank=Rank[i];
Student1.push_back(temp);
}
cout<<"the rank of student1 is: "<<endl;
display(Student1);
cout<<endl<<Student1;
vector<Information>Student2(Student1);
cout<<endl<<"the rank of student2 set is: "<<endl;
display(Student2);
vector<Information>Student3(Student2.begin(),Student2.end()-3);
cout<<endl<<"the size of student3 is: "<<Student3.size()<<endl;
Student3.swap(Student2);
cout<<"the size of student2 is: "<<Student2.size()<<endl;
cout<<"the size of student3 is: "<<Student3.size()<<endl;
Student3.pop_back();
cout<<"Student3's size is: "<<setw(5)<<Student3.size()<<endl;
Student3.erase(Student3.begin());
cout<<"Student3's size is: "<<setw(5)<<Student3.size()<<endl;
display(Student3);
return 0;
}
示例三:vector元素为vector
#include<iostream>
#include<string>
#include<vector>
#include<iomanip>
#include<windows.h>
using namespace std;
const int n=6;
static int N1=1;
static int N2=N1;
struct Information
{
string studentName;
string studentSex;
double studentRank;
friend ostream &operator<<(ostream& out,Information &p);
friend istream &operator>>(istream& in,vector<Information>&p);
};
ostream &operator<<(ostream& out,vector<Information>&p)//重载输出用算符,可以看到有与display()函数同样的效果
{
cout<<'('<<N1++<<')'<<endl;
for(int i=0;i<n;i++)
out<<"name: "<<setw(10)<<p[i].studentName<<" sex: "<<setw(5)<<p[i].studentSex<<" rank: "<<setw(5)<<p[i].studentRank<<endl;
return out;
}
ostream &operator<<(ostream& out,vector< vector<Information> >&p)
{
cout<<N2++<<": "<<endl;
for(int i=0;i<p.size();i++)
{
cout<<p[i];
cout<<endl;
}
return out;
}
int main()
{
string name[n]={"ZhangSan","liSi","WangMaZi","XiaoMing","XiaoHua","XiaoZhang"};
string sex[n]={"boy","girl","boy","girl","boy","girl"};
int _rank[n]={1,2,3,4,5,6};
vector<string>Name;
vector<string>Sex;
vector<int>Rank;
Name.insert(Name.begin(),name,name+n);
Sex.insert(Sex.begin(),sex,sex+n);
Rank.insert(Rank.begin(),_rank,_rank+n);
vector<Information>Student;
for(int i=0;i<n;i++)
{
Information temp;
temp.studentName=Name[i];
temp.studentSex=Sex[i];
temp.studentRank=Rank[i];
Student.push_back(temp);
}
cout<<Student<<endl;
N1=1;
system("pause");
vector< vector<Information> >VEC;
for(int i=0;i<n;i++)
VEC.push_back(Student);
string name1[n]={"ZhangSan1","liSi2","WangMaZi3","XiaoMing4","XiaoHua5","XiaoZhang6"};
string sex1[n]={"boy1","girl2","boy3","girl4","boy5","girl6"};
int _rank1[n]={11,22,33,44,55,66};
vector<string>Name1;
vector<string>Sex1;
vector<int>Rank1;
Name1.insert(Name1.begin(),name1,name1+n);
Sex1.insert(Sex1.begin(),sex1,sex1+n);
Rank1.insert(Rank1.begin(),_rank1,_rank1+n);
vector<Information>Student1;
for(int i=0;i<n;i++)
{
Information temp;
temp.studentName=Name1[i];
temp.studentSex=Sex1[i];
temp.studentRank=Rank1[i];
Student1.push_back(temp);
}
cout<<VEC;
N1=1;
VEC.insert(VEC.begin()+2,2,Student1);
cout<<VEC;
N1=1;
VEC.erase(VEC.begin()+3);
cout<<VEC;
N1=1;
VEC.pop_back();
cout<<VEC;
//.......
return 0;
}
更多数据结构不再列举
本文参考了部分网络上的代码示例,加入了笔者自己的一些代码,如有不妥之处,肯请读者指正。
STL—Vector简介的更多相关文章
- STL的简介
Standard Template Library,(标准模板库)<来自百度百科的整理> ————可复用性(reusability) STL是基于模板,内联函数的使用使得生成的代码短小高效 ...
- vector 简介
vector简介 vector是STL中最常见的容器,它是一种顺序容器,支持随机访问.vector是一块连续分配的内存,从数据安排的角度来讲,和数组极其相似, 不同的地方就是:数组是静态分配空间,一旦 ...
- C++ STL vector容器学习
STL(Standard Template Library)标准模板库是C++最重要的组成部分,它提供了一组表示容器.迭代器.函数对象和算法的模板.其中容器是存储类型相同的数据的结构(如vector, ...
- STL vector
STL vector vector是线性容器,它的元素严格的按照线性序列排序,和动态数组很相似,和数组一样,它的元素存储在一块连续的存储空间中,这也意味着我们不仅可以使用迭代器(iterator)访问 ...
- STL vector用法介绍
STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和f ...
- STL vector+sort排序和multiset/multimap排序比较
由 www.169it.com 搜集整理 在C++的STL库中,要实现排序可以通过将所有元素保存到vector中,然后通过sort算法来排序,也可以通过multimap实现在插入元素的时候进行排序.在 ...
- STL vector 用法介绍
介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...
- STL vector使用方法介绍
介绍 这篇文章的目的是为了介绍std::vector,怎样恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...
- stl——vector详解
stl——vector详解 stl——vector是应用最广泛的一种容器,类似于array,都将数据存储于连续空间中,支持随机访问.相对于array,vector对空间应用十分方便.高效,迭代器使ve ...
随机推荐
- ZeroMQ接口函数之 :zmq_msg_get - 获取消息的性质
ZeroMQ 官方地址 :http://api.zeromq.org/4-1:zmq_msg_get zmq_msg_get(3) ØMQ Manual - ØMQ/3.2.5 Name zmq_m ...
- ZeroMQ接口函数之 :zmq_recv – 从一个socket上接收一个消息帧
ZeroMQ 官方地址 :http://api.zeromq.org/4-1:zmq_recv zmq_recv(3) ØMQ Manual - ØMQ/4.1.0 Name zmq_r ...
- table常用功能总结
1,设置表格边框为单线框 table, th, td { border: 1px solid blue; }加上:table { border-collapse:collapse; } 由于 tabl ...
- 【超级干货】手机移动端WEB资源整合
meta基础知识 H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 <meta name="viewport" content="width=device-wid ...
- android—-线性布局
android五大布局之线性布局. 1.线性布局的特点:各个子元素彼此连接,中间不留空白 而今天我们要讲解的就是第一个布局,LinearLayout(线性布局),我们屏幕适配的使用 用的比较多的就是L ...
- NorthWind 数据库整体关系
http://blog.csdn.net/bergn/article/details/1502150 今天看到一张非常有用的图,说明有关Northwind数据库整体关系的图,以前一直在用,但是没有一个 ...
- thinkphp留言板例子(多条件查询)
登录: login.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...
- C++ 小工具一键解决SVN Clean Up 失败的问题
参考文章: 1.http://blog.csdn.net/luochao_tj/article/details/46358145 2.http://blog.csdn.net/segen_jaa/ar ...
- ueditor的模板功能
~~项目临时发现kindeditor比ueditor更好用~~ 不过还是回来把ueditor的学习,加一个书签标明一下阶段. 下面写一下ueditor的模板功能,即修改模板,使ueditor自动生成想 ...
- bash shell + python简单教程,让你的mac/linux终端灰起来~
前提条件:已经安装python,命令行支持bash 在bash_profile中添加 function ccolor { python /Users/xirtam/Documents/tools/cc ...