//数组初始化vector
int iarray[]={1,2,3,4,5,6,7,8,9,0};
//count: iarray数组个数
size_t count=sizeof(iarray)/sizeof(int);
//int数组初始化 ivec3
vector<int> ivec3(iarray,iarray+count);
for(int_ite=ivec3.begin ();int_ite!=ivec3.end ();int_ite++)
cout<<"ivec3: "<<*int_ite<<endl;
//string数组初始化 svec1
string word[]={"ab","bc","cd","de","ef","fe"};
//s_count: word数组个数
size_t s_count=sizeof(word)/sizeof(string);
//string数组初始化 svec1
vector<string> svec1(word,word+s_count);
for(string_ite=svec1.begin ();string_ite!=svec1.end ();string_ite++)
cout<<"svec1: "<<*string_ite<<endl;
- //vector的赋值并不可以像数组一样方便的用花括号方便的完成赋值,这里借用了数组来初始化这个vector,初始化方 式vector<elementType> intvec(begin,end);这样可以用起来看上去还是比较习惯的
- int a[7]={1,2,3,4,5,6,7};
- vector<int> ivector(a,a+7);
- vector<int>::iterator iter;
- for_each(ivector.begin(),ivector.end(),print);//用for_each进行遍历
- cout<<endl;
- ivector[5]=1;
- cout<<ivector[5]<<endl<<ivector.size()<<endl;
- for_each(ivector.begin(),ivector.end(),print);//用for_each进行遍历
- return 0;