贡献者: addis
Matlab 和 Python 等动态语言虽然用起来方便,但缺点是运行较慢,对于一些计算量大的项目不适合.目前在高性能计算中广泛使用的只有两种语言即 C++ 和 Fortran.虽然 Fortran 普遍被认为是一个过时的语言,但在计算物理中,许多人仍然在使用,一是因为以前遗留下的 Fortran 代码比较多,二是一些年纪较大的学者只会 Fortran.
一本在数值算法中很有名的书是 Numerical Recipes,这本书第三版以前都使用 Fortran 或 C,而第三版却只有 C++,这也是本书选择介绍 C++ 而不是 Fortran 的原因之一.本书将从 Numerical Recipes 中借鉴许多代码上的风格和算法.
C++ 的特征实在多不胜数,事实上无论是什么语言,做计算物理的研究者大多会倾向于只选择一些最基础的语法来使用. 我们在这里列出本书使用的 C++ 特性.
bool, char, int, long, long long, float, double,long double)char, short, int 几乎总是 8, 16, 32 bit 的,虽然标准没这么规定.
=, +, -, *, /, %, ++, --, +=, -=, *=, /=, ?:) 以及优先级
% 的定义使 (m / n) * n + m % n == m 成立.
&, |, ^, ~, <<,>>)
printf)
if, else if, else)
for(;;), while,do while,break,continue)
goto,label: 谨慎使用,可用于跳出多重循环.
inline 函数,static 变量)
const
typedef
new, delete.
throw, try, catch
cmath
complex
vector(包括内存管理机制、改变长度可能使指针失效)
string, string32
iostream(cin,cout,<< 算符,>> 算符,cin 可以放在判断语句中,如 if (cin >> a).)
ifstream,ofstream
cin 或 fin 读到了文件末尾(文件末尾允许任意多空格/空行)?不要用 cin.eof 判断,例如读 int,可以先初始化为 INT_MAX,然后读完以后判断是否仍为 INT_MAX.
cin.ignore(numeric_limits<int>::max(), '\n');
#include <climits> 中的常数(INT_MIN,INT_MAX,DBL_MAX,DBL_EPSILON),以及 std::numeric_limits<>::max() 等.
__cplusplus 用于检测 cpp 版本.
<bits/stdc++.h> 快速 include 所有 C 头文件和 STL 头文件.
pair
unordered_set,set
unordered_map,ordered_map
stack
queue,deque,priority_que
for(auto &e : v),需要定义 v.begin(),v.end(),iterator 需要支持 ++,!=,*p.
public, private,数据成员,函数成员,operator+, -, *, /, (), [])
类名() = default;(可以有其他 constructor),没有 destructor,没有 virtual function.可以用 std::is_pod<类名>::value 来判断.
#include, #define, #if, #ifdef, #ifndef, #else, #endif)
var(变量),#var(字符串),##var(代码)的区别
#define ... do {} while(0) 避免多个语句不加花括号带来的错误.
namespace
constexpr