// 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. 1.3.1、CDH 搭建Hadoop在安装之前(端口---Cloudera Manager和Cloudera Navigator使用的端口)

    下图概述了Cloudera Manager,Cloudera Navigator和Cloudera Management Service角色使用的一些端口: Cloudera Manager和Clou ...

  2. AssetBundle Manager

    [AssetBundle Manager] AssetBundleManager是一个款Unity公司制作的Unity库. 1.Simulation Mode The main advantage o ...

  3. python--第四天总结

    lambda表达式 处理简单函数自动返回 学习条件运算时,对于简单的 if else 语句,可以使用三元运算来表示,即: # 普通条件语句 if 1 == 1: name = 'wupeiqi' el ...

  4. php71

    yum -y install php-mcrypt libmcrypt libmcrypt-devel autoconf freetype gd jpegsrc libmcrypt libpng li ...

  5. c++中的类(class)-----笔记(类模板)

    1,一个模板类至少具有一个类参数,类参数是个符号以表示将要被某个确定数据类型代替的类型. #include<iostream> #include<string> using n ...

  6. cf-Global Round2-E. Pavel and Triangles

    题目链接:http://codeforces.com/contest/1119/problem/E 题意:给定n个数a[i],分别表示长度为2i-1的木条的数量,问使用这些木条最多能构成多少三角形. ...

  7. 江西财经大学第一届程序设计竞赛 H题 求大数的阶乘

    链接:https://www.nowcoder.com/acm/contest/115/H 来源:牛客网 晚上,小P喜欢在寝室里一个个静静的学习或者思考,享受自由自在的单身生活. 他总是能从所学的知识 ...

  8. ORACLE的强制索引

    在一些场景下,可能ORACLE不会自动走索引,这时候,如果对业务清晰,可以尝试使用强制索引,测试查询语句的性能. 以EMP表为例: 先在EMP表中建立唯一索引,如图. 普通搜索: SELECT * F ...

  9. Watcher、ZK状态、事件类型 ,权限

    zookeeper有watch事件,是一次性触发的,当watch监视的数据发生变化时,通知设置了该watch的client,即watcher. 同样,其watcher是监听数据发送了某些变化,那就一定 ...

  10. 什么是QPS,PV

    术语说明: QPS = req/sec = 请求数/秒 [QPS计算PV和机器的方式] QPS统计方式 [一般使用 http_load 进行统计] QPS = 总请求数 / ( 进程总数 * 请求时间 ...