C++11--20分钟了解C++11 (下)

20分钟了解C++11

9 override关键字 (虚函数使用)

 *
 * 避免在派生类中意外地生成新函数
 */
// C++ 03
class Dog {
   virtual void A(int);
   virtual void B() const;
}

class Yellowdog : public Dog {
   virtual void A(float);  // 生成一个新函数
   virtual void B(); //生成一个新函数
}

// C++ 11
class Dog {
   virtual void A(int);
   virtual void B() const;
   void C();
}

class Yellowdog : public Dog {
   virtual void A(float) override;  // Error: no function to override
   virtual void B() override;       // Error: no function to override
   void C() override;               // Error: not a virtual function
}

10 final (虚函数和类使用)


class Dog final {    // 在类名后添加表示不能再从该类派生子类
   ...
};

class Dog {
   virtual void bark() final;  // 在虚函数后添加表示该函数不能再被覆写
};

11 编译器生成默认构造函数

class Dog {
   Dog(int age) {}
};

Dog d1;  // 错误:编译器不会自动生成默认构造函数,因为已经有构造函数了

// C++ 11:
class Dog {
   Dog(int age);
   Dog() = default;    // 强制编译器生成默认构造函数
};

12 delete 禁用某些函数


class Dog {
   Dog(int age) {}
}

Dog a(2);
Dog b(3.0); // 3.0会从double类型转成int类型
a = b;     // 编译器会生成赋值运算符

// C++ 11 手动禁用:
class Dog {
   Dog(int age) {}
   Dog(double ) = delete;
   Dog& operator=(const Dog&) = delete;
}

13 constexpr 常量表达式

int arr[6];    //OK
int A() { return 3; }
int arr[A()+3];   // 编译错误

// C++ 11
constexpr int A() { return 3; }  // 强制计算在编译时发生

int arr[A()+3];   // 生成一个大小为6的数组

// 利用constexpr写更快的程序
constexr int cubed(int x) { return x * x * x; }

int y = cubed(1789);  // 在编译时计算

//cubed()函数:
//1. 非常快,不消耗运行时计算
//2. 非常小,不占据二进制空间

14 新的字符串字面值

  // C++ 03:
  char*     a = "string";  

  // C++ 11:
  char*     a = u8"string";  // UTF-8 string.
  char16_t* b = u"string";   // UTF-16 string.
  char32_t* c = U"string";   // UTF-32 string.
  char*     d = R"string \\"    // raw string.

15 lambda函数

cout << [](int x, int y){return x+y}(3,4) << endl;  // Output: 7
auto f = [](int x, int y) { return x+y; };
cout << f(3,4) << endl;   // Output: 7

template<typename func>
void filter(func f, vector<int> arr) {
   for (auto i: arr) {
      if (f(i))
         cout << i << " ";
   }
}

int main() {
   vector<int> v = {1, 2, 3, 4, 5, 6 };

   filter([](int x) {return (x>3);},  v);    // Output: 4 5 6
   ...
   filter([](int x) {return (x>2 && x<5);},  v); // Output: 3 4

   int y = 4;
   filter([&](int x) {return (x>y);},  v);    // Output: 5 6
   //注: [&] 告诉编译器要变量捕获
}

// Lambda函数几乎像是一个语言的扩展,非常方便
// template
// for_nth_item

17 用户自定义的字面值


// C ++在很大程度上使用户定义的类型(类)与内置类型的行为相同.
// 用户自定义的字面值使其又往前迈出一步

//老的C++,不知道到底表示多长?
long double height = 3.4;

// 在学校物理课的时候
height = 3.4cm;
ratio = 3.4cm / 2.1mm; 

// 为什么在编程时不这么做?
// 1. 语言不支持
// 2. 单位转换需要运行时开销

// C++ 11:
long double operator"" _cm(long double x) { return x * 10; }
long double operator"" _m(long double x) { return x * 1000; }
long double operator"" _mm(long double x) { return x; }

