Vector的用法详解

1.vector的常见用法

//vector的定义

//定义一个一维数组name[size]
vector<typename> name;

//定义一个二维vector数组,当作两个维都可变长的二维数组 
vector<vector<int> > name;
//注意如果typename是一个STL容器,定义的时候要记得在>>符号前加上空格 

//定义vector数组的方法
vector<typename> Arrayname[arraySize];
//eg:vector<int> vi[100]; 

/*
访问vi[index]
1.通过下标访问:下标从0到vi.size-1  
2.通过迭代器访问 : 
vector<typename>::iterator it;
*/  

vector<int>::iterator it;
vector<double>::iterator it;
//通过*it来访问vector里的元素 

vector<int> vi;
for(int i=1;i<=5;i++){
    vi.push_back(i);//push_back(i)在vi的末尾添加元素i,即依次添加1 2 3 4 5     
} 

 可以通过确定容器首地址然后类似下表指针访问数组的方法赋值

#include<iostream>
#include<vector>
using namespace std;
int main(){
    vector<int> vi;
    for(int i=1;i<=5;i++){
        vi.push_back(i);
    }
    //vi.begin()为取vi的首元素地址,而it指向这个地址
    vector<int>::iterator it=vi.begin();
    for(int i=0;i<5;i++){
        cout<<*(it+i)<<' ';
    } 
    cout<<endl;
    return 0;
} 

结果:

pop_back():删除vector的为元素,时间复杂度为O(1)

#include<iostream>
#include<vector>
using namespace std;
int main(){
    vector<int> vi;
    for(int i=1;i<=5;i++){
        vi.push_back(i);
    }
    vi.pop_back();
    for(int i=0;i<vi.size();i++){
        cout<<vi[i]<<' ';
    }
    return 0;
} 

 其他函数

//size用来获得vector中元素的个数
vi.size()

//clear用来清空vector种所有的元素,时间复杂度为O(n)
vi.clear() 

//insert(it,x)用来向vector的任意迭代器it处插入一个元素x,时间复杂度为O(n)
vi.insert(vi.begin()+2,-1)//将-1插入vi[2]的位置

//erase用来删除单个元素,删除一个区间内的所有元素
//删除单个元素
vi.erase(vi.begin()+3)
//删除一个区间内的所有元素
vi.erase(vi.begin()+1,vi.begin()+4)//删除vi[1]到vi[3]的值 

 vector本身可以作为数组使用,在元素个数不确定的场合可以很好的节省空间

例题:

Course List for Student

题目描述

Zhejiang University has 40000 students and provides 2500 courses. Now given the student name lists of all the courses, you are supposed to output the registered course list for each student who comes for a query.

输入

Each input file contains one test case. For each case, the first line contains 2 positive integers: N (<=40000), the number of students who look for their course lists, and K (<=2500), the total number of courses. Then the student name lists are given for the courses (numbered from 1 to K) in the following format: for each course i, first the course index i and the number of registered students Ni (<= 200) are given in a line. Then in the next line, Ni student names are given. A student name consists of 3 capital English letters plus a one-digit number. Finally the last line contains the N names of students who come for a query. All the names and numbers in a line are separated by a space.

输出

For each test case, print your results in N lines. Each line corresponds to one student, in the following format: first print the student's name, then the total number of registered courses of that student, and finally the indices of the courses in increasing order. The query results must be printed in the same order as input. All the data in a line must be separated by a space, with no extra space at the end of the line.

样例输入

11 5
4 7
BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
1 4
ANN0 BOB5 JAY9 LOR6
2 7
ANN0 BOB5 FRA8 JAY9 JOE4 KAT3 LOR6
3 1
BOB5
5 9
AMY7 ANN0 BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
ZOE1 ANN0 BOB5 JOE4 JAY9 FRA8 DON2 AMY7 KAT3 LOR6 NON9

样例输出

ZOE1 2 4 5
ANN0 3 1 2 5
BOB5 5 1 2 3 4 5
JOE4 1 2
JAY9 4 1 2 4 5
FRA8 3 2 4 5
DON2 2 4 5
AMY7 1 5
KAT3 3 2 4 5
LOR6 4 1 2 4 5
NON9 0

链接

http://codeup.cn/problem.php?cid=100000596&pid=0

代码

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int M=26*26*26*10+1;
vector<int> selectCourse[M];

int getID(char name[]){//解析名字 
    int id=0;
    for(int i=0;i<3;i++){//前三个字母 
        id=id*26+(name[i]-'A');
    }
    id=id*10+(name[3]-'0');//最后一个数字 
    return id;
}

int main(){
    char name[5];
    int N,K;
    cin>>N>>K;//N即正在寻找其课程列表的学生人数,K即课程总数
    for(int i=0;i<K;i++){
        int course,x;
        cin>>course>>x;//以行的形式给出课程索引i和注册学生人数Ni
        for(int j=0;j<x;j++){
            cin>>name;//给出Ni个学生的姓名
            int id=getID(name);
            selectCourse[id].push_back(course);
        }
    } 
    for(int i=0;i<N;i++){
        cin>>name;//最后一行包含要查询的N个学生的姓名
        int id=getID(name);
        sort(selectCourse[id].begin(),selectCourse[id].end());
        cout<<name<<" "<<selectCourse[id].size()<<" ";//打印该学生的姓名和该学生的注册课程总数 
        for(int j=0;j<selectCourse[id].size();j++){
            cout<<selectCourse[id][j]<<" ";//打印升序的课程索引 
        }
        cout<<endl;
    }
    return 0;
} 
原文地址:https://www.cnblogs.com/ak918xp/p/13541408.html