Hello world
#include<iostream>using namespace std;int main(){ cout << "hello, world!" << endl; return 0;}return 0
可以省略.
基本數據類型
#include<iostream>using namespace std;int main(){ cout << "size of char: " << sizeof(char) << endl; cout << "size of int: " << sizeof(int) << endl; cout << "size of long int: " << sizeof(long int) << endl; cout << "size of float: " << sizeof(float) << endl; cout << "size of double: " << sizeof(double) << endl; cout << "size of wide char: " << sizeof(wchar_t) << endl;}long
, short
, unsigne
這些為修飾符.
typedef
#include<iostream>using namespace std;typedef int newtype;int main(){ newtype a = 10; cout << a << endl;}注意typedef也需要分號.
enum
#include<iostream>using namespace std;enum color{r, g = 30, b} c;int main(){ c = r; cout << c << endl; c = g; cout << c << endl; c = b; cout << c << endl; color d = c; cout <<d << endl; }定義的enum名字是合法的類型. 其成員名字也是合法的變量, 可以直接訪問, 但不能修改. 在定義enum時, 如果沒有指定值, 則從0開始遞增. 只能使用整數指定. 三種變量
http://www.runoob.com/cplusplus/cpp-variable-scope.html 作用域是程序的一個區域,一般來說有三個地方可以聲明變量:
在函數或一個代碼塊內部聲明的變量,稱為局部變量。在函數參數的定義中聲明的變量,稱為形式參數。在所有函數外部聲明的變量,稱為全局變量。常量的定義
#include<iostream>using namespace std;#define NUM_A 30int main(){ const int NUM_B = 20; cout << NUM_A << endl; cout << NUM_B << endl;}可以#define
宏, 也可以用const
關鍵字
static
#include<iostream>using namespace std;void fn(void);int main(){ for (int i = 0; i < 3; i++) { fn(); } }void fn(){ static int count = 0; cout << count++ << endl;}012類似于java里的類內static變量, 值只初始化一次, 并且在程序執行期間保持.
extern
http://www.runoob.com/cplusplus/cpp-storage-classes.html extern 存儲類用于提供一個全局變量的引用,全局變量對所有的程序文件都是可見的。當您使用 ‘extern’ 時,對于無法初始化的變量,會把變量名指向一個之前定義過的存儲位置。
當您有多個文件且定義了一個可以在其他文件中使用的全局變量或函數時,可以在其他文件中使用 extern 來得到已定義的變量或函數的引用??梢赃@么理解,extern 是用來在另一個文件中聲明一個全局變量或函數。
extern 修飾符通常用于當有兩個或多個文件共享相同的全局變量或函數的時候
rand
#include <iostream>#include <ctime>#include <cstdlib>using namespace std;int main(){ cout << time(NULL) << endl; srand(time(NULL)); for (int i = 0; i < 10; i++) { int j = rand(); cout << j << endl; }}調用rand之間需要初始化種子, 否則每次生成的隨機數變化很小.
array與setw的使用
#include <iostream>using namespace std;#include<iomanip>using std::setw;int main(){ int a[10] = {1, 2, 3}; // the left will be initiallized with 0. for (int i = 0 ; i < 10; i++) { cout << setw(4) << i << ":" <<setw(8)<< a[i] << endl; } int b[] = {1, 2, 3};}setw()
函數用來格式化輸出.
char數組
#include <iostream>using namespace std;int main(){ char a[] = {'a', 'b'}; // 沒有添加 /0, 輸出結果不可預測 cout << a << endl; char b[] = {'a', 'b', '/0'}; cout << b << endl; char c[] = "abc"; cout << c << endl;// char d[3] = "efg";// error: initializer-string for array of chars is too long char d[4] = "efg"; // /0 will be added automatically. cout << d << endl;}二維數組與指針
#include <iostream>using namespace std;int main(){ int a[2][2] = {{1, 2}, {3, 4}}; int b[2][2] = {1, 2, 3, 4}; int *ptr = b[0]; for (int i = 0; i < 2; i++) { for (int j = 0 ; j < 2; j++) { cout << i << "," << j << ":" << a[i][j] << "," << b[i][j] << "," << *(ptr + i * 2 + j) << endl; } }}數組作為形參
#include <iostream>using namespace std;//double average(int *arr, int len) // the same.double average(int arr[], int len){ double sum = 0; for(int i = 0; i < len; i++) { sum += arr[i]; } return sum / len;}int main(){ const int len = 10; int a[len]; for(int i = 0; i < len; i++) { a[i] = i; } int *b = a; cout << average(a, len) << endl; cout << average(b, len) << endl;}array作為返回值
#include <iostream>#include <cstdlib>#include <ctime>using namespace std;const int len = 10;int* get_arr(){ static int a[len];//static must be PRovided if the program had to be written in this style; for(int i = 0; i < len; i++) { a[i] = i; } return a;}int main(){ int* a = get_arr(); for(int i = 0; i < len; i++) { cout << a[i] << endl; }}String
#include <iostream>#include <string>using namespace std;int main(){ string a = "Hello, "; string b = " world!"; cout << a + b << endl; cout << a.size() << endl; cout << (a > b) << endl;}explicit
explicit
關鍵字是為了防止構造函數自動調用: 自動將cls a;
轉換為cls a()
#include <iostream>//#include"explicit_demo.hpp"using namespace std;class Demo{public: explicit Demo(int i);};Demo::Demo(int i){ cout << "Demo(int i)" << endl;}int main(){ Demo e = 2;}去掉explicit
能通過編譯, 加上則不行.