20分钟了解C++ 11

1 初始化列表 Initializer List


//C++ 03中用初始化列表初始化数组
int arr[4] = {3, 2, 4, 5}; vector<int> v;
v.push_back(3);
v.push_back(2);
v.push_back(4);
v.push_back(5); // C++ 11 做了扩展,可以初始化vector
vector<int> v = {3, 4, 1, 9}; // 调用初始化列表构造函数
// 所以相关的STL容器都更新了,支持初始化列表 // 定义自己的初始化列表构造函数:
#include <initializer_list>
class BoVector {
vector<int> m_vec;
public:
BoVector(const initializer_list<int>& v) {
for (initializer_list<int>::iterator itr = v.begin(); itr!=v.end(); ++ itr)
m_vec.push_back(*itr);
}
}; BoVector v = {0, 2, 3, 4};
BoVector v{0, 2, 3, 4}; // 初始化的另一种方式 // 自动进行常规的初始化
class Rectangle {
public:
Rectangle(int height, int width, int length){ }
}; void draw_rect(Rectangle r); int main() {
draw_rect({5, 6, 9}); // Rectangle{5,6,9}被自动调用
} // 注意:要小心使用
// 1. 可读性非常差,即使有IDE的帮助也是。函数名称很少表示函数所采用的参数类型
// 2. 函数可以被不同参数类型重载,比如下面的情况 void draw_rect(Triangle t);

2 统一初始化Uniform Initialization

// C++ 03
class Dog { // 聚合类或者结构体,直接初始化对应成员
public:
int age;
string name;
};
Dog d1 = {5, "Henry"}; // 可以直接用大括号聚合初始化 // C++ 11 扩展了大括号初始化的范围
class Dog {
public:
Dog(int age, string name) {...};
};
Dog d1 = {5, "Henry"}; //类有合适参数的构造函数 /* 统一初始化的搜索顺序:
* 1. 初始化列表构造函数
* 2. 具有合适参数的常规构造函数
* 3. 聚合初始化(直接初始化对应成员)
*/ Dog d1{3}; class Dog {
public:
int age; // 第3选择 Dog(int a) { // 第2选择
age = a;
} Dog(const initializer_list<int>& vec) { // 第1选择
age = *(vec.begin());
}
};

3 auto类型

std::vector<int> vec = {2, 3, 4, 5};

// C++ 03
for (std::vector<int>::iterator it = vec.begin(); it!=vec.end(); ++ it)
m_vec.push_back(*it); // C++ 11: 使用auto节省大量打字的时间
for (auto it = vec.begin(); it!=vec.end(); ++ it)
m_vec.push_back(*it); auto a = 6; // a is a integer
auto b = 9.6; // b is a double
auto c = a; // c is an integer
auto const x = a; // int const x = a
auto& y = a; // int& y = a // 它是静态类型,没有运行时成本,fat-free.
// 使得代码更易维护 // 1. 当需要类型转换时不要使用auto
// 2. IDE变得很重要

4. foreach

 */
// C++ 03:
for (vector<int>::iterator itr = v.begin(); itr!=v.end(); ++ itr)
cout << (*itr); // C++ 11:
for (auto i: v) { // 任何有begin()和end()的类都能使用
cout << i ; // 只读访问
} for (auto& i: v) {
i++; // 改变v的值,避免拷贝构造
} auto x = begin(v); // 同int x = v.begin(); int arr[4] = {3, 2, 4, 5};
auto y = begin(arr); // y == 3
auto z = end(arr); // z == 5
// 因为数组定义了begin()和end()
// 可以通过给数据的容器定义begin()和end(),使你的代码适配第三方库

5 nullptr

 * 代替C++ 03中的NULL
*/
// NULL具有二义性
void foo(int i) { cout << "foo_int" << endl; }
void foo(char* pc) { cout << "foo_char*" << endl; } int main() {
foo(NULL); // 二义性,调用哪个函数? // C++ 11
foo(nullptr); // 调用foo(char*)
}

6 枚举类 enum class


// C++ 03
enum apple {green_a, red_a};
enum orange {big_o, small_o};
apple a = green_a;
orange o = big_o; if (a == o) //两个不同枚举类型进行比较
cout << "green apple and big orange are the same\n";
else
cout << "green apple and big orange are not the same\n"; // C++ 11
enum class apple {green, red};
enum class orange {big, small};
apple a = apple::green;
orange o = orange::big; if (a == o) //编译失败,因为我们没有定义 ==(apple, orange)
cout << "green apple and big orange are the same\n";
else
cout << "green apple and big orange are not the same\n";

7 static_assert


// 运行时断言
assert( myPointer != NULL ); // C++ 11提供了静态断言,在编译时检查
// 如果int的大小不是4,编译不过
static_assert( sizeof(int) == 4 );

8 委托构造函数


