Vector构造函数
#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; int main() { string str[] = {"Alex" ,"John" ,"Robert"}; //empty vector object vector<int> v1; //creates vector with 10 empty elements vector<int> v2(10); //creates vector with 10 elements //and assign value 0 for each vector<int> v3(10,0); //creates vector and assigns values from string array vector<string> v4(str+0 ,str+3); vector<string>::iterator sIt = v4.begin(); while(sIt != v4.end()) { cout<<*sIt++<<" "; } cout<<endl; //copy constructor vector<string> v5(v4); for(int i = 0;i<3;i++) cout<<v5[i]<<" "; cout<<endl; return 0 ; }
Vector.assign函数,给容器赋值;
#include <vector> #include <algorithm> #include <iostream> #include <iterator> using namespace std; int main() { int ary[] = {1,2,3,4,5}; //流迭代器,针你可以把东西写的输出流中。 ostream_iterator<int> output(cout," "); vector<int> v; //赋值 v.assign(ary,ary+5); copy(v.begin(),v.end(),output); cout<<endl; v.assign(3,100); copy(v.begin(),v.end(),output); cout<<endl; return 0; }
at(),访问容器中的元素,并且可以修改容器中的元素。
#include <vector> #include <iostream> #include <iterator> using namespace std; void main() { vector<int> vec(3,10); vec[0] = 100; vec.at(1) = 200; ostream_iterator<int> output(cout," "); copy(vec.begin() ,vec.end() ,output); cout<<endl; }
back(),返回容器的最后一个元素的引用。
#include <iostream> #include <vector> #include <string> #include <iterator> using namespace std; //back()返回容器最后一个元素的引用,可通过它修改最后一个元素。 template<class T ,class D> class Member { public: Member(T t ,D d):name(t),sal(d){} void print(); private: T name; D sal; }; template<class T ,class D> void Member<T,D>::print() { cout<<name<<" "<<sal<<endl; } int main() { typedef Member<string,double> M; vector<M> v; v.push_back(M("Robert",6000)); v.push_back(M("Linda",7500)); v.push_back(M("Aliens",8500)); vector<M>::iterator mIt = v.begin(); cout<<"Entire vector:"<<endl; while(mIt != v.end()) { (mIt++)->print(); } cout<<endl; cout<<"Return from back()"<<endl; v.back()=M("china",99); mIt = v.begin(); while(mIt != v.end()) { (mIt++)->print(); } cout<<endl; return 0; }
Resize(),调整容器的大小。size()返回容器的大小。Capacity()返回容器的容量。
#include <iostream> #include <vector> using namespace std; int main() { vector<int> v(10,0); cout<<"Size of v = "<<v.size()<<endl; cout<<"Capacity of v = "<<v.capacity()<<endl; v.resize(100); cout<<"After resizing:"<<endl; cout<<"Size of v = "<<v.size()<<endl; cout<<"Capacity of v = "<<v.capacity()<<endl; }
实验显示,返回的容量和大小一样。
clear(),清空容器中的元素
#include <iostream> #include <vector> #include <algorithm> using namespace std; template<class T> class Print { public: void operator()(T & t) { cout<<t<<" "; } }; int main() { vector<int> v(10); Print<int> print; fill(v.begin() ,v.end() ,5); cout<<"Vector v : "; for_each(v.begin() ,v.end() ,print); cout<<endl; cout<<"Size of V = "<<v.size()<<endl; v.clear(); cout<<"Vector v : "; for_each(v.begin() ,v.end() ,print); cout<<endl; cout<<"Size of V = "<<v.size()<<endl; cout<<"Vector v is "<<(v.empty()?"":"not "); cout<<"empty"<<endl; }
erase(),删除指定元素,返回指定元素的下一个指针。
#include <iostream> #include <iterator> #include <vector> using namespace std; void main() { vector<int> v(10); vector<int>::iterator It; for(int i = 0 ;i < 10 ;i++) { v[i] = i; } copy(v.begin() ,v.end() ,ostream_iterator<int>(cout," ")); cout<<endl; It = v.begin() + 2; v.erase(It); copy(v.begin() ,v.end() ,ostream_iterator<int>(cout ," ")); cout<<endl; It = v.begin(); v.erase(It ,It + 2); copy(v.begin() ,v.end() ,ostream_iterator<int>(cout ," ")); cout<<endl; //循环查找某个元素; It = v.begin(); for( ;It != v.end() { //注意,erase()返回指向下一个元素的指针。 if(*It == 9) It = v.erase(It); else It++; } copy(v.begin() ,v.end() ,ostream_iterator<int>(cout," ")); cout<<endl; return ; }
front(),返回容器的第一个元素。
#include <iostream> #include <vector> #include <string> #include <iterator> using namespace std; template<class T ,class D> class Member { public: Member(T t ,D d):name(t),sal(d){} void print(); private: T name; D sal; }; template<class T ,class D> void Member<T,D>::print() { cout<<name<<" "<<sal<<endl; } int main() { typedef Member<string ,double> M; vector<M> v; v.push_back(M("Linda",75000)); v.push_back(M("Robert",60000)); vector<M>::iterator It = v.begin(); cout<<"Entire vector:"<<endl; while(It != v.end()) { (It++)->print(); } cout<<endl; cout<<"Return from front()"<<endl; v.front().print(); return 0; }
insert() 向容器指定位置处插入元素。
#include <iostream> #include <vector> #include <iostream> #include <algorithm> #include <iterator> using namespace std; template<class T> class Print { public: void operator()(T & t) { cout<<t<<" "; } }; void main() { int ary[5]; fill(ary,ary+5,1); vector<int> v; vector<int>::iterator It; Print<int> print; copy(ary ,ary+5 ,back_inserter(v)); cout<<"vector v : "; for_each(v.begin() ,v.end() ,print); cout<<endl; It = v.begin(); cout<<"v.insert(It,5) :"; v.insert(It ,5); for_each(v.begin() ,v.end() ,print); cout<<endl; //Insert range ary+2 - ary+5 at the position "It" It = v.begin() + 5; cout<<"v.insert(It ,ary+2 ,ary+5) :"; v.insert(It ,ary+2 ,ary+5); for_each(v.begin() ,v.end() ,print); cout<<endl; //Insert 2 value of "20" at the position of It It = v.end() - 2; v.insert(It ,2 ,20); for_each(v.begin() ,v.end() ,print); cout<<endl; return ; }
max_size()函数,返回可以增长的最大长度。
#include <iostream> #include <vector> using namespace std; int main() { vector<int> v(10); cout<<"Size of v = "<<v.size()<<endl; cout<<"Max size of v = "<<v.max_size()<<endl; return 0; }
结果如下:
pop_back() 将容器最后面的元素弹出。(删除)
#include <iostream> #include <vector> #include <algorithm> using namespace std; template<class T> class Print { public: void operator()(T & t) { cout<<t<<" "; } }; int main() { vector<int> v; Print<int> print; for(int i = 0 ; i < 5 ;i++) { v.push_back(i+1); } while (!v.empty()) { for_each(v.begin() ,v.end() ,print); cout<<endl; v.pop_back(); } }
运行结果是:
reverse()函数,用来扩展vector的容量。
#include <iostream> #include <vector> #include <iterator> using namespace std; void main() { vector<int> v(5 ,0); /*--------------------------------*/ cout<<"Size of v = "<<v.size()<<endl; cout<<"Capacity v = "<<v.capacity()<<endl; cout<<"Value of each element :"; copy(v.begin() ,v.end() ,ostream_iterator<int>(cout ," ")); cout<<endl; /*--------------------------------*/ v[0] = 5; v[1] = 8; //automatically increases size; v.push_back(3); v.push_back(7); cout<<"Size of v = "<<v.size()<<endl; cout<<"Capacity v = "<<v.capacity()<<endl; cout<<"Value of each element :"; copy(v.begin() ,v.end() ,ostream_iterator<int>(cout ," ")); cout<<endl; /*--------------------------------*/ //increase capacity to 100; v.reserve(100); cout<<"Size of v = "<<v.size()<<endl; cout<<"Capacity v = "<<v.capacity()<<endl; cout<<"sizeof v = "<<sizeof(v)<<endl; cout<<"Value of each element :"; copy(v.begin() ,v.end() ,ostream_iterator<int>(cout ," ")); cout<<endl; }
resize()函数,调整容器的大小。若参数大于当前容器大小,可将增加的元素初始化为固定值。反之,将多余的元素从容器中删除。
#include <iostream> #include <iterator> #include <vector> using namespace std; void main() { vector<int> v(5); for(int i = 0 ;i < 5 ;i++) { v[i] = i * 2; } copy(v.begin() ,v.end() ,ostream_iterator<int>(cout ," ")); cout<<endl; //将size扩展为7个,并且将新增加的初始化为7; v.resize(7,100); copy(v.begin() ,v.end() ,ostream_iterator<int>(cout ," ")); cout<<endl; //调整vector的大小为4,多余的删去; v.resize(4); copy(v.begin() ,v.end() ,ostream_iterator<int>(cout ," ")); cout<<endl; return ; }
swap()函数,交换两个容器的元素、size、capacity;
#include <iostream> #include <vector> #include <algorithm> using namespace std; template<class T> class Print { public: void operator()(T & t) { cout<<t<<" "; } }; int main() { int ary[] = {1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10}; Print<int> print; vector<int> v1(ary,ary+7); vector<int> v2(ary+7,ary+10); /*=======================*/ cout<<"Vector v1 :"; for_each(v1.begin() ,v1.end() ,print); cout<<endl; cout<<"Size of v1 = "<<v1.size()<<endl<<endl; cout<<"Vector v2 :"; for_each(v2.begin() ,v2.end() ,print); cout<<endl; cout<<"Size of v1 = "<<v2.size()<<endl<<endl; v1.swap(v2); cout<<"======================"<<endl; /*=======================*/ cout<<"Vector v1 :"; for_each(v1.begin() ,v1.end() ,print); cout<<endl; cout<<"Size of v1 = "<<v1.size()<<endl<<endl; cout<<"Vector v2 :"; for_each(v2.begin() ,v2.end() ,print); cout<<endl; cout<<"Size of v1 = "<<v2.size()<<endl<<endl; }