// 111111111111.cpp: 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <map> class FooVector {
std::vector<int> content_;
public:
FooVector(std::initializer_list<int> list) {
for (auto it = list.begin(); it != list.end(); it++) {
content_.push_back(*it);
}
}
}; class FooMap {
std::map<int, int> content_;
using pair_t = std::map<int, int>::value_type;
public:
FooMap(std::initializer_list<pair_t> list) {
for (auto it = list.begin(); it != list.end(); ++it) {
content_.insert(*it);
}
}
}; void test1() {
FooVector foo_1 = { ,,,, };
FooMap foo_2 = { {,},{,},{,} };
} void func(std::initializer_list<int> l) {
for (auto it = l.begin(); it != l.end(); it++) {
std::cout << *it << std::endl;
}
} void test2()
{
func({});
func({ ,, });
} int main()
{
test1();
test2();
return ;
}
 // 111111111111.cpp: 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <functional> void func(void) {
std::cout << __FUNCTION__ << std::endl;
} class Foo {
public:
static int foo_func(int a) {
std::cout << __FUNCTION__ << "(" << a << ") ->:";
return a;
}
}; class Bar {
public:
int operator()(int a) {
std::cout << __FUNCTION__ << "(" << a << ") ->: ";
return a;
}
}; void test1()
{
std::function<void(void)> fr1 = func;
fr1(); std::function<int(int)> fr2 = Foo::foo_func;
std::cout << fr2() << std::endl; Bar bar;
fr2 = bar;
std::cout << fr2() << std::endl;
}
//====================================================================
class A {
std::function<void()> callback_;
public:
A(const std::function<void()>& f) :callback_(f) {}
void notify(void) {
callback_();
}
}; class Foo0 {
public:
void operator()(void) {
std::cout << __FUNCTION__ << std::endl;
}
}; void test2() {
Foo0 foo;
A aa(foo);
aa.notify();
}
//================================================================
void call_when_even(int x, const std::function<void(int)>& f) {
if (!(x & )) {
f(x);
}
} void output(int x) {
std::cout << x << " ";
} void test3() {
for (int i = ; i < ; i++) {
call_when_even(i, output);
}
std::cout << std::endl;
}
//====================================================================
void output_add_2(int x) {
std::cout << x + << " ";
} void test4() {
{
auto fr = std::bind(output, std::placeholders::_1);
for (int i = ; i < ; ++i) {
call_when_even(i, fr);
}
std::cout << std::endl;
}
{
auto fr = std::bind(output_add_2, std::placeholders::_1);
for (int i = ; i < ; ++i) {
call_when_even(i, fr);
}
std::cout << std::endl;
}
} int main()
{
test1();
test2();
test3();
test4();
return ;
}

c++11 初始化列表 bind function 示例的更多相关文章

  1. C++11初始化列表

    [C++11之初始化列表] 在C++03中,在严格遵守POD的定义和限制条件的结构及类型上可以使用初始化列表(initializer list),构想是结构或是数组能够依据成员在该结构内定义的顺序通过 ...

  2. c++11之初始化列表

    一.前言     C++的学习中.我想每一个人都被变量定义和申明折磨过,比方我在大学笔试过的几家公司.都考察了const和变量,类型的不同排列组合,让你差别有啥不同.反正在学习C++过程中已经被折磨惯 ...

  3. C++11绑定器bind及function机制

    前言 之前在学muduo网络库时,看到陈硕以基于对象编程的方式,大量使用boost库中的bind和function机制,如今,这些概念都已引入至C++11,包含在头文件<functional&g ...

  4. C++11 学习笔记 std::function和bind绑定器

    C++11 学习笔记 std::function和bind绑定器 一.std::function C++中的可调用对象虽然具有比较统一操作形式(除了类成员指针之外,都是后面加括号进行调用),但定义方法 ...

  5. C++11新特性——初始化列表 initializer_list

    破事水: 由于最近数据结构有个实验报告说是要对字符串进行排序,想偷个懒不想一个一个地赋值,虽然可以用strcpy和传入二级指针的形式直接写,但是这样感觉不美观漂亮. 然后就去膜了一下C++11的新特性 ...

  6. C++11的初始化列表

      初始化是一个非常重要的语言特性,最常见的就是对对象进行初始化.在传统 C++ 中,不同的对象有着不同的初始化方法,例如普通数组.POD (plain old data,没有构造.析构和虚函数的类或 ...

  7. C++11之列表初始化

    1. 在C++98中,标准允许使用花括号{}来对数组元素进行统一的集合(列表)初始化操作,如:int buf[] = {0};int arr[] = {1,2,3,4,5,6,7,8}; 可是对于自定 ...

  8. 10.C++-构造函数初始化列表、类const成员、对象构造顺序、析构函数

    首先回忆下,以前学的const 单独使用const修饰变量时,是定义的常量,比如:const int i=1; 使用volatile const修饰变量时,定义的是只读变量 使用const & ...

  9. boost::bind应用示例

    // testBind.cpp : Defines the entry point for the console application. // #include "stdafx.h&qu ...

随机推荐

  1. windows环境安装zabbix客户端

    安装windows版zabbix客户端 一.下载客户端 在数据盘新建一个zabbix_agent目录 浏览器打开 https://www.zabbix.com/download_agents 选择”z ...

  2. with check(转)

    假如我要为一个表中添加一个外键约束.语法如下 alter table  dbo.employee with check add constraint [FK_employeeno]  foreign ...

  3. PHP实现curl和snoopy类模拟登陆方法

    Snoopy.class.php下载 方法/步骤   第一种:使用snoopy类实现模拟登陆 1.在网上下载一个Snoopy.class.php的文件   2.代码实现: <?php set_t ...

  4. 每月IT摘录201811

    技术 1.打牢基础,从会使用-了解原理-了解思想一步一步来,最怕基础很弱但却以什么都用过为荣的人,这样的人我招进来也只是初级而已,工作年限再多也没有用.少林里面,有功和拳之分,如蛇拳猴拳是拳,马步功石 ...

  5. 并查集和树的一些性质 hdu1325

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1325 题意是每次输入一对数字n,m表示一条树边,并且n是m的父亲,直到n==0&&m= ...

  6. Postman 测试swagger类接口技巧

    测试情景: 公司的项目接口越来越多,由于开发周期短,时间紧,缺乏接口文档,直接使用swagger记录所有接口信息.那么: 1. 我们如何更快的测试动辄300+数量的接口,如何分组管理和维护这些接口? ...

  7. js文字展示各种滚动效果

    js文字展示各种滚动效果:http://www.dowebok.com/demo/188/

  8. Exceptions

    [定义] error: external, like out of memory exception: internal, like file not found 父类都是throwable 逻辑有错 ...

  9. QQ分享登陆报错

    linker command failed with exit code 1 (use -v to see invocation)报错原因 builtSetting下搜索 bitco 改为NO

  10. java_13.2 Object

    1.类 Object 是类层次结构的根类.每个类都使用 Object 作为超类.所有对象(包括数组)都实现这个类的方法 2.hashCode()方法 返回该对象的哈希值.一般情况下,该方法会根据对象的 ...