0%

C++11新特性

nullptr

nullptr

1
2
3
4
5
6
7
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif

0 \0 NULL nullptr 的差异和共同点

auto与decltype

decltype

右值引用

浅拷贝与深浅拷贝

阅读全文 »

声明和定义都放在.hpp中

演示代码
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#pragma once

namespace lt {
template<class T>
class Stack {
private:
T* arr;
int top;
int size;
void resize(int size);
public:
Stack();
Stack(int size);
~Stack();
void Push(T e);
void Pop();
T Top();
bool Empty();
};
}


#include<cstdlib>
#include"datastructure/Stack.hpp"
using namespace lt;
#define MALLOCSIZE 256
template<class T>
Stack<T>::Stack() :size(MALLOCSIZE), top(0), arr(NULL) {
arr = (T*)malloc(sizeof(T) * size);
}
template<class T>
Stack<T>::Stack(int _size) : size(_size), top(0), arr(NULL) {
arr = (T*)malloc(sizeof(T) * size);
}
template<class T>
Stack<T>::~Stack() {
free(arr);
}
template<class T>
void Stack<T>::Push(T e) {
if (size <= top) {
resize(size + MALLOCSIZE);
}
arr[top++] = e;
}
template<class T>
void Stack<T>::Pop() {
top--;
}
template<class T>
T Stack<T>::Top() {
return arr[top - 1];
}
template<class T>
bool Stack<T>::Empty() {
return top == 0;
}
template<class T>
void Stack<T>::resize(int _size) {
arr = (T*)realloc(arr, sizeof(T) * _size);
size = _size;
}

声明放在.hpp文件中,定义放在.cpp文件中

声明放在.hpp文件中,定义放在.cpp文件中,需要在.cpp文件中将所有用到的类型显示实例化
编译器怎么处理模板呢?本质上来说,模板可被编译器产生多种多样函数和类的代码。只有当被使用的时候,编译器才会产生用于这种类型的代码。模板不是直接编译成以后需要的各种类的实现,而是将模板代码解析放入内存中,当编译某个编译单元需要用到该模板,则会按需产生相应的代码。实例化是编译器确定特定模板与特定参数集一起使用并在模板上执行参数替换以生成要编译的类或函数以及最终编译为模板的二进制代码的过程。

.hpp演示代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once

namespace lt {
template<class T>
class Stack {
private:
T* arr;
int top;
int size;
void resize(int size);
public:
Stack();
Stack(int size);
~Stack();
void Push(T e);
void Pop();
T Top();
bool Empty();
};
}
.cpp演示代码
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
33
34
35
36
37
38
39
40
41
42
#include<cstdlib>
#include"datastructure/Stack.hpp"
using namespace lt;
#define MALLOCSIZE 64
template<class T>
lt::Stack<T>::Stack():size(MALLOCSIZE),top(0),arr(NULL) {
arr = (T*)malloc(sizeof(T) * size);
}
template<class T>
lt::Stack<T>::Stack(int _size):size(_size), top(0), arr(NULL) {
arr = (T*)malloc(sizeof(T) * size);
}
template<class T>
lt::Stack<T>::~Stack() {
free(arr);
}
template<class T>
void lt::Stack<T>::Push(T e) {
if (size <= top) {
resize(size+ MALLOCSIZE);
}
arr[top++] = e;
}
template<class T>
void lt::Stack<T>::Pop() {
top--;
}
template<class T>
T lt::Stack<T>::Top() {
return arr[top-1];
}
template<class T>
bool lt::Stack<T>::Empty() {
return top == 0;
}
template<class T>
void lt::Stack<T>::resize(int _size) {
arr = (T*)realloc(arr,sizeof(T) * _size);
size = _size;
}

template class Stack<int>;
阅读全文 »

多线程

C++11新标准多线程支持库
< thread > : 提供线程创建及管理的函数或类接口;
< mutex > : 为线程提供获得独占式资源访问能力的互斥算法,保证多个线程对共享资源的同步访问;
< condition_variable > : 允许一定量的线程等待(可以定时)被另一线程唤醒,然后再继续执行;
< future > : 提供了一些工具来获取异步任务(即在单独的线程中启动的函数)的返回值,并捕捉其所抛出的异常;
< atomic > : 为细粒度的原子操作(不能被处理器拆分处理的操作)提供组件,允许无锁并发编程。

并发与并行

  • 并发:同一时间段内可以交替处理多个操作,强调同一时段内交替发生。
  • 并行:同一时刻内同时处理多个操作,强调同一时刻点同时发生。

无参函数

1
2
3
4
5
6
7
8
9
10
11
12
13
#define THREADINGNUMS 500
void threadfunc() {
for (int i = 0; i < 100000; i++) {
cout << "thread"<< 1<<":" << i << endl;
}
}
void multithreading(){
std::thread t[THREADINGNUMS];
for (int i = 0; i < THREADINGNUMS; i++) {
t[i] = std::thread(threadfunc);
t[i].detach();
}
}

有参函数

1
2
3
4
5
6
7
8
9
10
11
12
13
#define THREADINGNUMS 500
void threadfunc(int index) {
for (int i = 0; i < 100000; i++) {
cout << "thread"<< index<<":" << i << endl;
}
}
void multithreading(){
std::thread t[THREADINGNUMS];
for (int i = 0; i < THREADINGNUMS; i++) {
t[i] = std::thread(threadfunc,i);
t[i].detach();
}
}

类内部创建线程

join和detach

阅读全文 »

静态分配指针数组

1
A* a[NUMS];

动态分配指针数组

1
A **a= new A*[NUMS];
阅读全文 »