名稱空間
變體
操作

std::initializer_list

來自 cppreference.com
< cpp‎ | 工具
 
 
 
 

(不要與 成員初始化列表 混淆)

定義於標頭檔案 <initializer_list>
template< class T >
class initializer_list;
(C++11 起)

std::initializer_list<T> 型別的物件是一個輕量級代理物件,它提供對 const T 型別物件陣列的訪問(這些物件可能分配在只讀記憶體中)。

在以下情況下,會自動構造 std::initializer_list 物件:

std::initializer_list 可以實現為一對指標或指標和長度。複製 std::initializer_list 不會複製對應的初始化列表的支援陣列

如果聲明瞭 std::initializer_list 的顯式或部分特化,則程式非良構。

目錄

[編輯] 成員型別

名稱 定義
value_type T
reference const T&
const_reference const T&
size_type std::size_t
iterator const T*
const_iterator const T*

[編輯] 成員函式

建立空的初始化列表
(公共成員函式) [編輯]
容量
返回初始化列表中元素的數量
(公共成員函式) [編輯]
迭代器
返回指向第一個元素的指標
(公共成員函式) [編輯]
返回指向最後一個元素之後位置的指標
(公共成員函式) [編輯]

[編輯] 非成員函式

過載 std::begin
(函式模板) [編輯]
特化 std::end
(函式模板) [編輯]
針對 std::initializer_list 過載的自由函式模板
返回指向容器或陣列開頭的反向迭代器
(函式模板) [編輯]
(C++14)
返回容器或陣列的反向結束迭代器
(函式模板) [編輯]
(C++17)
檢查容器是否為空
(函式模板) [編輯]
(C++17)
獲取指向底層陣列的指標
(函式模板) [編輯]

[編輯] 註解

功能測試宏 標準 特性
__cpp_initializer_lists 200806L (C++11) 列表初始化std::initializer_list

[編輯] 示例

#include <cassert>
#include <initializer_list>
#include <iostream>
#include <vector>
 
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 T*, std::size_t> c_arr() const
    {
        return {&v[0], v.size()}; // copy list-initialization in return statement
                                  // this is NOT a use of std::initializer_list
    }
};
 
template<typename T>
void templated_fn(T) {}
 
int main()
{
    S<int> s = {1, 2, 3, 4, 5}; // copy list-initialization
    s.append({6, 7, 8});        // list-initialization in function call
 
    std::cout << "The vector now has " << 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';
    auto la = al; // a shallow-copy of top-level proxy object
    assert(la.begin() == al.begin()); // guaranteed: backing array is the same
 
    std::initializer_list<int> il{-3, -2, -1};
    assert(il.begin()[2] == -1); // note the replacement for absent operator[]
    il = al; // shallow-copy
    assert(il.begin() == al.begin()); // guaranteed
 
//  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
}

輸出

constructed with a 5-element list
The vector now has 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

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 2129 C++11 std::initializer_list 可以有顯式
特化或部分特化
在這種情況下程式是
非良構的

[編輯] 參閱

(C++20)
一個連續物件序列的非擁有檢視
(類模板) [編輯]
只讀字串檢視
(類模板) [編輯]