首页 > 其他 > 详细

STL—Vector简介

时间:2015-04-25 00:01:43      阅读:267      评论:0      收藏:0      [点我收藏+]

有关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简介

原文:http://www.cnblogs.com/yqbeyond/p/4455058.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!