int main() {
   long double height = 3.4_cm;
   cout << height  << endl;              // 34
   cout << (height + 13.0_m)  << endl;   // 13034
   cout << (130.0_mm / 13.0_m)  << endl; // 0.01
}

//注: 加constexpr使单位转换在编译时计算

// 限制: 只能用于以下参数类型:
   char const*
   unsigned long long
   long double
   char const*, std::size_t
   wchar_t const*, std::size_t
   char16_t const*, std::size_t
   char32_t const*, std::size_t
// 返回值可以是任何类型

// 例子:
int operator"" _hex(char const* str, size_t l) {
   // 将十六进制格式化字符串str转成int ret
   return ret;
}

int operator"" _oct(char const* str, size_t l) {
   // 将格式化8进制字符串str转成 int ret
   return ret;
}

int main() {
   cout << "FF"_hex << endl;  // 255
   cout << "40"_oct << endl;  // 32
}

18 可变参数模板 Variadic Template

 *
 * 可以接受任意个数的任意类型模板参数的模板
 * 类和函数模板都可以是可变的
 */
template<typename... arg>
class BoTemplate;

BoTemplate<float> t1;
BoTemplate<int, long, double, float> t2;
BoTemplate<int, std::vector<double>> t3;

BoTemplate<> t4;

// 可变参数和非可变参数组合
template<typename T, typename... arg>
class BoTemplate;

BoTemplate<> t4;  // Error
BoTemplate<int, long, double, float> t2;  // OK

// 定义一个默认模板参数
template<typename T = int, typename... arg>
class BoTemplate;

19 模板别名 Template Alias

  template<class T> class Dog { /* ... */ };
  template<class T>
    using DogVec = std::vector<T, Dog<T>>;

  DogVec<int> v;  // 同: std::vector<int, Dog<int>>

20 decltype

 * 相当于GNU typeof
 */
  const int& foo();      // 声明一个函数foo()
  decltype(foo())  x1;  //  类型是const int&

  struct S { double x; };
  decltype(S::x)   x2;  //  x2类型double

  auto s = make_shared<S>();
  decltype(s->x)   x3;  //  x3类型double

  int i;
  decltype(i)      x4;  //  x4类型int  

  float f;
  decltype(i + f)  x5;   // x5类型float

  // decltype对于模板泛型编程非常有用
  template<type X, type Y>
  void foo(X x, Y y) {
     ...
     decltype(x+y) z;
     ...
  }

  // 如果返回值要使用decltype?
  template<type X, type Y>
  decltype(x+y) goo(X x, Y y) {      // 错误:x 和 y 未定义
     return  x + y;
  }

  // auto和decltype组合实现具有尾随返回类型的模板
  template<type X, type Y>
  auto goo(X x, Y y) -> decltype(x+y) {
     return  x + y;
  }

原文地址:https://www.cnblogs.com/logchen/p/10193470.html

时间: 2024-08-07 15:17:51

C++11--20分钟了解C++11 (下)的相关文章

11.18 Apache用户认证11.19 11.20 域名跳转11.21 Apache访问日志

11.18 Apache用户认证更改虚拟主机内容vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf增加用户名与密码? /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd aming-c是创建 -m指定类型查看生成的密码文档内容上面已经他去了.htpasswd目录,再创建用记就不需要-c在wi上指定域名hostsC:\Windows\System32\drivers\etc认证:没有

11.18 Apache用户认证 11.19/11.20 域名跳转 11.21 Apache访问日志

[[email protected] abc.com]# /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd amingNew password: Re-type new password: Adding password for user aming [[email protected] abc.com]# cat /data/.htpasswd aming:$apr1$zwiDnzEZ$JSD12PoIVH90Sry//fz3T.

20.10 for循环;20.11 while循环(上);20.12 while循环(下);20.13 break跳出循环;20.14 ;20.15

