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 30
int 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;
}
0
1
2
类似于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
能通过编译, 加上则不行.
vector
#include <vector>
using namespace std;
int main()
{
vector<int> list;
//size
cout << list.size() << endl;
//is empty
cout << "Is empty? " << (list.empty()? "Yes": "No" )<< endl;
//push back
for (int i = 0;i < 5; i++)
{
list.push_back(i);
}
// insert
list.insert(list.begin(), 20000);
cout << list.size() << endl;
cout << "Is empty? " << (list.empty()? "Yes": "No" )<< endl;
cout << list[0] << endl;
//iterator
vector<int>::iterator v = list.begin();
while(v != list.end())
{
cout << *v++ << " ";
}
cout << endl;
//front and back
cout << list.front() << endl; // the first element
cout << list.back() << endl; // the last element
//data
int* p = list.data();
cout << p[0] << endl;
//remove specifical elements
cout << "Before Remove:" ;
v = list.begin();
while(v != list.end())
{
cout << *v++ << " ";
}
list.erase(list.begin());
list.erase(list.begin(), list.begin() + 1);
v = list.begin();
cout <<endl << "After Remove:";
while(v != list.end())
{
cout << *v++ << " ";
}
cout << endl;
//remove all elements
list.clear();
cout << "After clear(), Is empty? " << (list.empty()? "Yes": "No" )<< endl;
}
map
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
map<string, int> m;
//insert
m["a"] = 2;
m["c"] = 3;
cout << m["a"] << endl;
//find
map<string, int>::iterator it = m.find("a");
cout << (it->first) << ":" << (it->second) << endl;
it = m.find("b");
cout << (it != m.end() ? "b found": "b not found.") << endl;
//count
cout << "Number of a:" << m.count("a") << endl;
cout << "Number of b:" << m.count("b") << endl;
//size
cout << "Size of map: " << m.size() << endl;
//iterator
cout << "Map content:" << endl;
map<string, int>:: iterator iterator = m.begin();
for(;iterator != m.end(); ++iterator)
{
cout << (iterator->first) << ":" << (iterator->second) << endl;
}
//erase
m.erase("a");
}
sort
#include<iostream>
#include<vector>
#include <algorithm>
#include<map>
#include<set>
using namespace std;
bool cmp(int v1, int v2)
{
return v2 - v1 < 0;
}
void print_vector(vector<int>v)
{
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
}
int main()
{
vector<int> v;
v.push_back(2);
v.push_back(5);
v.push_back(4);
print_vector(v);
cout << "Asc: ";
sort(v.begin(), v.end(), cmp);
print_vector(v);
cout << "Desc:" ;
sort(v.begin(), v.end());
print_vector(v);
}
set
#include<iostream>
#include<vector>
#include<set>
using namespace std;
void print_vector(vector<int>v)
{
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
}
int main()
{
vector<int> v;
v.push_back(2);
v.push_back(5);
v.push_back(4);
v.push_back(4);
set<int> s(v.begin(), v.end());
for(set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
s.insert(10); //insert
vector<int> v2(s.begin(), s.end());
print_vector(v2);
}
stack
#include<iostream>
#include<stack>
using namespace std;
int main()
{
stack<int> s;
for (int i = 0; i < 10; i++)
{
s.push(i);
}
cout << "Empty? " << (s.empty()? "Yes":"No") << endl;
cout << "Size before top: " << s.size() << endl;
cout << "Top:" << s.top() << endl;
cout << "Size after top: " << s.size() << endl;
cout << "Size before pop: " << s.size() << endl;
s.pop();
cout << "Size after pop: " << s.size() << endl;
}
pow
#include<cmath>
#include<iostream>
using namespace std;
int main()
{
cout << pow(10.0, 0) << endl; // pow(base, power)
}
模板函数, 泛型, toString
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
template<class T>
string toString(T i)
{
string s;
stringstream ss;
ss << i;
ss >> s;
return s;
}
int main()
{
string s = toString(10.1);
cout << s << endl;
}
heap
#include<queue>
#include<iostream>
#include<vector>
using namespace std;
int main()
{
// priority_queue<int> q;//big end
priority_queue<int, vector<int>, greater<int> > q;//little end
for(int i = 10; i < 20; i++)
{
q.push(i);
}
while(!q.empty())
{
cout << q.top() << endl;
q.pop();
}
}
#include<queue>
#include<vector>
#include<iostream>
using namespace std;
struct Edge{
int val;
Edge(int v)
{
this->val = v;
}
};
struct cmp
{
bool operator ()(const Edge& e1, const Edge& e2)
{
return e1.val > e2.val;
}
};
int main()
{
priority_queue<Edge, vector<Edge>, cmp> heap;
for(int i = 10; i > 0; i--)
{
Edge e(i);
heap.push(e);
}
while(!heap.empty())
{
cout << heap.top().val<< endl;
heap.pop();
}
}