c++11 初始化列表 bind function 示例
// 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 示例的更多相关文章
- C++11初始化列表
[C++11之初始化列表] 在C++03中,在严格遵守POD的定义和限制条件的结构及类型上可以使用初始化列表(initializer list),构想是结构或是数组能够依据成员在该结构内定义的顺序通过 ...
- c++11之初始化列表
一.前言 C++的学习中.我想每一个人都被变量定义和申明折磨过,比方我在大学笔试过的几家公司.都考察了const和变量,类型的不同排列组合,让你差别有啥不同.反正在学习C++过程中已经被折磨惯 ...
- C++11绑定器bind及function机制
前言 之前在学muduo网络库时,看到陈硕以基于对象编程的方式,大量使用boost库中的bind和function机制,如今,这些概念都已引入至C++11,包含在头文件<functional&g ...
- C++11 学习笔记 std::function和bind绑定器
C++11 学习笔记 std::function和bind绑定器 一.std::function C++中的可调用对象虽然具有比较统一操作形式(除了类成员指针之外,都是后面加括号进行调用),但定义方法 ...
- C++11新特性——初始化列表 initializer_list
破事水: 由于最近数据结构有个实验报告说是要对字符串进行排序,想偷个懒不想一个一个地赋值,虽然可以用strcpy和传入二级指针的形式直接写,但是这样感觉不美观漂亮. 然后就去膜了一下C++11的新特性 ...
- C++11的初始化列表
初始化是一个非常重要的语言特性,最常见的就是对对象进行初始化.在传统 C++ 中,不同的对象有着不同的初始化方法,例如普通数组.POD (plain old data,没有构造.析构和虚函数的类或 ...
- C++11之列表初始化
1. 在C++98中,标准允许使用花括号{}来对数组元素进行统一的集合(列表)初始化操作,如:int buf[] = {0};int arr[] = {1,2,3,4,5,6,7,8}; 可是对于自定 ...
- 10.C++-构造函数初始化列表、类const成员、对象构造顺序、析构函数
首先回忆下,以前学的const 单独使用const修饰变量时,是定义的常量,比如:const int i=1; 使用volatile const修饰变量时,定义的是只读变量 使用const & ...
- boost::bind应用示例
// testBind.cpp : Defines the entry point for the console application. // #include "stdafx.h&qu ...
随机推荐
- depth: working copy\infinity\immediates\files\empty
depth: working copy\infinity\immediates\files\empty 有时间,需要整理下,svn 合并深度这四项:具体的意思.
- Transparency Sort Mode
[Transparency Sort Mode] Transparency Sort Mode, which allows you to control how Sprites are sorted ...
- chromedriver和chrome匹配的版本
- C/s程序过时了吗?
目前的程序从原来的形态演变成了 C/s,B/s,和手机端. 其实应该各有自己的客户群,及定位. 比如C/s为单机版的可以完成个性化突出的复杂客户端应用,企业级别的应用. B/s的特点安装简单,功能制作 ...
- js常用返回网页顶部几种方法
一.使用锚标记 此方法最简单,只需在body下放个隐藏的锚点标记,内容如下: 代码如下 复制代码 <a name="top" id="top">& ...
- VS2013 warning C4018 "<” 有符号/无符号不匹配
1, VS2013 warning C4018 "<” 有符号/无符号不匹配" 警告 出错代码: void show(const vector<int>& ...
- 树形DP+RMQ+尺取法 hdu4123
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4123 参考博客:两种解法-树形dp+二分+单调队列(或RMQ)-hdu-4123-Bob’s Race ...
- 倒计时问题java
public static void main(String args[]){ Scanner sc = new Scanner(); int x = sc.nextInt(); System.out ...
- 179. Largest Number(INT, String)
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- unity中Camera.ScreenToWorldPoint
Camera.ScreenToWorldPointVector3 ScreenToWorldPoint(Vector3 position); 将屏幕坐标转换为世界坐标. 如何转换?假如给定一个所谓的屏 ...