vector

template < class T, class  Allocator = allocator > class vector;

 
这两个参数的意义:

T: 元素的类型
Allocator: 元素所要分配的空间大小。
下面就一一介绍Vector的内部方法

[cpp] view plaincopy
1   vector构造函数:也就是如何对一个vec对象进行初始化。  
////////////////////////////代码//////////////////////////////////////  
explicit vector ( const Allocator& = Allocator() );  
explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );  
template   
         vector ( InputIterator first, InputIterator last, const Allocator& = Allocator() );  
vector ( const vector& x );  
////////////////////////////代码//////////////////////////////////////  
参数解释:n,初始化元素个数。 T,你所要添加进去的元素。  
First,容器第一个元素,last最后一个元素,一个vector的引用。  
示例:  
// constructing vectors  
#include   
#include   
using namespace std;  
  
int main ()  
{  
  unsigned int i;  
  
  // constructors used in the same order as described above:  
  vector first;            // 初始化一个int类型的空vector变量  
  vector second (4,100);             // 初始化4个100  
  vector third (second.begin(),second.end());    
// 通过second 进行赋值  
  vector fourth (third);               // a copy of third  
  
  // the iterator constructor can also be used to construct from arrays:  
  int myints[] = {16,2,77,29};  
  vector fifth (myints, myints + sizeof(myints) / sizeof(int) );  
  
  cout << "The contents of fifth are:";  
  for (i=0; i < fifth.size(); i++)  
    cout << " " << fifth[i];  
  
  cout << endl;  
  
  return 0;  
}  
输出结果:  
The contents of fifth are: 16 2 77 29   
2   vector::~vector析构函数  
调用每一个元素对象的析构函数  
  
  
  
  
成员函数  
3   vector::assign  
template   
  void assign ( InputIterator first, InputIterator last );  
  
void assign ( size_type n, const T& u );  
  
如 c.assign(beg,end)c.assign(n,elem)  
  将[beg; end)区间中的数据赋值给c。将n个elem的拷贝赋值给c。  
  
示例:  
// vector assign  
#include   
#include   
using namespace std;  
  
int main ()  
{  
  vector first;  
  vector second;  
  vector third;  
  
  first.assign (7,100);             // a repetition 7 times of value 100  
  
  vector::iterator it;  
  it=first.begin()+1;  
  
  second.assign (it,first.end()-1); // the 5 central values of first  
  
  int myints[] = {1776,7,4};  
  third.assign (myints,myints+3);   // assigning from array.  
  
  cout << "Size of first: " << int (first.size()) << endl;  
  cout << "Size of second: " << int (second.size()) << endl;  
  cout << "Size of third: " << int (third.size()) << endl;  
  return 0;  
}  
  
  
  
输出结果  
Size of first: 7  
Size of second: 5  
Size of third: 3  
  
4   vector::at  
const_reference at ( size_type n ) const;  
      reference at ( size_type n );  
  
返回vector内的n的引用。  
代码示例  
// vec.cpp : Defines the entry point for the console application.  
//  
  
#include "stdafx.h"  
// constructing vectors  
#include   
#include   
using namespace std;  
  