20.10 for循环 案例1 1. 编写for循环脚本:计算1到100所有数字和: [[email protected] ~]# vi for1.sh 添加内容: #!/bin/bash sum=0 for i in `seq 1 100` do echo "$sum + $i" sum=$[$sum+$i] echo $sum done echo $sum 2. 执行for1.sh脚本: [[email protected] ~]# sh for1.sh 案例2 1. 文件列表循环

【C++11】30分钟了解C++11新特性

作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 什么是C++11 C++11是曾经被叫做C++0x,是对目前C++语言的扩展和修正,C++11不仅包含核心语言的新机能,而且扩展了C++的标准程序库(STL),并入了大部分的C++ Technical Report 1(TR1)程序库(数学的特殊函数除外). C++11包括大量的新特性:包括lambda表达式,类型推导关键字auto.decl

Cocos2d-x 3.1.1 学习日志6--30分钟了解C++11新特性

新的关键字 auto C++11中引入auto第一种作用是为了自动类型推导 auto的自动类型推导,用于从初始化表达式中推断出变量的数据类型.通过auto的自动类型推导,可以大大简化我们的编程工作.auto实际上实在编译时对变量进行了类型推导,所以不会对程序的运行效率造成不良影响.另外,似乎auto并不会影响编译速度,因为编译时本来也要右侧推导然后判断与左侧是否匹配.如果没有auto关键字 写个迭代器要写很长长,这也算是节省了我们的脑细胞吧,~~~~(>_<)~~~~ !! auto a; /

[转]30分钟了解C++11新特性

新的关键字 autoC++11中引入auto第一种作用是为了自动类型推导auto的自动类型推导,用于从初始化表达式中推断出变量的数据类型.通过auto的自动类型推导,可以大大简化我们的编程工作.auto实际上实在编译时对变量进行了类型推导,所以不会对程序的运行效率造成不良影响.另外,似乎auto并不会影响编译速度,因为编译时本来也要右侧推导然后判断与左侧是否匹配.如果没有auto关键字 写个迭代器要写很长长,这也算是节省了我们的脑细胞吧,~~~~(>_<)~~~~ !! [html] view

20.10 for循环 20.11/20.12 while循环 20.13 break跳出循环 20.14 continue结束本次循环 20.15 exit退出整个脚本

20.10 for循环 ?语法:for 变量名 in 条件; do -; done ? 案例1 1+2+3..+100的和 #!/bin/bash sum=0 for i in `seq 1 100` // seq 1到100个数字 do sum=$[$sum+$i] echo $i done echo $sum sum 第一次作为变量的时候,是0:当进入for循环里面的时候,每运算一次,sum变量就会改变一次,直至$i 结束:最后输出结果 $sum ? 案例2 文件列表循环 #!/bin/ba

现代软件工程_团队项目_阿尔法阶段_第四次会议记录_2017.11.20

第四次会议记录 会议时间:2017.11.20  12:00-12:15 会议地点:中科大西区第三教学楼一楼讨论区 参会人员:刘荪傲 姜博文 夏铭阳 徐宇飞 张淦霖 [内容一]:前端介绍页面原型v1.0.3 增加: 一.页面简单逻辑判断(没有连接数据库,单纯只是格式判断) 1.登录界面设置唯一的账号和密码,账号不对会提示账号不存在,账号正确密码不对提示密码错误,都正确点击登录连接到首页 2.注册界面设置账号密码昵称为6-16位字符,确认密码需要与密码相同,手机号为11位数字,邮箱后缀名为@mai

30分钟学会iOS 11开发环境xcode 9图文教程

关注微信公众号[异步图书]每周送书 Xcode是一款功能全面的应用程序,通过此工具可以轻松输入.编译.调试并执行Objective-C程序.如果想在Mac上快速开发iOS应用程序,则必须学会使用这个强大的工具的方法.在本文容中,将详细讲解Xcode 9开发工具的基本知识,为读者步入本书后面知识的学习打下基础. 1.1 基本面板介绍 使用Xcode 9打开一个iOS 11项目后的效果如图1-1所示. 图1-1 打开一个iOS 11项目后的效果(1)调试区域:左上角的这部分功能是控制程序编译调试或者