const语义变化
为解决 const 关键字的双重语义问题,C++11 保留了 const 表示 “只读” 的语义,而将 “常量” 的语义划分给了 constexpr。因此,C++11 标准中,建议将 const 和 constexpr 的 功能区分开,即:凡是 表达 “只读” 语义的场景都用 const,表达 “常量” 语义的场景都用 constexpr (若认定一个变量为常量表达式,就将其声明为 constexpr 类型)。
可能有人疑问,“只读” 不就意味着其不能被修改吗?事实上,由前文可知,“只读 ”和 “不允许被修改” 之间并无必然联系,例如:
int a = 10;
const int &con_b = a;
cout << con_b << endl; # 10
a = 20;
cout << con_b << endl; # 20
上述 常量引用例程 中,用 const 修饰了 con_b 变量,表示该变量 “只读”,即无法通过变量自身去修改所绑定对象的值。但这并不意味着 con_b 的值不能经由其它途径 (如其他变量) 间接改变,通过改变 a 的值就可以使 con_b 的值发生变化。这完全符合先前的结论。
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_39478403/article/details/112138437
抄一个环形缓存的实现
//
// ring_buffer.h
//
#pragma once
template <class T>
class RingBuffer {
private:
unsigned int size_;
int front_;
int rear_;
T* data_;
public:
RingBuffer(unsigned size) : size_(size) {
data_ = new T[size_];
}
~RingBuffer() {
delete [] data_;
data_ = nullptr
}
inline bool IsEmpty() const {
return front_ == rear_;
}
inline bool IsFull() const {
return front_ == (rear_ + 1) % size_;
}
bool Push(const T& value) {
if (IsFull()) {
return false;
}
data_[rear_] = value;
rear_ = (rear_ + 1) % size_;
return true;
}
bool Push(const T* value) {
if (IsFull()) {
return false;
}
data_[rear_] = *value;
rear_ = (rear_ + 1) % size_;
return true;
}
bool Pop() {
if (IsEmpty()) {
return false;
}
value = data_[front_];
front_ = (front_ + 1) % size_;
return true;
}
};
抄一个大顶堆的简单实现
template<typename T>
class MaxHeap {
public:
MaxHeap(int max_size);
~MaxHeap();
void Push(T data);
void Pop();
T& Top();
void ShowHeap();
private:
T* heap_;
int max_size_;
int current_size_;
void ChangeSize();
};
template<typename T>
MaxHeap<T>::MaxHeap(int max_size) : max_size_(max_size) {
if (max_size_ > 0) {
heap_ = new T[max_size_];
}
curent_size_ = 0;
}
template<typename T>
MaxHeap<T>::~MaxHeap() {
delete [] heap_;
}
template<typename T>
void MaxHeap<T>::Push(T data) {
if (current_size_ == max_size_) {
ChangeSize();
}
heap_[current_size_] = data;
int son_index = current_size_;
int father_index = (current_size_ - 1) / 2;
while (son_index > 0) {
if (heap_[son_index] > heap_[father_index]) {
std::swap(heap_[son_index] , heap_[father_index]);
son_index = father_index;
father_index = (father_index - 1) / 2;
continue;
}
break;
}
current_size_++;
}
template<typename T>
void MaxHeap<T>::Pop() {
if (current_size_ == 0) {
return;
}
heap_[0] = heap_[--current_size_];
int father_index = 0;
int left_son = father_index * 2 + 1;
int right_son = left_son + 1;
while (left_son < current_size_) {
int max_index = right_son < current_size_ && heap_[left_son] < heap_[right_son] ? right_son : left_son;
if (heap_[father_index] > heap_[max_index]) {
break;
}
std::swap(heap_[father_index] , heap_[max_index]);
father_index = max_index;
left_son = father_index * 2 + 1;
right_son = left_son + 1;
}
}
template<typename T>
void MaxHeap<T>::ChangeSize() {
int new_size = max_size_ * 2;
T* temp = new T[new_size];
std::copy(heap_, heap_+max_size_, temp);
delete [] heap_;
heap_ = temp;
max_size_ = new_size;
}