int main ()  
{  
    vector myvector(10);//初始化10个为0的元素。  
    unsigned int i;  
    for (i=0;i  
#include   
using namespace std;  
  
int main ()  
{  
  vector myvector;  
  
  myvector.push_back(10);  
  
  while (myvector.back() != 0)  
  {  
    myvector.push_back ( myvector.back() -1 );  
  }  
  
  cout << "myvector contains:";  
  for (unsigned i=0; i  
#include   
using namespace std;  
  
int main ()  
{  
  vector myvector;  
  for (int i=1; i<=5; i++) myvector.push_back(i);  
  
  vector::iterator it;  
  
  cout << "myvector contains:";  
  for ( it=myvector.begin() ; it < myvector.end(); it++ )  
    cout << " " << *it;  
  
  cout << endl;  
  
  return 0;  
}  
Output:  
myvector contains: 1 2 3 4 5  
6   vector::capacity  
返回vector对象存储空间大小  
  
例子:  
// comparing size, capacity and max_size  
#include   
#include   
using namespace std;  
  
int main ()  
{  
  vector myvector;  
  
  // set some content in the vector:  
  for (int i=0; i<100; i++) myvector.push_back(i);  
  
  cout << "size: " << (int) myvector.size() << "\n";  
  cout << "capacity: " << (int) myvector.capacity() << "\n";  
  cout << "max_size: " << (int) myvector.max_size() << "\n";  
  return 0;  
}  
A possible output for this program could be:  
size: 100  
capacity: 141  
max_size: 1073741823  
7   vector::clear  
清除对象内的所有元素,就是调用了析构函数,清空,然后为0  
  
示例  
// clearing vectors  
#include   
#include   
using namespace std;  
  
int main ()  
{  
  unsigned int i;  
  vector myvector;  
  myvector.push_back (100);  
  myvector.push_back (200);  
  myvector.push_back (300);  
  
  cout << "myvector contains:";  
  for (i=0; i  
#include   
using namespace std;  
  
int main ()  
{  
  vector myvector;  
  int sum (0);  
  
  for (int i=1;i<=10;i++) myvector.push_back(i);  
  
  while (!myvector.empty())  
  {  
     sum += myvector.back();  
     myvector.pop_back();  
  }  
  
  cout << "total: " << sum << endl;  
    
  return 0;  
}  
9   vector::end  
iterator end ();  
const_iterator end () const;  
  
Returns an iterator referring to the past-the-end element in the vector container.  
Both iterator and const_iterator are member types. In the vector class template, these are random access iterators  
  
// test_vector.cpp : 定义控制台应用程序的入口点。  
//  
  
#include "stdafx.h"  
#include   
#include   
using namespace std;  
  
int _tmain(int argc, _TCHAR* argv[])  
{  
    vector myvec;  
    for (int i=0;i<6;i++)  
    {  
        myvec.insert(myvec.end(),i);  
    }  
    cout<<"element";  
    vector::iterator it;  
    for (it=myvec.begin();it  
#include   
using namespace std;  
  
int main ()  
{  
  unsigned int i;  
  vector myvector;  
  
  // set some values (from 1 to 10)  
  for (i=1; i<=10; i++) myvector.push_back(i);  
    
  // erase the 6th element  
  myvector.erase (myvector.begin()+5);  
  
  // erase the first 3 elements:  
  myvector.erase (myvector.begin(),myvector.begin()+3);  
  
  cout << "myvector contains:";  
  for (i=0; i  
#include   
using namespace std;  
  
int main ()  
{  
  vector myvector;  
  
  myvector.push_back(78);  
  myvector.push_back(16);  
  
  // now front equals 78, and back 16  
  
  myvector.front() -= myvector.back();  
  
  cout << "myvector.front() is now " << myvector.front() << endl;  
  
  return 0;  
}  
  
Output:  
myvector.front() is now 62  
12  vector::get_allocator  
allocator_type get_allocator() const;  
// test_vector.cpp : 定义控制台应用程序的入口点。  
//  
  
#include "stdafx.h"  
#include   
#include   
using namespace std;  
  
int _tmain(int argc, _TCHAR* argv[])  
{  
    vector myvector;  
    int* p;  
    unsigned int i;  
  
    p=myvector.get_allocator().allocate(5);  
    for (int i=0;i<5;i++)  
    {  
        p[i]=i;  
    }  
    cout << "The allocated array contains:";  
    for (i=0; i<5; i++) cout << " " << p[i];  
    cout << endl;  
    myvector.get_allocator().deallocate(p,5);  
  
  
    return 0;  
}  
The allocated array contains: 0 1 2 3 4  
  
13  vector::insert  
iterator insert ( iterator position, const T& x );  
    void insert ( iterator position, size_type n, const T& x );  
template   
void insert ( iterator position, InputI  last );  
// test_vector.cpp : 定义控制台应用程序的入口点。  
//  
  
#include "stdafx.h"  
#include   
#include   
using namespace std;  
  
int _tmain(int argc, _TCHAR* argv[])  
{  
    vector myvector(3,100);  
    vector ::iterator it;  
    it=myvector.begin();  
    it=myvector.insert(it,200);  
    myvector.insert(it,2,300);  
  
    it=myvector.begin();  
  
    vector anothervector(2,400);  
    myvector.insert(it+2,anothervector.begin(),anothervector.end());  
      
  
    int array[]={501,502,503};  
    myvector.insert(myvector.begin(),array,array+3);  
  
    cout << "myvector contains:";  
      
    for (vector::iterator it=myvector.begin();it  
#include   
using namespace std;  
  
int main ()  
{  
  vector myvector;  
  
  // set some content in the vector:  
  for (int i=0; i<100; i++) myvector.push_back(i);  
  
  cout << "size: " << myvector.size() << "\n";  
  cout << "capacity: " << myvector.capacity() << "\n";  
  cout << "max_size: " << myvector.max_size() << "\n";  
  return 0;  
}  
A possible output for this program could be:  
size: 100  
capacity: 141  
max_size: 1073741823  
15  vector::operator=  
  
vector& operator= (const vector& x);  
  
重载“=”操作符  
  
// vector assignment  
#include   
#include   
using namespace std;  
  
int main ()  
{  
  vector first (3,0);  
  vector second (5,0);  
  
  second=first;  
  first=vector();  
  
  cout << "Size of first: " << int (first.size()) << endl;  
  cout << "Size of second: " << int (second.size()) << endl;  
  return 0;  
}  
输出结果:  
Size of first: 0  
Size of second: 3  
  
16  vector::operator[]  
  reference operator[] ( size_type n );  
const_reference operator[] ( size_type n ) const;  
  
Returns a reference to the element at position n in the vector container.  
返回vector容器内位置为n的元素的引用  
和at()函数相同的功能,就是at()不在范围内会抛异常  
示例  
// test_vector.cpp : 定义控制台应用程序的入口点。  
//  
  
#include "stdafx.h"  
#include   
#include   
using namespace std;  
  
int _tmain(int argc, _TCHAR* argv[])  
{  
    vector myvector(10);  
    unsigned int i;  
    vector ::size_type size=myvector.size();  
    for (i=0;i<10;i++)  
    {  
        myvector[i]=i;  
    }  
    for (i=0;i  
#include   
using namespace std;  
  
int main ()  
{  
  vector myvector;  
  int sum (0);  
  myvector.push_back (100);  
  myvector.push_back (200);  
  myvector.push_back (300);  
  
  while (!myvector.empty())  
  {  
    sum+=myvector.back();  
    myvector.pop_back();  
  }  
  
  cout << "The elements of myvector summed " << sum << endl;  
  
  return 0;  
}  
输出结果:The elements of myvector summed 600  
18  vector::push_back  
void push_back ( const T& x );  
  
在末尾处添加一个元素  
// vector::push_back  
#include   
#include   
using namespace std;  
  
int main ()  
{  
  vector myvector;  
  int myint;  
  
  cout << "Please enter some integers (enter 0 to end):\n";  
  
  do {  
    cin >> myint;  
    myvector.push_back (myint);  
  } while (myint);  
  
  cout << "myvector stores " << (int) myvector.size() << " numbers.\n";  
  
  return 0;  
}  
19  vector::rbegin  
  
reverse_iterator rbegin();  
const_reverse_iterator rbegin() const;  
  
按照反序迭代输出  
示例  
  
// test_vector.cpp : 定义控制台应用程序的入口点。  
//  
  
#include "stdafx.h"  
#include   
#include   
using namespace std;  
  
int _tmain(int argc, _TCHAR* argv[])  
{  
    vector myvector(10);  
    unsigned int i;  
    vector ::size_type size=myvector.size();  
    for (i=0;i<10;i++)  
    {  
        myvector[i]=i;  
    }  
    vector::reverse_iterator it;  
    for (it=myvector.rbegin();it  
#include   
#include   
using namespace std;  
  
int main ()  
{  
  vector content;  
  size_t filesize;  
  
  ifstream file ("test.bin",ios::in|ios::ate|ios::binary);  
  if (file.is_open())  
  {  
    filesize=file.tellg();  
  
    content.reserve(filesize);  
  
    file.seekg(0);  
    while (!file.eof())  
    {  
      content.push_back( file.get() );  
    }  
  
    // print out content:  
    vector::iterator it;  
    for (it=content.begin() ; it  
#include   
using namespace std;  
  
int main ()  
{  
  vector myvector;  
  
  unsigned int i;  
  
  // set some initial content:  
  for (i=1;i<10;i++) myvector.push_back(i);  
  
  myvector.resize(5);  
  myvector.resize(8,100);  
  myvector.resize(12);  
  
  cout << "myvector contains:";  
  for (i=0;i  
#include   
using namespace std;  
  
int main ()  
{  
  vector myints;  
  cout << "0. size: " << (int) myints.size() << endl;  
  
  for (int i=0; i<10; i++) myints.push_back(i);  
  cout << "1. size: " << (int) myints.size() << endl;  
  
  myints.insert (myints.end(),10,100);  
  cout << "2. size: " << (int) myints.size() << endl;  
  
  myints.pop_back();  
  cout << "3. size: " << (int) myints.size() << endl;  
  
  return 0;  
}  
  
Output:  
0. size: 0  
1. size: 10  
2. size: 20  
3. size: 19  
24  vector::swap  
交换vector对象里的数据  
// swap vectors  
#include   
#include   
using namespace std;  
  
main ()  
{  
  unsigned int i;  
  vector first (3,100);   // three ints with a value of 100  
  vector second (5,200);  // five ints with a value of 200  
  
  first.swap(second);  
  
  cout << "first contains:";  
  for (i=0; i  

你可能感兴趣的:(vector,iostream,iterator,reference,output,insert)