现在的位置: 首页 > 综合 > 正文

C++ STL — 第2章 介绍C++和标准库

2014年10月30日 ⁄ 综合 ⁄ 共 3326字 ⁄ 字号 评论关闭

语言特性:

1. Templates模板

1
2
3
4
5
template<class T>
inline const T&
max(
const T&
a, 
const T&
b)
{
    return a
< b ? b : a;
}

无类型的模板参数

1
2
bitset<32>
bit32;
bitset<52>
bit51;

默认模板参数

1
2
3
4
template<class T, class container=vector<T>
>
class MyClass;
    
MyClass<int>
x;  
//
equal to MyClass<int, vector<int> >

typename作用

1
2
3
4
5
6
7
8
9
10
//作为模板形惨
template<typename T>
class MyClass;
    
//使用嵌套依赖类型,typename后面的是类型名称而不是成员函数或变量
template<class T>
class MyClass
{
    //ptr
is a pointer to type T::SubType
    typename T::SubType
* ptr;
};

模板成员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
value;
public:
    void assign(const MyClass<T>&
x)
    {
        value
= x.value;
    }
    //x
必须和*this拥有同样的类型
};
    
void f()
{
    MyClass<double>
d;
    MyClass<int>
i;
    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
value;
public:
    template<class X>
    void assign(const MyClass<X>&
x)
    {
        value
= x.getValue;
    }
    T
getValue() 
const
    {
        return value;
    }
};
     
void f()
{
    MyClass<double>
d;
    MyClass<int>
i;
    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>
&other);
};
    
void f()
{
    MyClass<double>
d;
    MyClass<double>
d2(d); 
//call内置的拷贝构造函数
    MyClass<int>
i(d); 
//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
t = 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&
e) {
        //处理异常
    }
}

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
myClass;
    FSU::f();
    FSU::HTTP::HTTPHandle
handle;
}
    
//使用namespace另一种方法
//using
namespace FSU;
using FSU::MyClass;   //推荐使用这种办法

5. bool类型

1
2
3
//只有两个值
true和false
//0
is false , and other is true
bool flag
true;

6. explicit关键字

1
2
3
4
5
6
7
8
class Stack
{
    //避免隐式转换
    explicit Stack(int size);
};
    
Stack
stack1(50);  
//ok
Stack
stack2 = 50;  
//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)
<< endl; 
//print
f as int type
f(static_cast<string>("string")); //call
f() for string type
     
//dynamic_cast
class Car;  //Car
至少有一个虚函数,抽象基类
class BlueCar
public Car
{};
class RedCar
public Car
{};
     
void f(Car*
pCar)
{
    BlueCar*
pBlueCar = 
dynamic_cast<BlueCar*>(pCar);
    if(pBlueCar
== NULL) {
        //pBlueCar
不是指向BlueCar的对象
    }
}
    
//const_cast
//消除const限定
const int a
= 9;
const int*
pA = &a;
int*
b = 
const_cast<int*>(pA);
*b
= 99;
    
//reinterpret_cast
在编译期处理,可能不安全
//最常在函数指针类型之间的转换
typedef void (*FuncPtr)();
FuncPtr
funcPtrArray[10];
int doSomething();
funcPtrArray[0]
= &doSomething; 
//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
= 9; 
//only
inteter can
};
    
//定义式
const int MyClass::i;  //不用再初始化了

9. 大O复杂度

常量     O(1) runtime决定于元素的数目
对数 O( log(n) ) 对数曲线
线性 O(n) 线性增长
n-log-n     O( n*log(n) )  
平方     O(n*n) 平方

抱歉!评论已关闭.