c++ vector

Posted on 2007-10-23 21:04 把大海藏到心里 阅读(290) 评论(0)  编辑 收藏 所属分类: c/c++
一.语法:

#include <vector>
  vector();   默认构造函数
  vector( const vector& c );   从一个已知的vector对象复制到新vector对象中
  vector( size_type num, const TYPE& val = TYPE() );   有num个元素,如果有val,则每个元素的值都是val
  vector( input_iterator start, input_iterator end );   被初始化为start到end的元素   
  ~vector();   析钩函数

例子:

// create a vector of random integers   创建一个包含随机数的vector对象
 cout <<"original vector: ";
 vector<int> v;
 for(int i =0; i <10; i++){
   int num =(int) rand()%10;
   cout << num <<" ";
   v.push_back( num );
 }
 cout << endl;            

 // find the first element of v that is even   找到vector对象中的第一个元素
 vector<int>::iterator iter1 = v.begin();
 while( iter1 != v.end()&&*iter1 %2!=0){
   iter1++;
 }              

 // find the last element of v that is even   找到最后一个元素
 vector<int>::iterator iter2 = v.end();
 do{
   iter2--;
 }while( iter2 != v.begin()&&*iter2 %2!=0);              

 // only proceed if we find both numbers   找到初始和结束的元素
 if( iter1 != v.end()&& iter2 != v.begin()){
   cout <<"first even number: "<<*iter1 <<", last even number: "<<*iter2 << endl;        

   cout <<"new vector: ";
   vector<int> v2( iter1, iter2 );
   for(int i =0; i < v2.size(); i++){
     cout << v2[i]<<" ";
   }
   cout << endl;
 }
结果:
original vector:1979272198
 first even number:2, last even number:8
 new vector:27219    

二:方法

1.assign
#include <vector>
void assign( size_type num, const TYPE& val );
void assign( input_iterator start, input_iterator end );
例子:
vector<int> v;
 v.assign(10,42); 构造函数1
 for(int i =0; i < v.size(); i++){
   cout << v[i]<<" ";
 }
 cout << endl;            
结果:42424242424242424242
vector<int> v1;
 for(int i =0; i <10; i++){
   v1.push_back( i );
 }              

 vector<int> v2;
 v2.assign( v1.begin(), v1.end());   构造函数2         

 for(int i =0; i < v2.size(); i++){
   cout << v2[i]<<" ";
 }
 cout << endl;            
结果:0123456789

2.at




#include <vector>
TYPE& at( size_type loc );
const TYPE& at( size_type loc ) const;
例子:
vector<int> v(5,1);
 for(int i =0; i <10; i++){
   cout <<"Element "<< i <<" is "<< v[i]<< endl;
 }  

下面的安全些:
vector<int> v(5,1);
 for(int i =0; i <10; i++){
   cout <<"Element "<< i <<" is "<< v.at(i)<< endl;
 }
   

3.back
#include <vector>
TYPE& back();
const TYPE& back() const;

例子:
vector<int> v;
 for(int i =0; i <5; i++){
   v.push_back(i);
 }
 cout <<"The first element is "<< v.front()
      <<" and the last element is "<< v.back()<< endl;    
结果:
The first element is0and the last element is4    


4.begin
#include <vector>
iterator begin();
const_iterator begin() const;
例子:
vector<string> words;
  string str;
 
  while( cin >> str ) words.push_back(str);

  vector<string>::iterator iter;
  for( iter = words.begin(); iter != words.end(); iter++){
    cout <<*iter << endl;
  }
结果:
输入:hey mickey you're so fine







输出:hey
  mickey
  you're
  so
  fine
5.capacity
#include <vector>
size_type capacity() const;
例子:
vector<int> v1(10);
 cout <<"The capacity of v1 is "<< v1.capacity()<< endl;
 vector<int> v2;
 v2.reserve(20);
 cout <<"The capacity of v2 is "<< v2.capacity()<< endl;        
结果:
The capacity of v1 is10
 The capacity of v2 is20 
6.clear
#include <vector>
void clear();
清空所有元素