名前空間
変種
操作

std::initializer_list

提供: cppreference.com
< cpp‎ | utility


 
 
ユーティリティライブラリ
支援を入力します (basic types, RTTI, type traits)
動的なメモリ管理
エラー処理
プログラムユーティリティ
可変個引数関数
日付と時刻
関数オブジェクト
initializer_list(C++11)
bitset
hash(C++11)
関係演算子
Original:
Relational operators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rel_ops::operator!=
rel_ops::operator>
rel_ops::operator<=
rel_ops::operator>=
ペアとタプル
Original:
Pairs and tuples
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
pair
tuple(C++11)
piecewise_construct_t(C++11)
piecewise_construct(C++11)
フォワード、スワップ、および移動
Original:
Swap, forward and move
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
swap
forward(C++11)
move(C++11)
move_if_noexcept(C++11)
declval(C++11)
 
std::initializer_list
メンバ関数
Original:
Member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
initializer_list::initializer_list
容量
Original:
Capacity
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
initializer_list::size
イテレータ
Original:
Iterators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
initializer_list::begin
initializer_list::end
非メンバ関数
Original:
Non-member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
std::begin
std::end
 
Defined in header <initializer_list>
template< class T >
class initializer_list;
(C + + 11以来)
タイプstd::initializer_list<T>の目的は、状況に応じて、一時的な自動、また​​は静的な読み取り専用メモリの場合もある(不特定のストレージに実装によって割り当てられた、型Tのオブジェクトの配列へのアクセスを提供する軽量のプロキシオブジェクトであり、 )
Original:
An object of type std::initializer_list<T> is a lightweight proxy object, which provides access to an array of objects of type T, allocated by the implementation in unspecified storage (which could be automatic, temporary, or static read-only memory, depending on the situation)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
この配列は初期化され、ブレース-initのリストは関数呼び出しリストの初期化と代入式(std::initializer_listと混同しないこと)を含め、リスト初期化で使用されている場合コンストラクタ初期化子リストオブジェクトが構築されるとき、またはブレース- INIT-listを、ループの範囲であったのを含め、autoにバインドされています。
Original:
This array is initialized and a std::initializer_list object is constructed when a braced-init-list is used in リスト初期化, including function-call list initialization and assignment expression (not to be confused with コンストラクタ初期化子リスト), or when braced-init-list is bound to auto, including in a ranged for loop.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
初期化子リストは、ポインタやポインタや長さのペアとして実装することができる。 std::initializer_listをコピーすると、基礎となるオブジェクトはコピーされません。オリジナルの初期化子リストオブジェクトの有効期間が終了した後、基になる配列が存在することは保証されません。
Original:
Initializer list may be implemented as a pair of pointers or pointer and length. Copying a std::initializer_list does not copy the underlying objects. The underlying array is not guaranteed to exist after the lifetime of the original initializer list object has ended.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目次

[編集] メンバータイプ

メンバー·タイプ
Original:
Member type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Definition
value_type T
reference const T&
const_reference const T&
size_type size_t
iterator const T*
const_iterator const T*

[編集] メンバ関数

空の初期化リストを作成します
Original:
creates an empty initializer list
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(パブリックメンバ関数) [edit]
容量
Original:
Capacity
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
初期化子リスト内の要素数を返します
Original:
returns the number of elements in the initializer list
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(パブリックメンバ関数) [edit]
イテレータ
Original:
Iterators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
ポインタが最初の要素を返します
Original:
returns a pointer the first element
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(パブリックメンバ関数) [edit]
最後の要素は、過去1へのポインタを返します
Original:
returns a pointer to one past the last element
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(パブリックメンバ関数) [edit]

[編集] 非メンバ関数

std::beginを専門としています
Original:
specializes std::begin
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(関数テンプレート) [edit]
std::endを専門としています
Original:
specializes std::end
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(関数テンプレート) [edit]

[編集]

#include <iostream>
#include <vector>
#include <initializer_list>
 
template<class T>
struct S {
    std::vector<T> v;
    S(std::initializer_list<T> l) : v(l) {
         std::cout << "constructed with a " << l.size() << "-element list\n";
    }
    void append(std::initializer_list<T> l) {
        v.insert(v.end(), l.begin(), l.end());
    }
    std::pair<const int*, size_t> c_arr() const {
        return {&v[0], v.size()};  // list-initialization in return statement
    }
};
 
template<typename T>
void templated_fn(T) { }
 
int main()
{
    S<int> s = {1,2,3,4,5}; // direct list-initialization
    s.append({6,7,8});      // list-initialization in function call
    std::cout << "The vector size is now " << s.c_arr().second << " ints:\n";
    for(auto n : s.v) std::cout << ' ' << n;
    std::cout << '\n';
 
    std::cout << "range-for over brace-init-list: \n";
    for(int x : {-1, -2, -3}) // the rule for auto makes this ranged for work
        std::cout << x << ' ';
    std::cout << '\n';
 
    auto al = {10, 11, 12};   // special rule for auto
    std::cout << "The list bound to auto has size() = " << al.size() << '\n';
 
//    templated_fn({1,2,3}); // compiler error! "{1,2,3}" is not an expression,
                             // it has no type, and so T cannot be deduced
    templated_fn<std::initializer_list<int>>({1,2,3}); // OK
    templated_fn<std::vector<int>>({1,2,3});           // also OK
}

Output:

The vector size is now 8 ints:
 1 2 3 4 5 6 7 8
range-for over brace-init-list: 
-1 -2 -3 
The list bound to auto has size() = 3