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. [LeetCode&Python] Problem 860. Lemonade Change

    At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you, and ...

  2. dfs——皇后问题(回溯)

    #include <iostream> using namespace std; ],b[],c[],d[]; ; dfs(int i) { if(i>n) { sum++; ) { ...

  3. IE8的兼容问题

    1: rgba失效的问题: 在添加rgba的类名内加上:filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#7f00000 ...

  4. Linux如何在(localhost)本地打开html

    自己实验的结果,可能不是最好的办法,方法不唯一 1. 下载Tomcat https://www.cnblogs.com/liangweiping/p/5113857.html 如何查看本机的端口服务 ...

  5. 20155208徐子涵 2016-2017-2 《Java程序设计》第2周学习总结

    20155208徐子涵 2016-2017-2 <Java程序设计>第2周学习总结 教材学习内容总结 第三章 基础语法 3.1 类型.变量与运算符 • 关键字:在定义java文件名的时候要 ...

  6. HURST 1116:选美大赛(LIS+路径输出)

    选美大赛 Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 1099(318 users) Total Accepted: 349(252 ...

  7. 2017.8.23 shell判断文件,目录是否存在或者具有权限

    #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" #这里的-x 参数 ...

  8. HTML表格元素

      学习要点:     1.表格元素总汇     2.构建表格解析 一.表格元素总汇     表格的基本构成最少需要三个元素:<table>.<tr>.<td>,其 ...

  9. Spring的依赖注入的2种方式(1天时间)

    今天花了一天的时间才调试出来 private      接口   实现类的那个bean; 最后面的那个名字不能随便的写,必须是配置文件中,实现类的那个bean 就是后面的那个名字写错了,花了整整一天 ...

  10. 解决winfrom下TextBox不支持透明背景色

    不知道微软扯什么拉鸡蛋子,居然有控件不支持透明,我实在想喷设计的人脑残.尤其可恨的是TextBox不支持,更可恨的是直到最新版.net4.6也不支持.源码又看不见,具体实现细节都不知道,谁能改得动?这 ...