Translations of this page?:

Vector constructors(Vector 构造函数)

Syntax: 语法:

    #include <vector>
    using namespace std;
    vector();
    vector( const vector& c );
    explicit vector( size_type num, const T& val = T() );
    template< typename input_iterator >
    vector( input_iterator start, input_iterator end );
    ~vector();

The default vector constructor takes no arguments, creates a new instance of that vector.

vector 默认构造函数不带参数,只创建一个 vector 实例。

The second constructor is a default copy constructor that can be used to create a new vector that is a copy of the given vector c.

第二个构造函数是默认的复制构造函数,它会创建一个给定 vector c 的复本 vector。

The third constructor creates a vector with num objects. If val is specified, each of those objects will be given that value, otherwise, those objects are given T's default constructor's value. For example, the following code creates a vector consisting of five copies of the integer 42:

第三个构造函数创建了一个有 num 个对象的 vector。如果 val 值给定,所有的对象都被赋上 val 值, 否则就会被赋上 T 的默认构造函数的值,下面的代码会创建一个有 5 个整数42 构成的 vector。

   vector<int> v1( 5, 42 );

The last constructor creates a vector that is initialized to contain the elements between start and end. For example:

最后一个构造函数创建了一个包含了从 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<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;
   }

When run, this code displays the following output: 运行上述代码将会显示下面的结果:

   original vector: 1 9 7 9 2 7 2 1 9 8
   first even number: 2, last even number: 8
   new vector: 2 7 2 1 9

The last constructor provides for a handy way to initialize an STL vector with data from an old style array.

最后一个构造函数提供了一个手动从原始风格 array 中获取数据来初始化 STL vector 的方法,

Example(例子):

    float fp_values[] = { 0.1, 0.2 , 0.3, 0.4};      // somewhere an array is created(在这里创建一个数组)
...
    vector<float> fp_vector(fp_values,fp_values+4);  // elements in the array are copied into fp_vector(数组里的元素都被复制到 fp_vector 中了)

Keep in mind that pointers are just a possible elementary form of iterators.

记住指针也是一种可接受的基本迭代器类型

All of these constructors run in linear time except the first, which runs in constant time.

所有这些构造函数都是以 linear time 运行,除了第一个是以 constant time 运行。

The default destructor is called for each element when the vector is destroyed.

 
• • • SitemapRecent changesRSScc