語言特性:
1. Templates模板
1
2
3
4
5
|
template < class T> inline const T& const T& const T& { return a } |
無類型的模板參數
1
2
|
bitset<32> bitset<52> |
默認模板參數
1
2
3
4
|
template < class T, class container=vector<T> class MyClass; MyClass< int > // |
typename作用
1
2
3
4
5
6
7
8
9
10
|
//作為模板形慘 template < typename T> class MyClass; //使用嵌套依賴類型,typename後面的是類型名稱而不是成員函數或變數 template < class T> class MyClass //ptr typename T::SubType }; |
模板成員member template
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
class MyClass template < class T> void f(T); }; template < class T> class MyClass private : T public : void assign( const MyClass<T>& { value } //x }; void f() { MyClass< double > MyClass< int > d.assign(d); //ok d.assign(i); //error } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
template < class T> class MyClass private : T public : template < class X> void assign( const MyClass<X>& { value } T const { return value; } }; void f() { MyClass< double > MyClass< int > d.assign(d); //ok d.assign(i); //ok } |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
template < class T> class MyClass<T> public : template < class U> MyClass( const MyClass<U> }; void f() { MyClass< double > MyClass< double > //call內置的拷貝構造函數 MyClass< int > //call } |
嵌套模板類
1
2
3
4
5
6
|
template < class T> class MyClass ... template < class T2> class NestedClass; }; |
2. 初始化基礎類型
1
2
3
4
5
6
7
8
9
|
int i1; //沒有初始化 int i2 int (); //初始化為0 template < class T> void f() { //保證了如果T是基礎類型,一定初始化為0 T }; |
3. 異常處理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class Error; void f() { if (exception) { throw Error(); } } int main() { try { f(); } catch ( const Error& //處理異常 } } |
4. 命名空間namespace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
//避免名字衝突 namespace FSU class MyClass; void f(); namespace HTTP class HTTPHandle; } } //使用namespace int main() { FSU::MyClass FSU::f(); FSU::HTTP::HTTPHandle } //使用namespace另一種方法 //using using FSU::MyClass; //推薦使用這種辦法 |
5. bool類型
1
2
3
|
//只有兩個值 //0 bool flag true ; |
6. explicit關鍵字
1
2
3
4
5
6
7
8
|
class Stack { //避免隱式轉換 explicit Stack( int size); }; Stack //ok Stack //error |
7. C++ 類型轉換
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
//static_cast float f; cout static_cast < int >(f) //print f( static_cast <string>( "string" )); //call //dynamic_cast class Car; //Car class BlueCar public Car class RedCar public Car void f(Car* { BlueCar* dynamic_cast <BlueCar*>(pCar); if (pBlueCar //pBlueCar } } //const_cast //消除const限定 const int a const int * int * const_cast < int *>(pA); *b //reinterpret_cast //最常在函數指針類型之間的轉換 typedef void (*FuncPtr)(); FuncPtr int doSomething(); funcPtrArray[0] //error,函數返回值不匹配 funcPtrArray[0] reinterpret_cast <FuncPtr>(&doSomething); //ok |
8. static const member 的初始化
1
2
3
4
5
6
7
|
class MyClass public : static const int i //only }; //定義式 const int MyClass::i; //不用再初始化了 |
9. 大O複雜度
常量 | O(1) | runtime決定於元素的數目 |
對數 | O( log(n) ) | 對數曲線 |
線性 | O(n) | 線性增長 |
n-log-n | O( n*log(n) ) | |
平方 | O(n*n) | 平方 |