参考书籍: 《Professional C++》、《C++ in Action》、《C++ 代码设计与重用》 《C++ 标准库》、《深度探索 C++ 对象模型》 《Modern C++ Programming with Test-Driven Development》、《Effective Modern C++》
程序之母
面向过程
1
2
3
4
5
6
#include <iostream>
int main()
{
std::cout << "Hello World!" << std::endl;
return0;
}
面向对象
1
2
3
4
5
6
7
8
9
#include <iostream>
class HelloWorld
{
public:
HelloWorld() { std::cout << "Hello "; }
~HelloWorld() { std:cout << "World!\n"; }
}
HelloWorld helloWorld;
int main() { }
基础
预处理指令
include
define
ifdef [key] … #endif 或 #ifndef [key] … #endif
pragma [xyz] : e.g. -> 避免文件被多次包含(#paragma once,文件第一行)
main() 函数
基本 I/O 流:std::cout、std:cerr
命名空间
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// namespaces.h
namespace mycode {
void foo();
}
// namespaces.cpp
#include <iostream>
#include "namespaces.h"
usingnamespacestd;
namespace mycode {
void foo() {
cout << "foo() called in the mycode namespace" << endl;
}
}
// test.cpp
#include "namespaces.h"
usingnamespace mycode;
int main()
{
foo();
return0;
}
变量
未初始化经常是 bug 之源;
int i3 = static_cast(myFloat);
字面量
C++14 -> 0b1111011
C++14 -> int number1 = 23’456’78; float number2 = 0.123’456’7f;
myTicket.setPassengerName("Sherman T. Socketwrench");
auto myTicket2 = make_unique<AirlineTicket>(); // Heap-based
myTicket2->setPassengerName("Laudimore M. Hallidue");
AirlineTicket* myTicket3 = new AirlineTicket(); // Heap-based
delete myTicket3; // delete the heap object!
使用 String 类
C 式字符串 -> char* result = new char[strlen(str) + 1];
1
2
3
4
5
6
7
8
9
10
11
12
13
char text1[] = "abcdef";
size_t s1 = sizeof(text1); // is 7
size_t s2 = strlen(text1); // is 6
constchar* text2 = "abcdef";
size_t s3 = sizeof(text2); // is platform-dependent
size_t s4 = strlen(text2); // is 6
// C 式字符串字面量
char* ptr = "hello"; // Assign the string literal to a variable.
ptr[1] = 'a'; // Undefined behavior!
constchar* ptr = "hello";
ptr[1] = 'a'; // 错误!试图在只读内存中进行写操作
char arr[] = "hello"; // 编译器会自动创建相应大小的字符数组
arr[1] = 'a'; // 字符数组是可读写的
使用
1
2
3
4
5
6
7
8
string A("12");
string B("34");
string C;
C = A + B; // C = "1234"
A += B; // A = "1234"
// C++14
auto string1 = "Hello World"; // string1 will be a const char*
auto string2 = "Hello World"s; // string2 will be an std::string
数字转换 string to_string(int val); string to_string(unsigned val); string to_string(long val); string to_string(unsigned long val); string to_string(long long val); string to_string(unsigned long long val); string to_string(float val); string to_string(double val);
string to_string(long double val);
int stoi(const string& str, size_t idx=0, int base=10); long stol(const string& str, size_t idx=0, int base=10); unsigned long stoul(const string& str, size_t idx=0, int base=10); long long stoll(const string& str, size_t idx=0, int base=10); unsigned long long stoull(const string& str, size_t idx=0, int base=10); float stof(const string& str, size_t idx=0); double stod(const string& str, size_t idx=0); long double stold(const string& str, size_t idx=0);
1
2
conststring s = "1234";
int i = stoi(s); // i = 1234
Raw String Literals
1
2
3
4
string str = R"(Hello "World"!)";
string str = R"(Line 1
Line 2 with \t)";
string str = R"-(The characters )" are embedded in this string)-";"
注释
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
* saveRecord()
*
* Saves the given record to the database.
*
* Parameters:
* Record& rec: the record to save to the database.
* Returns: int
* An integer representing the ID of the saved record.
* Throws:
* DatabaseNotOpenedException if the openDatabase() method was not