1 exe 程序
1.1 最小 cpp
int main(){} // minimal cpp
思考一: 为什么最小程序没有 return 语句?
1.2 Hello
#include <iostream> int main() { std::cout << "Hello! "; }
思考二: 和 std::endl 的区别是什么?
1.3 函数
#include <iostream>
double Square(double x) { return x*x; } void PrintSquare(double x) { std::cout << "The square of " << x << " is "<< Square(x) << ' '; } int main() { PrintSquare(1.21); }
思考三: Square 有必要写成模板函数么?
2 类型、变量和运算
2.1 定义
声明 (declaration): a statement that introduces a name into the program. It specifies a type for the named entity
类型 (type): (为对象) 定义了一系列可能 值(value) 和一系列 操作(operations)
值 (value): a set of bits interpreted according to a type.
2.2 对象和变量
对象 (object): some memory that holds a value of some type
变量 (variable): a named object
2.3 初始化
等号 "=" 属于 C 语言, C++ 中尽量用 {}-list
double d1 = 2.3; double d2 {2.3}; complex<double> z = 1; // a complex number with double-precision floating-point scalars complex<double> z2 {d1,d2}; complex<double> z3 = {1,2}; // the = is optional with {...}
vector<int> v {1,2,3,4,5,6}; // a vector of ints
c++11 的 auto
auto b = true; // a bool auto ch = 'x'; // a char auto i = 123; // an int auto d = 1.2; // a double auto z = sqrt(y); // z has the type of whatever sqrt(y) returns
尽量使用 auto,除非如下:
1) in a large scope where we want to make the type clearly visible to readers of our code.
2) be explicit about a variable’s range or precision (e.g., double rather than float ).
3 常量
3.1 const 和 constexpr
const,意思是 “承诺不改变其值”,常用来指定 “接口” (interfaces), 这样 数据 可以传递给 函数 而本身不被修改,编译器通过 const 关键字来保证这种承诺的执行。
const int dmv = 17; // dmv is a named constant int var = 17; // var is not a constant
constexpr double max1 = 1.4∗Square(dmv); // OK if square(17) is a constant expression constexpr double max2 = 1.4∗Square(var); // error : var is not a constant expression const double max3 = 1.4∗Square(var); // OK, may be evaluated at run time
constexpr :meaning roughly ‘‘to be evaluated at compile time’’. This is used primarily to specify constants, to allow placement of data in memory where it is unlikely to be corrupted, and for performance.
double sum(const vector<double>&); // sum will not modify its argument vector<double> v {1.2, 3.4, 4.5}; // v is not a constant const double s1 = sum(v); // OK: evaluated at run time constexpr double s2 = sum(v); // error : sum(v) not constant expression
3.2 constexpr function
For a function to be usable in a constant expression, that is, in an expression that will be evaluated by the compiler, it must be defined constexpr
constexpr double Square(double x) { return x∗x; }
To be constexpr , a function must be rather simple: just a return -statement computing a value.
A constexpr function can be used for non-constant arguments, but when that is done the result is not a constant expression.
4 控制语句
4.1 if 语句
bool Accept() { cout << "Do you want to proceed (y or n)? "; // write question char answer = 0; cin >> answer; // read answer if(answer == 'y') return true; return false; }
4.2 switch 语句
bool Accept2() { cout << "Do you want to proceed (y or n)? "; // write question char answer = 0; cin >> answer; // read answer switch(answer) { case 'y': return true; case 'n': return false; default: cout << "I will take that for a no. "; return false; } }
4.3 while 语句
bool Accept3() { int tries = 1; while(tries < 4){ cout << "Do you want to proceed (y or n)? "; // write question char answer = 0; cin >> answer; // read answer switch(answer) { case 'y': return true; case 'n': return false; default: cout << "Sorry, I don't understand that. "; ++tries; // increment } } cout << "I'll take that for a no. "; return false; }
5 指针和数组
5.1 [] 和 *
[ ] 等于 ‘‘array of’’, ∗ 意思是 ‘‘pointer to’’
char v[6]; // array of 6 characters char∗ p; // pointer to character
前缀 ∗ 是取内容, 前缀 & 是取地址
char∗ p = &v[3]; // p points to v’s fourth element char x = ∗p; // *p is the object that p points to
T a[n]; // T[n]: array of n Ts T∗ p; // T*: pointer to T T& r; // T&: reference to T T f(A); // T(A): function taking an argument of type A returning a result of type T
5.2 拷贝和输出
拷贝一个数组的元素到另一个数组
void CopyFct() { int v1[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int v2[10]; for(auto i=0; i!=10; ++i) // copy elements v2[i] = v1[i]; }
将 v 中每一个元素的值 , 拷贝给 x 并显示其输出。
void Print() { int v[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; for(auto x : v) // range-for 的形式一 std::cout << x << ' '; // for every element of v, from the first to the last, place a copy in x and print it for(auto x : {21, 32, 43, 54, 65}) std::cout << x << ' '; }
5.3 引用
void Increment() { int v[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; for(auto &x : v) // range-for 的形式二 ++x; }
后缀 & 意思是 ‘‘reference to’’,类似指针,但却不需要一个前缀 ∗ 来访问值
// count the number of occurrences of x in p[] // p is assumed to point to a zero-ter minated array of char (or to nothing) int CountX(char* p, char x) { if(p==nullptr) return 0; int count = 0; for(; *p!=0; ++p) if(*p == x) ++count;
return count; }
其中,nullptr 参见博文 C++11 之 nullptr
参考资料:
<C++ Programming Language_4th>