// 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. cdh 安装系列3--cdh manager 安装 cdh 6.01 版本

    安装前提是cdh manager 已经可以通过admin登录,管理台安装在192.168.20.163 一.安装自动TLS Setup Auto-TLS 1.ssh 192.168.20.163 2. ...

  2. canvas动画---- 太阳、地球、月球

    <div> <canvas id="canvas" width="500" height="500"></ca ...

  3. 根据23423条件,截取字段‘abdecsdsadsadsad’,以ab/dec/sdsa/ds/ads 输出

    create or replace procedure p_getString ( p_finalString out varchar2, p_rulestring in number, p_sour ...

  4. ELK Deployed

    Enviroment prepare rpm -qa | grep java wget http://download.oracle.com/otn-pub/java/jdk/8u171-b11/51 ...

  5. CentOS ln 链接

    ln -s /u01/app/oracle/product/11.2.0/dbhome_1/bin/sqlplus/ /usr/bin (解决bash: sqlplus: command not fo ...

  6. npm、webpack、vue-cli快速上手版

    node.js和npm npm的安装和更新 Node.js下载安装,npm自带的包管理工具. 查看安装版本信息: node -v 查看node.js版本信息 npm -v 查看npm版本信息 更新np ...

  7. Android 环境搭建与Android SDK目录介绍

    Android SDK下载和安装 本地已有合适版本Android SDK,则无需再下载,或者可以使用SDK Manager更新SDK: 没有SDK,则需要下载. 这里说一下使用SDK Manager下 ...

  8. 15. 3Sum (重新分配数组大小)

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  9. Lua的闭包详解(终于搞懂了)

    词法定界:当一个函数内嵌套另一个函数的时候,内函数可以访问外部函数的局部变量,这种特征叫做词法定界 table.sort(names,functin (n1,n2) return grades[n1] ...

  10. Python开发【第二篇】:Python基本数据类型

    运算符 设定:a=10,b=20 . 算数运算 2.比较运算 3.赋值运算 4.逻辑运算 5.成员运算 基本数据类型 1.数字 int(整型)     在32位机器上,整数的位数为32位,取值范围为- ...