// 一个构造函数想复用另一个构造函数的代码
// 下面这种方法是错误的,会构造两个对象
class Dog {
public:
Dog() { ... }
Dog(int a) { Dog(); doOtherThings(a); }
}; // C++ 03的实现方法:
class Dog {
init() { ... };
public:
Dog() { init(); }
Dog(int a) { init(); doOtherThings(); }
};
/* 缺点:
* 1. 代码笨重.
* 2. 而且init()可能被其他函数调用
*/ // C++ 11 方法:
class Dog {
int age = 9;
public:
Dog() { ... }
Dog(int a) : Dog() { doOtherThings(); }
};
// 限制: Dog()也即被委托的构造函数必须先被调用

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

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

    20分钟了解C++11 9 override关键字 (虚函数使用) * * 避免在派生类中意外地生成新函数 */ // C++ 03 class Dog { virtual void A(int); ...

  2. Java基础部分(11~20)

    11."=="和 equals 方法究竟有什么区别? (单独把一个东西说清楚,然后再说清楚另一个,这样,它们的区别自然就出来了,混在一起说,则很难说清楚) ==操作符专门用来比较两 ...

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

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

  4. 【转载】20分钟MySQL基础入门

    原文:20分钟MySQL基础入门 这里持续更新修正 开始使用 MySQL 为关系型数据库(Relational Database Management System),一个关系型数据库由一个或数个表格 ...

  5. 20分钟成功编写bootstrap响应式页面 就这么简单

    最近发现一个叫 Bootstrap 的好东西,Bootstrap 是现在最流行的响应式 CSS 框架,它以移动设备优先,能够快速适应不同设备.使用它编写响应式页面快捷.方便,而且屏蔽了浏览器差异.使用 ...

  6. 《量化投资:以MATLAB为工具》连载(1)基础篇-N分钟学会MATLAB(上)

    http://blog.sina.com.cn/s/blog_4cf8aad30102uylf.html <量化投资:以MATLAB为工具>连载(1)基础篇-N分钟学会MATLAB(上) ...

  7. [转]20位活跃在Github上的国内技术大牛

    FROM : http://blog.csdn.net/yaoxtao/article/details/38518933 20位活跃在Github上的国内技术大牛 本文列举了20位在Github上非常 ...

  8. 无语的index hint:手工分配哈希区,5小时不出结果,优化后20分钟

    同事说,有个语句5个小时不出结果,叫我帮忙看看,于是叫同事发过来.不看不知道,一看吓一跳,3个表关联,强制使用了2个index hint,当中一个表9g,一个表67g,另一个小表40Mb.开发者,总以 ...

  9. 关于.net服务启动注册到zookeeper,但是注册节点20分钟自动消失解决办法

        ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,作用简单描述就是相当于一个中介,服务提供者将服务注册到zk,服务调用者直接从zk获取,zk的作用就是协调     最近碰到公 ...

随机推荐

  1. SEO:网站改版

    网站改版分为2种:前端页面改版(不使用301 ),链接结构发生变化(必须使用301) 1.确定一定以及肯定使用301永久重定向,不要使用302跳转 2.非常十分以及极其要求使用百度站长平台的“网站改版 ...

  2. xdoj-1319 求树上任意一点的最大距离----利用树的直径

    1 #include <bits/stdc++.h> using namespace std; ; vector < vector <int> > g(N); in ...

  3. Dependency Parsing -13 chapter(Speech and Language Processing)

    https://web.stanford.edu/~jurafsky/slp3/13.pdf constituent-based 基于成分的phrasal constituents and phras ...

  4. HDU 1875:畅通工程再续(最小生成树)

    畅通工程再续 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  5. 2018.4.2 sqlite优化

    一.参数优化. ```PRAGMA foreign_keys=ON;PRAGMA cache_size=8000;PRAGMA synchronous=OFF;PRAGMA temp_store=ME ...

  6. 简单的Windows应用程序命名规则

    读书:<高质量C++编程指南> 作者对“匈牙利”命名规则做了合理的简化,下述的命名规则简单易用,比较适合于Windows应用软件的开发. l [规则3-2-1]类名和函数名用大写字母开头的 ...

  7. 【BZOJ4566】【HAOI2016】找相同字符

    后缀自动姬好,好写好调好ac 原题: 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两 个子串中有一个位置不同. 1 <=n1, n2< ...

  8. linux网络编程概念(一)

    AF表示地址族(address family) PF表示协议族(protocol family) domain参数 AF_UNIX 内核中通信 sockaddr_un AF_INET 通过ipv4 s ...

  9. YUM安装调试以及命令具体解释

    背景,须要安装cacti,google了非常多安装资料.须要先yum安装一些准备lib包,比方snmp以及openssl等等. [root@mysqlvm2 ~]# yum install net-s ...

  10. ML(附录3)——过拟合与欠拟合

    过拟合与欠拟合 我们希望机器学习得到好的模型,该模型能够从训练样本中找到一个能够适应潜在样本的普遍规律.然而,如果机器学习学的“太好”了,以至把样本的自身特点当作潜在样本的一般特性,这就使得模型的泛化 ...