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
能通过编译, 加上则不行.