c++ vector用法

一、MSDN中的定义:

Defines the container template class vector and several supporting templates.

The vector is a container that organizes elements of a given type in a linear sequence.  It enables fast random access to any element, and dynamic additions and removals to and from the sequence.  The vector is the preferred container for a sequence when random-access performance is at a premium.

 

 二、要求:

vector是一个模板类,是一个比较有用的容器

头文件:#include<vector>

命名空间:using namespace std

 

三、用法:

vector是用来存储一个线性的元素集合的容器,可以很快速随机的对线性集合中的元素进行添加和移除

1、基本操作

(1)创建一个vector对象: vector <type>vec;       

ex:

vector<int>vec

(2)在尾部插入元素: type a; vec.push_back(a)

ex:

int a = 2;

vec.push_back(a);

(3)是用下标访问元素: cout<<vec[0]<<endl;

(4)使用迭代器访问元素

(5)插入元素:vec.insert(vec.begin()+1,a) //在i+1的元素前面插入a

(6)删除元素:vec.erase(vec.begin()+2);//删除第三个元素

(7)删除区间:vec.erase(vec.begin()+i,vec.begin()+j); //删除区间[i,j-1];区间从0开始

(8)向量大小:vec.size();

(9)向量清空:vec.clear();

ex:

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<int> vec;
    //在尾部添加元素
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);
    cout << "使用下标访问元素:" << endl;
    cout << vec[0] << endl;  //输出结果:1
    cout << vec[1] << endl;  //输出结果:2
    cout << vec[2] << endl;  //输出结果:3
    cout << "使用迭代器访问元素:" << endl;
    vector<int>::iterator it;
    for(it = vec.begin(); it != vec.end(); it++ )
    {
        cout << *it << endl;
    }
    //在第三个元素之前插入4
    vec.insert(vec.begin()+1,4);
    cout << "插入元素后结果:" << endl;
    for(it = vec.begin(); it != vec.end(); it++ )
    {
        cout << *it << endl;
    }
    //输出第三个元素
    vec.erase(vec.begin()+3);
    cout << "删除元素后结果:" << endl;
    for(it = vec.begin(); it != vec.end(); it++ )
    {
        cout << *it << endl;
    }
    //删除区间[1,2]
    vec.erase(vec.begin()+1,vec.begin()+3);
    cout << "删除区间后的结果:" << endl;
    for(it = vec.begin(); it != vec.end(); it++ )
    {
        cout << *it << endl;
    }
    //向量大小
    cout << "vector大小:" << vec.size()  << endl;
    //向量清空
    vec.clear();
    return 1;
}

结果:
使用下标访问元素:
1
2
3
使用迭代器访问元素:
1
2
3
插入元素后结果:
1
4
2
3
删除元素后结果:
1
4
2
删除区间后的结果:
1
vector大小:1

 

2、vector对结构体的操作

vector对结构体操作需要注意的是结构体需要定义为全局,否则会出错

ex:

#include<iostream>
#include<vector>
using namespace std;
typedef struct rect
{
    int id;
    int length;
    int 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 1;
}

结果:
1 2 3

 

3、算法

需要头文件:#include<algorithm>

(1)使用reverse将元素翻转:reverse(vec.begin(),vec.end());

ex:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    vector<int> vec;
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);
    vector<int>::iterator it;
    cout << "翻转之前序列:";
    for(it = vec.begin(); it != vec.end(); it++ )
    {
        cout << *it << ' ';
    }
    cout << endl;
    cout << "翻转之后序列:";
    reverse(vec.begin(),vec.end());
    for(it = vec.begin(); it != vec.end(); it++ )
    {
        cout << *it << ' ';
    }
    cout << endl;
    return 1;
}

结果:
翻转之前序列:1 2 3
翻转之后序列:3 2 1

(2)使用sort排序

sort(vec.begin(),vec.end());//默认为升序排列,即从小到大排序

若想要设置为降序排列的话,需要重写排列比较函数

bool Comp(const int &a,const int &b)
{
    return a>b;
}
调用时:sort(vec.begin(),vec.end(),Comp),这样就降序排序。

ex:

 

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool Comp(const int&a,const int &b)
{
    return a>b;
}
int main()
{
    vector<int>vec;
    vec.push_back(4);
    vec.push_back(3);
    vec.push_back(5);
    vec.push_back(1);
    vector<int>::iterator it;
    cout << "排序之前序列:";
    for(it = vec.begin(); it != vec.end(); it++ )
    {
        cout << *it << ' ';
    }
    //升序排序
    cout << endl;
    cout << "升序排序:";
    sort(vec.begin(),vec.end());
    for(it = vec.begin(); it != vec.end(); it++ )
    {
        cout << *it << ' ';
    }
    cout << endl;
    cout << "降序排序:";
    sort(vec.begin(),vec.end(),Comp);
    for(it = vec.begin(); it != vec.end(); it++ )
    {
        cout << *it << ' ';
    }
    cout << endl;
    return 1;
}

结果:
排序之前序列:4 3 5 1
升序排序:1 3 4 5
降序排序:5 4 3 1

posted on 2014-09-11 15:36  学习程序  阅读(181)  评论(0)    收藏  举报

导航