首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::initializer_list

%28不要混淆成员初始化列表29%。

Defined in header <initializer_list>

template< class T > class initializer_list;

(since C++11)

类型对象std::initializer_list<T>是一个轻量级代理对象,它提供对类型对象数组的访问。const T...

std::initializer_list对象在下列情况下自动构造:

  • 带括号的列表用于列表初始化,包括函数调用列表初始化和赋值表达式。
  • 带括号的列表注定要auto,包括在测距回路

初始化程序列表可以实现为一对指针或指针和长度。复制std::initializer_list不复制基础对象。

The underlying array is not guaranteed to exist after the lifetime of the original initializer list object has ended. The storage for std::initializer_list is unspecified (i.e. it could be automatic, temporary, or static read-only memory, depending on the situation).

(until C++14)

The underlying array is a temporary array (until C++17)materialized array prvalue (since C++17) of type const TN, in which each element is copy-initialized (except that narrowing conversions are invalid) from the corresponding element of the original initializer list. The lifetime of the underlying array is the same as any other temporary object, except that initializing an initializer_list object from the array extends the lifetime of the array exactly like binding a reference to a temporary (with the same exceptions, such as for initializing a non-static class member). The underlying array may be allocated in read-only memory.

(since C++14)

The program is ill-formed if an explicit or partial specialization of std::initializer_list is declared.

(since C++17)

成员类型

Member type

Definition

value_type

T

reference

const T&

const_reference

const T&

size_type

std::size_t

iterator

const T*

const_iterator

const T*

成员函数

(constructor)

creates an empty initializer list (public member function)

容量

Size返回初始化程序列表%28公共成员函数%29中的元素数

迭代器

BEGIN返回指向第一个元素%28公共成员函数%29的指针

End返回指向最后一个元素%28公共成员函数%29的指针

非会员职能

std::begin(std::initializer_list) (C++11)

specializes std::begin (function template)

std::end(std::initializer_list) (C++11)

specializes std::end (function template)

在标头中定义<iterator>

RBEGIN%28std::初始化器[医]列表%29%28C++14%29专业性病::rback%28功能%29

rend%28 std::初始值[医]列表%29%28C++14%29专业性病::rend%28功能%29

二次

代码语言:javascript
复制
#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 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 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
}

二次

产出:

二次

代码语言:javascript
复制
constructed with a 5-element list
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

二次

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券