zoukankan      html  css  js  c++  java
  • C++11的一些新特性

    3.1.9崭新的Template特性

    Variadic Template 可变参数模板

    void print()
    {
    }
    
    template <typename T, typename… Types>
    void print (const T& firstArg, const Types&… args)
    {
        std::cout<<firstArg<<std::endl;
        print(args);
    }

    如上,如果传人1或多个实参,上述的function template就会被调用,区分第一个实参,允许第一实参被打印,然后递归调用print()并传入其余实参。必须提供一个nontemplate重载函数print(),才能结束整个递归动作。

    Alias Template 带别名的模板

    template <typename T>

    using Vec = std::vector<T,MyAlloc<T>>;

    后,可以这样声明:

    Vec<int> coll;

    reference wrapper

    官方的解释是这样的:

    Reference wrapper

    Class that emulates a reference to an element of type T, but which can be copied (it is both copy-constructible and copy-assignable).

    实例:

    std::vector<std::reference_wrapper<Item>> books;  // elements are references
    
    Item f("Faust",12.99);
    books.push_back(f);    // insert book by reference

      for (const auto& book : books) {
        std::cout << book.get().getName() << ": "
        << book.get().getPrice() << std::endl;
      }

    注意book.get().getName(),循环体中book被声明为处理“类型为Item&”的元素,用它直接调用成员函数前要调用get()。

    C++11开始支持数组的begin和end,具体用法为:

    int ia[]={0,1,2,3,4,5,6,7};
    int *b=begin(ia);
    int *e=end(ia);
  • 相关阅读:
    CreateRemoteThread注入DLL
    远程线程注入引出的问题
    jQuery中排除指定元素,同时选择剩下的所有元素
    YUIDoc的使用方法小结
    实验二 栈和队列的应用
    实验一 线性表的基本操作
    最大子段和详解
    HDOJ 1995 汉诺塔V
    错排公式 详细解答
    HDOJ 2212 DFS
  • 原文地址:https://www.cnblogs.com/ph829/p/5145613.html
Copyright © 2011-2022 走看看