// 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 ;
}