zoukankan      html  css  js  c++  java
  • vector

    在c++中,vector是一个十分有用的容器。

    作用:它能够像容器一样存放各种类型的对象,简单地说,vector是一个能够存放任意类型的动态数组,能够增加和压缩数据。

    vector在C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库。

    为了使用vector,必须包含头文件<vector>。另,vector属于std命名空间,因此需要通过命名限定,可以有如下三种方式,后两种方式更好,因为未引入无关的内容。
    using namespace std;
    using namespace std::vector;
    std::vector<int> vec;

    相关函数的功能:

        vec.begin()//指向迭代器中第一个元素。   
        vec.end()//指向迭代器中末端元素的下一个,指向一个不存在元素。          
        vec.push_back(elem)     //在尾部加入一个数据。  
        vec.pop_back()          //删除最后一个数据。  
        vec.capacity()  //vector可用空间的大小。  
        vec.size()//返回容器中数据个数。  
        vec.empty() //判断容器是否为空。  
        vec.front()     //传回第一个数据。  
        vec.back()  //传回最后一个数据,不检查这个数据是否存在。  
        vec.at(index)   //传回索引idx所指的数据,如果idx越界,抛出out_of_range。  
        vec.clear() //移除容器中所有数据。  
        vec.erase(iterator) //删除pos位置的数据,传回下一个数据的位置。  
        vec.erase(begin,end)    //删除[beg,end)区间的数据,传回下一个数据的位置。注意:begin和end为iterator  
        vec.insert(position,elem)   //在pos位置插入一个elem拷贝,传回新数据位置。  
        vec.insert(position,n,elem) //在pos位置插入n个elem数据,无返回值。  
        vec.insert(position,begin,end)  //在pos位置插入在[beg,end)区间的数据,无返回值。  
    

     示例:

    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    #define N 5
    const int M=20;
    
    vector <int> v;
    vector<int>::iterator i1,i2;
    
    typedef struct ve//结构体vector
    {
    	char c;
    	int i;
    	double d;
    }sve;
    
    void pv()//输出 
    {
    	for (i1=v.begin();i1!=v.end();i1++)
    	{
    		printf("%3d",*i1);
    	}
    	printf("
    ");
    }
    
    int main()
    {
    	int i,j,a;
    
    	for (i=0;i<N;i++)//int型vector
    	{
    		v.push_back(i);
    	}
    
    	printf("%20s","set vector:");
    	for (i=0;i<N;i++)
    	{
    		printf("%3d",v[i]);
    	}
    
    	printf("
    %20s%d %d
    ","v.front,v.back:",v.front(),v.back());
    	i1=v.begin();//begin,end
    	i2=v.end()-1;
    	printf("%20s%d %d
    ","*v.begin,*v.end:",*i1,*i2);
    
    	v.insert(v.begin()+N,6);//insert,erase
    	printf("%20s","insert:");
    	pv();
    	printf("%d
    ",v.at(N));
    	v.insert(v.begin(),N,N);
    	pv();
    	v.insert(v.begin()+N,v.begin()+N,v.end());
    	pv();
    	printf("%20s","erase:");
    	v.erase(v.begin());
    	pv();
    	v.erase(v.begin(),v.begin()+1);
    	pv();
    	v.erase(v.begin(),v.end()-2);
    	pv();
    	printf("%20s","size:");
    	printf("%d
    ",(int)v.size());//size
    	printf("%20s","clear:");
    	v.clear();//clear
    	printf("%d
    ",(int)v.size());
    	printf("%d
    ",v.empty());
    
    	vector <sve> v1;//创建结构体vector
    	sve ve;
        for (i=0;i<N;i++)
        {
            ve.c='i';
            ve.d=(double)N;
            ve.i=N;
            v1.push_back(ve);
        }
        vector <sve>::iterator i3;
        for (i3=v1.begin();i3!=v1.end();i3++)
        {
            printf("%c  %lf  %d
    ",(*i3).c,(*i3).d,(*i3).i);
        }
    
    	return 0;
     }
    
     
    
  • 相关阅读:
    函数
    python操作文件
    POJ-2689-Prime Distance(素数区间筛法)
    POJ-2891-Strange Way to Express Integers(线性同余方程组)
    POJ-2142-The Balance
    POJ-1061-青蛙的约会(扩展欧几里得)
    Educational Codeforces Round 75 (Rated for Div. 2) D. Salary Changing
    Educational Codeforces Round 75 (Rated for Div. 2) C. Minimize The Integer
    Educational Codeforces Round 75 (Rated for Div. 2) B. Binary Palindromes
    Educational Codeforces Round 75 (Rated for Div. 2) A. Broken Keyboard
  • 原文地址:https://www.cnblogs.com/hemeiwolong/p/8995056.html
Copyright © 2011-2022 走看看