c++ initialize_list
看到这么一个东西,可以实现花括号( "{" "}" )初始化容器类。
使用时需包含头文件
#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的更多相关文章
- C++11 中的initialize_list
这就是一个简单的模板类,不过在C++中有了特殊的语法支持,定义的时候使用如下的格式: initialize_list<double> dl = {1.1, 1.2}; 或者: initia ...
- 深入理解C++11【2】
[深入理解C++11[2]] 1.继承构造函数. 当基类拥有多个构造函数的时候,子类不得不一一实现. C++98 可以使用 using 来使用基类的成员函数. #include < iostre ...
- c++ primer plus 第6版 部分三 9章 - 章
c++ primer plus 第6版 部分三 9章 - 章 第9章 内存模型和名称空间 1.单独编译 ...
- 几道leetcode不会做的题目
1.set没有back()函数,今天想到用这个,才发现没有. 2. tuple的initialize_list construct好像不能使用,其实之前没使用过tuple,都是pair,复杂一点的自己 ...
- 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 ...
- 第2课 auto类型推导(1)
第2课 auto类型推导(1) 一.auto类型推导 (一)与模板类型推导映射关系 1.auto类型推导与模板类型推导可以建立一一映射关系,它们之间存在双向的算法变换.auto扮演模板中T的角色,而变 ...
- 深入理解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}; ...
- C++11标准特性的一些理解
(1)auto 和 decltype 关键字 在C++11之前,auto关键字用来指定存储期(C++98中指的是自动生命周期).在新标准中,它的功能变为类型推断.C++11引入auto关键词与之前C语 ...
- 简单C++线程池
简单C++线程池 Java 中有一个很方便的 ThreadPoolExecutor,可以用做线程池.想找一下 C++ 的类似设施,尤其是能方便理解底层原理可上手的.网上找到的 demo,基本都是介绍的 ...
随机推荐
- RSA密钥——JAVA与C#的区别和联系
PS:好久没写博了,最近在考虑以后的事情,而且手上杂事也比较多,终于得空来写两篇. 首先感谢:http://www.codeproject.com/Articles/25487/Cryptogra ...
- sql: table,view,function, procedure created MS_Description in sql server
--添加描述 geovindu --https://msdn.microsoft.com/en-us/library/ms180047.aspx --https://msdn.microsoft.co ...
- windows下react-native环境搭建
首先不得不先吐槽一下自己,一个坑总是踩很多次,且乐此不疲. 咋办? 写博客记录记录呗. 零.记录的点 Java环境的下载与配置 Android环境的下载与配置 Node环境的下载与配置 创建第一个re ...
- inotify--内核中文件系统的通知机制
转载:http://www.ibm.com/developerworks/cn/linux/l-inotifynew/index.html 一. 引言 众所周知,Linux 桌面系统与 MAC 或 W ...
- Ajax基础实例
前端代码 <script type="text/javascript"> var xmlhttp; function go(url) { xmlhttp=null; i ...
- Python科学计算——前期准备
1.开发环境搭建 Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年发明,第一个公 ...
- R 网页数据爬虫1
For collecting and analyzing data. [启示]本处所分享的内容均是笔者从一些专业书籍中学习所得,也许会有一些自己使用过程中的技巧.心得.小经验一类的,但远比不上书中所讲 ...
- .NET Core Roadmap
This post was written by Scott Hunter. It has been about two weeks since we shipped .NET Core / ASP. ...
- mysql root强密码的必要性max_allowed_packet被改成1024引起的风险
前两天运维反馈说,有些机器的max_allowed_packet隔两天就会被改成1024,导致客户端调用时出错,网上有说内存不够的,也有人工修改的. 运维小姑娘一口咬定肯定没有改过的,而且my.cnf ...
- [ASP.NET MVC] 使用CLK.AspNet.Identity提供以角色为基础的访问控制(RBAC)
[ASP.NET MVC] 使用CLK.AspNet.Identity提供以角色为基础的访问控制(RBAC) 程序代码下载 程序代码下载:点此下载 前言 ASP.NET Identity是微软所贡献的 ...