美文网首页C++C++2.0
C++ 11的一些新特性(1)

C++ 11的一些新特性(1)

作者: 爱秋刀鱼的猫 | 来源:发表于2018-04-23 11:37 被阅读4次

最近学习了一下C++11的新特性,发现c++的进步很快,已经不在是我最早认识的那个c++了。

我的感觉是c++的更新的三个方向:

  1. 一个是语法层面上的
    语法层面上面应该是借鉴了像python语言这样的语法,比如for -each,auto 等这样的“语法糖”。

  2. STL / 第三方库 下面的更新
    比如加入unorder_map / set 等
    thread库,之前c++在window下和linux创建线程的api完全不一样,但是c++11之后,可以使用头文件thread,基本上统一了跨平台的问题。我觉得这个很好。

  1. c++核心的更新
    比如加入智能指针,functional,lambda函数,右值引用等

1. 指针指针
2. lambda 函数
3. STL unorder_map/set
4. funtion

function的机制有点像python里面 的传递一个函数。

#include <iostream>
#include <list>
#include <functional>
using namespace std;

int print_func(int val1, float val2) {
    cout << "val1 + val2 : " << val1 + val2 << endl;
    return val1 + val2;
}

int print_func2() {
    cout << "print_func" << endl;
    return 1;
}

int main()
{
    //function 有点像python里面 的传递一个函数
    function<int(int, float)> fun_name(print_func);  //参数是在这里是不可以带的
    int ret = fun_name(10, 20.0);                    //在调用的时候才可以调用
    cout << ret << endl;


    //使用function 实现任务队列
    list<function<void(void)>> task_list;
    //添加任务
    for (int i = 0; i < 10; i++) {
        function<void(void)> task(print_func2);
        task_list.push_back(task);
    }
    //拿任务干活
    while (!task_list.empty()) {
        function<void(void)> cur_task = task_list.front();
        task_list.pop_front();
        cur_task();
    }


    //上面的那个例子是不带参数的,实现一个带参数的版本
    list<function<int(void)>> task_list2;
    //添加任务
    for (int i = 0; i < 10; i++) {
        function<int(void)> task = bind(print_func, i, float(i * 2));  //使用bind将参数绑定
        task_list2.push_back(task);
    }
    //拿任务干活
    while (!task_list2.empty()) {
        function<int(void)> cur_task = task_list2.front();
        task_list2.pop_front();
        cur_task();
    }
}
5. bind函数 / 仿函数 / 占位符

这个有点像python里面的高阶函数的东西,就是把函数作为一个参数进行传递。

下面几段代码,可以理解一下

  • 代码1
    下面的下划线表示占位符,_1 表示的是函数的参数是不确定的,后面传递什么就是什么。
using namespace std::placeholders;

int mutiply(int a, int b) {
  return a * b;
}

int main() 
{ 
  auto f = bind(mutiply, 5, _1);
  for (int i = 0; i<10; i++) {
      cout << "5*" << i << "=" << f(i) << endl;
  }
}
  • 代码2
    这里分别有三个占位符,是_1 ,_2 ,_3 。
    _1 表示的是表示函数传递过来的第一个参数,_2表示函数传递过来的第二个参数,_3表示函数传递过来的第三个参数。
using namespace std::placeholders;

void show(const string& a, const string&b, const string&c) {
  cout << a << ":" << b << ":" << c << ";" << endl;
}

int main() 
{ 
  auto x = bind(show, _1, _2, _3); 
  auto y = bind(show, _2, _3, _1); 
  auto z = bind(show, "hello", _2, _1); 

  x("one", "two", "three"); 
  y("one", "two", "three"); 
  z("one", "two");

  return 0;
}

输出:
one:two:three;
two:three:one;
hello:two:one;

代码3:

#include <functional>
#include <iostream>
#include <string> 
#include <vector> 
using namespace std; 
void excute(const vector<function<void ()>>& fs) 
{ 
  for(auto&f:fs)
  { f(); } 
} 

void plain_old_func()
{ 
  cout<<"I'am old plain"<<endl; 
}

class functor
{ 
  public: 
  void operator()() { 
      cout<<"I'am a functor"<<endl; 
  } 
}; 

int main() 
{ 
  vector<function<void()>> x; 
  x.push_back(plain_old_func); 
  functor instance; 
  x.push_back(instance); 
  x.push_back([](){ cout<<"HI,I am lamda expression"<<endl; }); 
  excute(x); 

  system("pause");
  return 0; 
} 

代码4

#include <iostream>
#include <functional>
#include <memory>
#include <thread>
using namespace std;

class A
{
public:
  int Func(int x, int y)
  {
      return x + y;
  }
  void testfunc(void) {
      cout << "wahahah" << endl;
  }
};
int main() {
  A a;
  //知识点1 : 
  //bf2把一个类成员函数绑定了类对象,生成了一个像普通函数一样的新的可调用实体
  auto bf2 = std::bind(&A::Func, a, std::placeholders::_1, std::placeholders::_2);
  cout << bf2(10, 20); ///< same as a.Func(10, 20)  


  //知识点2 :
  //以std::make_shared<thread> 的方式去执行函数
  auto f = std::bind(&A::testfunc, a);
  auto res = std::make_shared<thread>(f);
  
  system("pause");
  return 0;
}

代码5

class B {
public:
  
  void start() {
      auto res = std::bind(&B::testfunc, *this);
      //auto res = std::bind(&B::testfunc, this);   //这里加不加 * 都可以
      //执行testfunc ,方式1 :
      res();

      //执行testfunc ,方式2 :
      //auto res2 = std::make_shared<thread>(res);
  }
  
  void testfunc(void) {
      cout << "wahahah" << endl;
  }
};

int main(){
  B b;
  b.start();
}

相关文章

  • C++ 11的一些新特性(1)

    最近学习了一下C++11的新特性,发现c++的进步很快,已经不在是我最早认识的那个c++了。 我的感觉是c++的更...

  • C++ 11 语言特性介绍

    C++ 11 语言特性 新特性说明nullptr,autoNAfor-each区间迭代shared_ptr uni...

  • c++ 的关键字回顾

    2013年的时候写了一些学c++时候的经验由底层和逻辑深入剖析c++系列,两年前写了一篇c++11新特性详解,去年...

  • C++ 11常用特性

    C++ 11常用特性(转)

  • C++ 11 新特性

    C++11 中值得关注的几大变化(详解) | | 酷 壳 - CoolShell 1. delete 和 defa...

  • C++ 11的一些新特性(2)

    最近学习了一下C++11的新特性,发现c++的进步很快,已经不在是我最早认识的那个c++了。 我的感觉是c++的更...

  • cppinsights 编译安装

    cppinsights 是一款C++源代码到源代码的转换,它可以把C++中的模板、auto以及C++11新特性展开...

  • C++11 标准新特性: 右值引用与转移语义

    C++11 标准新特性: 右值引用与转移语义 VS2013出来了,对于C++来说,最大的改变莫过于对于C++11新...

  • C++ 11 新特性汇总

    内容引自《写给大忙人看的C++》,未包含多线程相关话题,但是基本上C++最重要的方面都包括在内了 概要 改进的对象...

  • C++ 11新特性(部分)

    1. 右值引用 对左值的引用(lValue),放在赋值表达式左边的,有精确的内存地址; 右值(rValue) 指的...

网友评论

    本文标题:C++ 11的一些新特性(1)

    本文链接:https://www.haomeiwen.com/subject/xuvklftx.html