首页
社区
课程
招聘
[分享]《探索现代化C++》泛读笔记摘要1
发表于: 2022-9-7 07:58 3858

[分享]《探索现代化C++》泛读笔记摘要1

2022-9-7 07:58
3858

《探索现代化C++》泛读笔记摘要1

学习C++的原因

作为一个编程者低级语言让你理解程序执行期间发生了什么,反过来让你理解其他程序语言的行为。

 

C++有很多高级特性,它支持很多编程范式:面向对象编程,通用编程,元编程,并行编程以及过程编程等。系列的编程技术,比如说RAII和表达式模板被C++发明出来。

Chapter 1 C++ 基础

第一个C++程序

1
2
3
4
5
6
7
8
#include <iostream>
 
int main() {
    std::cout<<"The answer to the Ultimate Question of Lift,\n"
                        << "the Universe , and Everything is: "
                        << std::endl << 6*7 <<std::endl;
    return 0;
}
  • 输入和输出不是C++语言的核心,它而是由库提供。
  • 标准I/O是流模式的。
  • 命名空间用来帮助我们管理名字和避免命名冲突

变量

1
2
3
4
5
6
int i1 = 2;
float pi = 3.1415926;
double x = -1.5e6// -1500000
double y = -1.5-6   // -0.0000015
char c1 = 'a', c2=35;
bool cmp =i1<pi;  // -> true

内置类型

1
2
3
4
5
6
7
8
9
10
char
short
long
long long
unsigned
signed
float
double
long double
bool

字符和字符串

1
2
char c = 'f';
char name[8] = "Herbert";
1
2
3
4
5
6
#include <string>
int main()
{
    std::string name = "Herbert";
  name = name + ", our cool anti-hero";
}

C++字符串存在优化的情况,少于16字节的字符串不存在动态内存中,而是存在string对象自身中。

声明变量

变量越迟声明越好,这有助于提高代码阅读性,而且有助于编译器优化内存使用。

 

C++ 11 类型推断

1
auto i4 = i3+7;

常量

1
2
3
4
const int ci1 = 2;
const float pi = 3.1415926;
const char cc= 'a';
const char bool cmp = ci1<pi;

如果不是外部常量则在声明时必须初始化。

Literals

1
2
3
4
5
6
7
2
2u
2l
2ul
2.0
2.0f
2.01

C++ 14 二进制 Literals

1
int b1 = 0b11111010; // int b1 = 250

C++ 17 提高大数阅读性的一些分割符撇号

1
2
3
4
long long  d = 6'546'687'616'861'1291;
unsigned long long u1x = 0x139'ae3b'2ab0'94f3;
int b = 0b101'1001'0011'1010'1101'1010'0001;
const long double = 3.141'592'653'589'793'238'4621;

C++ 17 还支持16进制的浮点Literals

1
2
float f1 = 0x10.1p0f;
double d2 = 0x1ffp10;

C与C++字符串使用上的改变。

1
2
3
4
char s1[] = "Old C style";
std::string s2 = "In C++ better like this";
std::string s3 = "This is a very long and clusmsy text "
                                    "that is too long for one line.";

C++14 可以通过添加后缀s创建字符串字面值。

1
2
f("I'm not a string"); // 第一个是由const char[] 的literal初始化的字符串
f("I'm a really a string"s); // 第二个是一个string literal初始化的字符串

C++11 Non-narrowing 初始化,避免数据丢失。大括号里的值不能发生缩小转换。

1
2
3
4
5
6
7
8
9
10
11
12
13
long l2 = 1234567890123;
long l = { 1234567890123 }; // 这里编译器编译时会报错,因为这里的初始化发生了收缩转换。
int i1 = 3.14;
int i1n = {3.14}; //ERROR
unsigned u2 = -3;
unsigned u2n = {-3}; // ERROR
float f1 = {3.14}; // okay
double d;
float f2= {d}; // ERROR
unsigned u3 = {3};
int i2 = {2};
unsigned u4={i2}; // ERROR
int i3 = {u3}; // ERROR

作用域

作用域决定了(非静态)变量和常量的寿命和可见性。

 

建议不要使用全局变量。


[培训]《安卓高级研修班(网课)》月薪三万计划,掌握调试、分析还原ollvm、vmp的方法,定制art虚拟机自动化脱壳的方法

收藏
免费 0
支持
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回
//