std::
forward_list::forward_list
公众成员函数 <forward_list>
C++11; |
default (1) |
explicit forward_list (const allocator_type& alloc = allocator_type()); |
|
fill (2) |
explicit forward_list (size_type n);
explicit forward_list (size_type n, const value_type& val,
const allocator_type& alloc = allocator_type()); |
|
range (3) |
template <class InputIterator>
forward_list (InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type()); |
|
copy (4) |
forward_list (const forward_list& fwdlst);
forward_list (const forward_list& fwdlst, const allocator_type& alloc); |
|
move (5) |
forward_list (forward_list&& fwdlst);
forward_list (forward_list&& fwdlst, const allocator_type& alloc); |
|
initializer list (6) |
forward_list (initializer_list<value_type> il,
const allocator_type& alloc = allocator_type()); |
C++14: |
default (1) |
forward_list();
explicit forward_list (const allocator_type& alloc); |
|
fill (2) |
explicit forward_list (size_type n, const allocator_type& alloc = allocator_type());
explicit forward_list (size_type n, const value_type& val,
const allocator_type& alloc = allocator_type()); |
|
range (3) |
template <class InputIterator>
forward_list (InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type()); |
|
copy (4) |
forward_list (const forward_list& fwdlst);
forward_list (const forward_list& fwdlst, const allocator_type& alloc); |
|
move (5) |
forward_list (forward_list&& fwdlst);
forward_list (forward_list&& fwdlst, const allocator_type& alloc); |
|
initializer list (6) |
forward_list (initializer_list<value_type> il,
const allocator_type& alloc = allocator_type()); |
构建前向链表对象
构造一个forward_list容器对象,根据使用的构造函数重载版本初始化它的内容:
-
(1)空构造函数(默认构造函数)
-
构造一个空容器,没有元素。
-
(2)默认填充构造函数
-
构造一个包含n个元素的容器。每个元素都是val的副本。
-
(3)范围构造函数
-
构造与范围(first,last)相同数量的元素的容器,
每个元素都由该范围中相应的元素以相同的顺序构造.
-
(4)拷贝构造函数
-
使用fwdlst中每个元素的副本以相同的顺序构造容器。
-
(5)移动构造函数(和使用分配器移动)
-
构造一个获取fwdlst元素的容器。
如果指定了alloc并且与fwdlst的allocator不同,则移动元素。
否则,不构造元素(所有权直接转移)。
fwdlst处于未指定但有效的状态。
-
(6)初始化列表构造函数
-
使用il中每个元素的副本以相同的顺序构造容器。
-
容器保存了一个alloc的内部副本,用于分配和释放元素的存储空间,
以及构造和销毁它们(由它的allocator_traits指定)。
如果没有alloc参数传递给构造函数,则使用默认构造的分配器,以下情况除外:
-
- 拷贝构造函数(4,第一类)创建一个容器,
保存并使用通过调用fwdlst分配器上相应的
selected_on_container_copy_construction特性返回的分配器的副本。
- 移动构造函数(5,第一类)获取fwdlst的分配器。
通过使用相应的参数调用allocator_traits::construct,可以复制、移动所有元素,或者以其他方式构造所有元素。
☲ 参数
-
alloc
-
分配器对象。
容器保存并使用这个分配器的内部副本.
成员类型allocator_type是容器使用的内部分配器类型,
在 forward_list中定义为第二个模板形参(Alloc)的别名.
如果allocator_type是默认分配器(没有状态)的实例化,则忽略考虑.
-
n
-
容器初始大小(即构造时容器中元素的数量)。
成员类型size_type是一个无符号整型。
-
val
-
填充容器的值.
容器中的n个元素的每一个都将被初始化为这个值的副本。
成员类型value_type是容器中元素的类型,在forward_list中定义为其第一个模板形参(T)的别名。
-
first, last
-
输入迭代器初始和最终位置的范围。
使用的范围是[first,last),它包括first和last之间的所有元素,
包括first所指向的元素,但不包括last所指向的元素。
函数模板实参InputIterator必须是一个输入迭代器类型,该类型指向可以构造value_type对象的类型的元素。
-
fwdlst
-
用来复制或获取的一个相同类型的 forward_list 对象(具有相同的类模板参数T和Alloc),其内容被复制或获取。
-
il
-
一个initializer_list对象。
这些对象是由初始化列表声明符自动构造的。
成员类型value_type是容器中元素的类型,在 forward_list 中定义为其第一个模板形参(T)的别名。
☣ 示例
// forward_list constructors
#include <iostream>
#include <forward_list>
int main ()
{
// constructors used in the same order as described above:
std::forward_list<int> first; // default: empty
std::forward_list<int> second (3,77); // fill: 3 seventy-sevens
std::forward_list<int> third (second.begin(), second.end()); // range initialization
std::forward_list<int> fourth (third); // copy constructor
std::forward_list<int> fifth (std::move(fourth)); // move ctor. (fourth wasted)
std::forward_list<int> sixth = {3, 52, 25, 90}; // initializer_list constructor
std::cout << "first:" ; for (int& x: first) std::cout << " " << x; std::cout << '\n';
std::cout << "second:"; for (int& x: second) std::cout << " " << x; std::cout << '\n';
std::cout << "third:"; for (int& x: third) std::cout << " " << x; std::cout << '\n';
std::cout << "fourth:"; for (int& x: fourth) std::cout << " " << x; std::cout << '\n';
std::cout << "fifth:"; for (int& x: fifth) std::cout << " " << x; std::cout << '\n';
std::cout << "sixth:"; for (int& x: sixth) std::cout << " " << x; std::cout << '\n';
return 0;
} |
可能输出:
forward_list constructor examples:
first:
second: 77 77 77
third: 77 77 77
fourth:
fifth: 77 77 77
sixth: 3 52 25 90
✥ 复杂度
对于默认构造函数(1)和move构造函数(5)(除非alloc与x的分配器不同)是常量。
对于所有其他情况,结果容器大小是线性的。
☣ 迭代器的有效性
移动构造函数(5),如果元素被移动,则使所有与fwdlst相关的迭代器、指针和引用失效。
⇄ 数据竞争
访问所有复制的元素。移动构造函数(5)修改fwdlst。
☂ 异常安全性
强保证:在抛出异常的情况下没有影响。
如果allocator_traits::construct不支持元素结构的适当参数,
或者如果[first,last)指定的范围无效,它将导致未定义的行为。
🍄 另请参阅
forward_list::operator= |
返回一个指向数组末尾元素的下一个元素的迭代器 (公众成员函数) |
forward_list::assign |
访问最后一个元素(公众成员函数) |
forward_list::resize |
返回指向数组最后一个元素的常量反向迭代器(公众成员函数) |
forward_list::clear |
返回指向数组开始元素前一个元素的常量反向迭代器 (公众成员函数) |