使用数组初始化vector

时间:2021-04-28 19:38:12

#include<iostream>

using namespace std;


int main()
{
   int a[]={0,1,2,3,4,5};
   vector<int> t(a,a+6);


   for(vector<int>::iterator i = t.begin();  i!= t.end(); ++i)
   {
       cout<<*i<<endl;
   }
   return 0;
}


=====================================================

vector 有一构造函数形式如下:

template< class InputIt >

vector( InputIt first, InputIt last, 

         const  Allocator &  alloc  =  Allocator ( )   ) ;


故可以使用两个iterator 或者两个指针来初始化vector, vector 的内容为first(包括) 到last 前一个元素