std::list<T,Allocator>::list
提供: cppreference.com
list(); |
(1) | |
explicit list( const Allocator& alloc ); |
(2) | |
| (3) | ||
explicit list( size_type count, {{#pad:|4}} const T& value = T(), {{#pad:|4}} const Allocator& alloc = Allocator()); |
(C++11未満) | |
list( size_type count, {{#pad:|4}} const T& value, {{#pad:|4}} const Allocator& alloc = Allocator()); |
(C++11以上) | |
| (4) | ||
explicit list( size_type count ); |
(C++11以上) (C++14未満) |
|
explicit list( size_type count, const Allocator& alloc = Allocator() ); |
(C++14以上) | |
template< class InputIt > list( InputIt first, InputIt last, {{#pad:|4}} const Allocator& alloc = Allocator() ); |
(5) | |
list( const list& other ); |
(6) | |
list( const list& other, const Allocator& alloc ); |
(6) | (C++11以上) |
list( list&& other ); |
(7) | (C++11以上) |
list( list&& other, const Allocator& alloc ); |
(8) | (C++11以上) |
list( std::initializer_list<T> init, {{#pad:|4}} const Allocator& alloc = Allocator() ); |
(9) | (C++11以上) |
様々なデータソースから新しいコンテナを構築します。 オプションでユーザ提供のアロケータ alloc を使用します。
1) デフォルトコンストラクタ。 デフォルト構築されたアロケータを使用して空のコンテナを構築します。
2) 指定されたアロケータ
alloc を使用して空のコンテナを構築します。3) 値
value を持つ要素の count 個のコピーを持つコンテナを構築します。5) 範囲
[first, last) の内容を持つコンテナを構築します。
InputIt が整数型の場合、このコンストラクタは list(static_cast<size_type>(first), static_cast<value_type>(last), a) と同じ効果を持ちます。 |
(C++11未満) |
このオーバーロードは、オーバーロード (3) との曖昧さを回避するため、 InputIt が LegacyInputIterator を満たす場合にのみ、オーバーロード解決に参加します。 |
(C++11以上) |
6) コピーコンストラクタ。
other の内容のコピーを持つコンテナを構築します。 alloc が提供されない場合、アロケータは std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()) を呼んだかのように取得されます。7) ムーブコンストラクタ。 ムーブセマンティクスを用いて
other の内容を持つコンテナを構築します。 アロケータは other の持つアロケータからムーブ構築によって取得されます。 8) アロケータ拡張ムーブコンストラクタ。 新しいコンテナのためのアロケータとして
alloc を使用し、 other の内容をムーブします。 alloc != other.get_allocator() の場合は、要素単位のムーブになります。9) 初期化子リスト
init の内容を持つコンテナを構築します。引数
| alloc | - | このコンテナのすべてのメモリ確保のために使用するアロケータ |
| count | - | コンテナのサイズ |
| value | - | コンテナの要素を初期化するための値 |
| first, last | - | 要素のコピー元の範囲 |
| other | - | コンテナの要素を初期化するソースとして使用する別のコンテナ |
| init | - | コンテナの要素を初期化するための初期化子リスト |
計算量
1-2) 一定。
3-4)
count に比例。5)
first と last の距離に比例。6)
other のサイズに比例。7) 一定。
8)
alloc != other.get_allocator() の場合は other のサイズに比例、そうでなければ一定。9)
init のサイズに比例。例外
Allocator::allocate の呼び出しが例外を投げる可能性があります。
ノート
コンテナのムーブ構築 (オーバーロード (7)) の後、 other を指す参照、ポインタ、イテレータ (終端イテレータは除く) は有効なまま残りますが、以後 *this 内の要素を指すようになります。 現行の標準ではこの保証は [container.requirements.general]/12 の包括的な文言によってなされていますが、より直接的な保証が LWG 2321 で検討されています。
例
Run this code
#include <list>
#include <string>
#include <iostream>
template<typename T>
std::ostream& operator<<(std::ostream& s, const std::list<T>& v)
{
s.put('[');
char comma[3] = {'\0', ' ', '\0'};
for (const auto& e : v) {
s << comma << e;
comma[0] = ',';
}
return s << ']';
}
int main()
{
// C++11 の初期化子リストの構文。
std::list<std::string> words1 {"the", "frogurt", "is", "also", "cursed"};
std::cout << "words1: " << words1 << '\n';
// words2 == words1
std::list<std::string> words2(words1.begin(), words1.end());
std::cout << "words2: " << words2 << '\n';
// words3 == words1
std::list<std::string> words3(words1);
std::cout << "words3: " << words3 << '\n';
// words4 is {"Mo", "Mo", "Mo", "Mo", "Mo"}
std::list<std::string> words4(5, "Mo");
std::cout << "words4: " << words4 << '\n';
}
出力:
words1: [the, frogurt, is, also, cursed]
words2: [the, frogurt, is, also, cursed]
words3: [the, frogurt, is, also, cursed]
words4: [Mo, Mo, Mo, Mo, Mo]
欠陥報告
以下の動作変更欠陥報告は以前に発行された C++ 標準に遡って適用されました。
| DR | 適用先 | 発行時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 2193 | C++11 | the default constructor is explicit | made non-explicit |
関連項目
| コンテナに値を代入します (パブリックメンバ関数) | |
| コンテナに値を代入します (パブリックメンバ関数) |