std::initializer_list

来自cppreference.com

 
 
实用工具库
类型的支持 (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.
该数组初始化一个std::initializer_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]
返回一个指针,指向一个过去的最后一个元素
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