vector::vector
构造一个具有以下特性的矢量:特定大小、特定值的元素、特定分配器或作为其他矢量的全部或部分副本。 元素存储在连续内存块中,内存分配由默认分配器或你提供的自定义分配器管理。
vector( ); explicit vector( const Allocator& Al ); explicit vector( size_type Count ); vector( size_type Count, const Type& Val ); vector( size_type Count, const Type& Val, const Allocator& Al ); vector( const vector& Right ); vector( vector&& Right ); vector( initializer_list<Type> IList, const _Allocator& Al ); template<class InputIterator> vector( InputIterator First, InputIterator Last ); template<class InputIterator> vector( InputIterator First, InputIterator Last, const Allocator& Al );
参数
参数 |
描述 |
Al |
要用于此对象的分配器类。 get_allocator 返回对象的分配器类。 |
Count |
构造的矢量中的元素数。 |
Val |
构造的矢量中的元素值。 |
Right |
要成为副本的构造的矢量中的矢量。 |
First |
要复制的元素范围内的第一个元素的位置。 |
Last |
要复制的元素范围外的第一个元素的位置。 |
IList |
包含要复制的元素的 initializer_list。 |
备注
所有构造函数都存储分配器对象 (Al) 并初始化此矢量。
前两个构造函数指定一个空初始矢量。 第二个构造函数显式指定要使用的分配器类型 (Al)。
第三个构造函数指定类 Type 的默认值的指定数量 (Count) 的元素的重复。
第四个和第五个构造函数指定 (Count) 元素的重复,元素的值为 Val。
第六个构造函数指定矢量 Right 的副本。
第七个构造函数移动矢量 Right。
第八个构造函数使用 initializer_list 指定元素。
第九个和第十个构造函数复制矢量的范围(First、Last)。
示例
// vector_ctor.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>
int main()
{
using namespace std;
vector <int>::iterator v1_Iter, v2_Iter, v3_Iter, v4_Iter, v5_Iter, v6_Iter;
// Create an empty vector v0
vector <int> v0;
// Create a vector v1 with 3 elements of default value 0
vector <int> v1(3);
// Create a vector v2 with 5 elements of value 2
vector <int> v2(5, 2);
// Create a vector v3 with 3 elements of value 1 and with the allocator
// of vector v2
vector <int> v3(3, 1, v2.get_allocator());
// Create a copy, vector v4, of vector v2
vector <int> v4(v2);
// Create a new temporary vector for demonstrating copying ranges
vector <int> v5(5);
for (auto i : v5) {
v5[i] = i;
}
// Create a vector v6 by copying the range v5[_First, _Last)
vector <int> v6(v5.begin() + 1, v5.begin() + 3);
cout << "v1 =";
for (auto& v : v1){
cout << " " << v;
}
cout << endl;
cout << "v2 =";
for (auto& v : v2){
cout << " " << v;
}
cout << endl;
cout << "v3 =";
for (auto& v : v3){
cout << " " << v;
}
cout << endl;
cout << "v4 =";
for (auto& v : v4){
cout << " " << v;
}
cout << endl;
cout << "v5 =";
for (auto& v : v5){
cout << " " << v;
}
cout << endl;
cout << "v6 =";
for (auto& v : v6){
cout << " " << v;
}
cout << endl;
// Move vector v2 to vector v7
vector <int> v7(move(v2));
vector <int>::iterator v7_Iter;
cout << "v7 =";
for (auto& v : v7){
cout << " " << v;
}
cout << endl;
vector<int> v8{ { 1, 2, 3, 4 } };
for (auto& v : v8){
cout << " " << v ;
}
cout << endl;
}
要求
标头:<vector>
命名空间: std