Vector容器 构造函数

 

=====Vector constructors(Vector 构造函数)=====
+Syntax:
+语法:
+
+<code>
+    #include <vector>
+    using namespace std;
+    vector();
+    vector( const vector& c );
+    explicit vector( size_type num, const TYPE& val = TYPE() );
+    template< typename input_iterator >
+    vector( input_iterator start, input_iterator end );
+    ~vector();
+</code>
+
+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 TYPE'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 值, 否则就会被赋上 TYPE 的默认构造函数的值,下面的代码会创建一个有 5
+ 个整数42 构成的 vector。
+
+<code>
+   vector<int> v1( 5, 42 );
+</code>
+
+The last constructor creates a vector that is initialized to contain the
+elements between start and end. For example:
+
+最后一个构造函数创建了一个包含了从 start 到 end 之间的所有元素的 vector。例如:
+
+<code>
+   // 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;
+   }
+</code>
+
+When run, this code displays the following output:
+运行上述代码将会显示下面的结果:
+
+<code terminal>
+   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
+</code>
+
+The last constructor provides for a handy way to initialize an STL vector with data from an old style array.
+
+最后一个构造函数提供了一个手动从原始风格 array 中获取数据来初始化 STL vector 的方法,
+
+Example(例子):
+
+<code>
+    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 中了)
+</code>
+
+Keep in mind that pointers are just a possible elementary form of iterators.
+
+记住指针也是一种可接受的基本迭代器类型
+
+All of these constructors run in [[/complexity|linear time]] except the first, which runs in
+[[/complexity|constant time]].
+
+所有这些构造函数都是以 [[/complexity|linear time]] 运行,除了第一个是以 [[/complexity|constant time]] 运行。
+
+The default destructor is called for each element when the vector is destroyed.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值