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. 带列表写入文件出错先 json.dumps

    output = json.dumps(output, ensure_ascii=False).encode('utf-8')

  2. hdu-2196 树形dp 求一个树中所有节点能到达的最远距离f[i] (其实也不难嘛!)

    #include <bits/stdc++.h> using namespace std; ; struct T { int to; int w; }; vector < vecto ...

  3. CentOS右键菜单添加新增文件命令

    进入模板文件夹, 增加空白文件即可. cd /home/tom/Templatesvi text01.txt //新增一个名为text01的文本文件. :wq //保存退出. 另外可以用LibreOf ...

  4. 针对 Intellij IDEA 2018.2 版本 异常退出问题

    原文:https://blog.csdn.net/weixin_41370091/article/details/81322694 近日刚安装好Intellij IDEA(2018.2 Ultimat ...

  5. crontab 例子

    一个简单的 crontab 示例 0,20,40 22-23 * 7 fri-sat /home/ian/mycrontest.sh 在这个示例中,我们的命令在 7 月的每个星期五和星期六晚上 10 ...

  6. CF643D Bearish Fanpages

    题意 英文版题面 Problems Submit Status Standings Custom test .input-output-copier { font-size: 1.2rem; floa ...

  7. 尚硅谷【SpringBoot】入门

    https://www.bilibili.com/video/av20965295/?p=2 缺点: 基于springframe的封装    对framework api需要熟悉 2微服务 2014 ...

  8. git pull refusing to merge unrelated histories

    1. 简介 最近的项目开发环境我换到实验室的机器上了,毕竟是台式机,速度杠杠的.于是出现了一个问题,台式机上面的代码我笔记上的代码同步的问题.于是想到了Git这个强大的工具.关于git的简介我就不说了 ...

  9. css属性的继承性

    不可继承属性: 盒子模型属性:width height padding border margin 背景相关属性:background相关(background-image background-si ...

  10. pstStream->pstPack[i].pu8Addr详解

    /****************************************************************************** * funciton : save H2 ...