list::list
构造一个列表,它具有特定大小或它的元素具有特定值,或具有特定分配器或作为其他列表的全部或部分副本。
list( ); explicit list( const Allocator& Al ); explicit list( size_type Count ); list( size_type Count, const Type& Val ); list( size_type Count, const Type& Val, const Allocator& Al ); list( const list& Right ); list( list&& Right ); list( initializer_list<Type> IList, const Allocator& Al ); template<class InputIterator> list( InputIterator First, InputIterator Last ); template<class InputIterator > list( InputIterator First, InputIterator Last, const Allocator& Al );
参数
参数 |
描述 |
Al |
要用于此对象的分配器类。 |
Count |
所构造列表中元素的数目。 |
Val |
列表中元素的值。 |
Right |
所构造列表要作为其副本的列表。 |
First |
要复制的范围元素中的第一个元素的位置。 |
Last |
要复制的元素范围以外的第一个元素的位置。 |
IList |
包含要复制的元素的 initializer_list。 |
备注
所有构造函数都存储一个分配器对象 (Al) 并初始化列表。
get_allocator 返回用于构造列表的分配器对象的副本。
第一个构造函数指定一个空的初始列表,第二个指定要使用的分配器类型 (Al)。
第三个构造函数指定特定数目 (Count) 的元素的重复,这些元素具有类 Type 的默认值。
第四个和第五个构造函数指定 (Count) 元素的重复,元素的值为 Val。
第六个构造函数指定列表 Right 的副本。
第七个构造函数移动列表 Right。
第八个构造函数使用 initializer_list 指定元素。
接下来的两个构造函数复制列表的范围 [First, Last)。
所有构造函数均不执行任何临时重新分配。
示例
// list_class_list.cpp
// compile with: /EHsc
#include <list>
#include <iostream>
int main()
{
using namespace std;
// Create an empty list c0
list <int> c0;
// Create a list c1 with 3 elements of default value 0
list <int> c1(3);
// Create a list c2 with 5 elements of value 2
list <int> c2(5, 2);
// Create a list c3 with 3 elements of value 1 and with the
// allocator of list c2
list <int> c3(3, 1, c2.get_allocator());
// Create a copy, list c4, of list c2
list <int> c4(c2);
// Create a list c5 by copying the range c4[_First, _Last)
list <int>::iterator c4_Iter = c4.begin();
c4_Iter++;
c4_Iter++;
list <int> c5(c4.begin(), c4_Iter);
// Create a list c6 by copying the range c4[_First, _Last) and with
// the allocator of list c2
c4_Iter = c4.begin();
c4_Iter++;
c4_Iter++;
c4_Iter++;
list <int> c6(c4.begin(), c4_Iter, c2.get_allocator());
cout << "c1 =";
for (auto c : c1)
cout << " " << c;
cout << endl;
cout << "c2 =";
for (auto c : c2)
cout << " " << c;
cout << endl;
cout << "c3 =";
for (auto c : c3)
cout << " " << c;
cout << endl;
cout << "c4 =";
for (auto c : c4)
cout << " " << c;
cout << endl;
cout << "c5 =";
for (auto c : c5)
cout << " " << c;
cout << endl;
cout << "c6 =";
for (auto c : c6)
cout << " " << c;
cout << endl;
// Move list c6 to list c7
list <int> c7(move(c6));
cout << "c7 =";
for (auto c : c7)
cout << " " << c;
cout << endl;
// Construct with initializer_list
list<int> c8({ 1, 2, 3, 4 });
cout << "c8 =";
for (auto c : c8)
cout << " " << c;
cout << endl;
}
要求
标头:<list>
命名空间: std