看到这么一个东西,可以实现花括号( "{" "}" )初始化容器类。

使用时需包含头文件

#include <initialize_list>

我们都看过这样的代码

vector<int> arr = { ,,,, };

或者

vector<int> arr{ ,,,, };

右边那个花括号返回的类型便是initialize_list

我们可以在自己的类中这么用

class foo {
public:
std::vector<int> data;
//构造函数里放上initialize_list
foo() {}
foo(std::initializer_list<int> list) :data(list) {} void print() {
for (auto item : data) {
std::cout << item;
}
std::cout << endl;
}
};

试验一下

int main() {
foo test1{ ,,,, };
foo test2 = { ,,,, };
test1.print();
test2.print();
return ;
}

可以正常输出

cppreference 的测试代码如下:可以看到这个东西的花样还是挺多的

 #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[], 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 = { , , , , }; // copy list-initialization
s.append({ , , }); // 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 : {-, -, -}) // the rule for auto makes this ranged-for work
std::cout << x << ' ';
std::cout << '\n'; auto al = { , , }; // 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>>({ , , }); // OK
templated_fn<std::vector<int>>({ , , }); // also OK
}

c++ initialize_list的更多相关文章

  1. C++11 中的initialize_list

    这就是一个简单的模板类,不过在C++中有了特殊的语法支持,定义的时候使用如下的格式: initialize_list<double> dl = {1.1, 1.2}; 或者: initia ...

  2. 深入理解C++11【2】

    [深入理解C++11[2]] 1.继承构造函数. 当基类拥有多个构造函数的时候,子类不得不一一实现. C++98 可以使用 using 来使用基类的成员函数. #include < iostre ...

  3. c++ primer plus 第6版 部分三 9章 - 章

    c++ primer plus 第6版                                               部分三 9章 - 章 第9章   内存模型和名称空间 1.单独编译 ...

  4. 几道leetcode不会做的题目

    1.set没有back()函数,今天想到用这个,才发现没有. 2. tuple的initialize_list construct好像不能使用,其实之前没使用过tuple,都是pair,复杂一点的自己 ...

  5. c++11特性学习总结

    ubuntu 16.04 自带gcc 5.4 支持c++11 ubuntu 18.04 自带gcc 7.3 支持c++14 查看编译器支持: c++11 c++14 c++17 c++11 featu ...

  6. 第2课 auto类型推导(1)

    第2课 auto类型推导(1) 一.auto类型推导 (一)与模板类型推导映射关系 1.auto类型推导与模板类型推导可以建立一一映射关系,它们之间存在双向的算法变换.auto扮演模板中T的角色,而变 ...

  7. 深入理解C++11 C3

    继承构造函数 class A { public: A(int i):m_i(i) {} A(double d, int i):m_d(d),m_i(i){} private: int m_i{0}; ...

  8. C++11标准特性的一些理解

    (1)auto 和 decltype 关键字 在C++11之前,auto关键字用来指定存储期(C++98中指的是自动生命周期).在新标准中,它的功能变为类型推断.C++11引入auto关键词与之前C语 ...

  9. 简单C++线程池

    简单C++线程池 Java 中有一个很方便的 ThreadPoolExecutor,可以用做线程池.想找一下 C++ 的类似设施,尤其是能方便理解底层原理可上手的.网上找到的 demo,基本都是介绍的 ...

随机推荐

  1. 【循序渐进学Python】3. Python中的序列——字符串

    字符串是零个或多个的字符所组成的序列,字符串是Python内建的6种序列之一,在Python中字符串是不可变的. 1. 格式化字符串 字符串格式化使用字符串格式化操作符即百分号%来实现.在%左侧放置一 ...

  2. 与众不同 windows phone (47) - 8.0 其它: 锁屏信息和锁屏背景, 电池状态, 多分辨率, 商店, 内置协议, 快速恢复

    [源码下载] 与众不同 windows phone (47) - 8.0 其它: 锁屏信息和锁屏背景, 电池状态, 多分辨率, 商店, 内置协议, 快速恢复 作者:webabcd 介绍与众不同 win ...

  3. 线段树区间求最大值(点更新)---I Hate It

    HDU   1754 Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的 ...

  4. Chrome浏览器的Timing分析

    以W3C网站为例: Stalled是浏览器得到要发出这个请求的指令,到请求可以发出的等待时间,一般是代理协商.以及等待可复用的TCP连接释放的时间,不包括DNS查询.建立TCP连接等时间等. SSL( ...

  5. jsp页面 列表 展示 ajax异步实现

    1. 服务端先返回页面基本结构(如message.jsp), <%@ page language="java" contentType="text/html; ch ...

  6. Delphi的几个跨平台小游戏例子。

    Embarcadero开源了几个FireMonkey的小游戏,支持Windows, Android,Ios, MacOS等. 源码地址: https://github.com/EmbarcaderoP ...

  7. 一、PBNI环境搭建及初步使用

    PowerBuilder Native Interface(PowerBuilder本机接口PBNI)允许将第3方程序转换为PowerBuilder对象,供PowerBuilder直接使用,也允许将P ...

  8. IntelliJ和tomcat中的目录结构

    IntelliJ和tomcat中的目录结构   IntelliJ的官网帮助中心:http://www.jetbrains.com/idea/webhelp/getting-help.html   pr ...

  9. SQL数据库基础(六)

    子查询,又叫做嵌套查询. 将一个查询语句做为一个结果集供其他SQL语句使用,就像使用普通的表一样,被当作结果集的查询语句被称为子查询. 子查询有两种类型: 一种是只返回一个单值的子查询,这时它可以用在 ...

  10. SilverLight MD5加密

    效果体验:http://keleyi.com/tool/md5.htm 嵌入页面的代码: <div style="width:400px;height:230px"> ...