C++数据结构与算法 目录
本文前驱课程
1 C++自学精简教程 目录(必读)
2 动态数组 Vector(难度1)
其中,2 是 1 中的一个作业。2 中详细讲解了动态数组实现的基本原理。
本文目标
1 学会写基本的C++类模板语法;
2 为以后熟练使用 STL 打下基础;
3 为更进一步的阅读和掌握更多的C++库打下基础;
模板语法的学习最恰当的方式就是和非模板代码对比学习。
本文的内容只是在 2 动态数组 Vector(难度1)的基础上,把代码改造为模板语法。
除此之外,不添加任何内容。
模板语法介绍
当类的成员变量是不确定的类型的时候,我们使用模板类( template class)来实现这样的类。
模板类的定义如下:
template<typename T>
struct Vector{
T data;//成员变量 data 的类型不确定,写成 T
};
解释说明:
(1)template 表示后续代码中有类型是不确定的,先用typename T当中的T表示类型;
(2)typename表示T是一个未来才能确定的类型
(3)确定T的类型的方式就是创建一个Vector对象的时候在<>中指定:
Vector<int> arr; //这条语句表示用int替换代码中的T
非模板语法与模板语法差异对比图
代码与注释如下
#include<iostream>
#include <iomanip>
#include <cassert>
using namespace std;
//------下面的代码是用来测试你的代码有没有问题的辅助代码,你无需关注------
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
struct Record { Record(void* ptr1, size_t count1, const char* location1, int line1, bool is) :ptr(ptr1), count(count1), line(line1), is_array(is) { int i = 0; while ((location[i] = location1[i]) && i < 100) { ++i; } }void* ptr; size_t count; char location[100] = { 0 }; int line; bool is_array = false; bool not_use_right_delete = false; }; bool operator==(const Record& lhs, const Record& rhs) { return lhs.ptr == rhs.ptr; }std::vector<Record> myAllocStatistic; void* newFunctionImpl(std::size_t sz, char const* file, int line, bool is) { void* ptr = std::malloc(sz); myAllocStatistic.push_back({ ptr,sz, file, line , is }); return ptr; }void* operator new(std::size_t sz, char const* file, int line) { return newFunctionImpl(sz, file, line, false); }void* operator new [](std::size_t sz, char const* file, int line)
{ return newFunctionImpl(sz, file, line, true); }void operator delete(void* ptr) noexcept { Record item{ ptr, 0, "", 0, false }; auto itr = std::find(myAllocStatistic.begin(), myAllocStatistic.end(), item); if (itr != myAllocStatistic.end()) { auto ind = std::distance(myAllocStatistic.begin(), itr); myAllocStatistic[ind].ptr = nullptr; if (itr->is_array) { myAllocStatistic[ind].not_use_right_delete = true; } else { myAllocStatistic[ind].count = 0; }std::free(ptr); } }void operator delete[](void* ptr) noexcept {Record item{ ptr, 0, "", 0, true }; auto itr = std::find(myAllocStatistic.begin(), myAllocStatistic.end(), item); if (itr != myAllocStatistic.end()) { auto ind = std::distance(myAllocStatistic.begin(), itr); myAllocStatistic[ind].ptr = nullptr; if (!itr->is_array) { myAllocStatistic[ind].not_use_right_delete = true; } else { myAllocStatistic[ind].count = 0; }std::free(ptr); }}
#define new new(__FILE__, __LINE__)
struct MyStruct { void ReportMemoryLeak() { std::cout << "Memory leak report: " << std::endl; bool leak = false; for (auto& i : myAllocStatistic) { if (i.count != 0) { leak = true; std::cout << "leak count " << i.count << " Byte" << ", file " << i.location << ", line " << i.line; if (i.not_use_right_delete) { cout << ", not use right delete. "; } cout << std::endl; } }if (!leak) { cout << "No memory leak." << endl; } }~MyStruct() { ReportMemoryLeak(); } }; static MyStruct my; void check_do(bool b, int line = __LINE__) { if (b) { cout << "line:" << line << " Pass" << endl; } else { cout << "line:" << line << " Ohh! not passed!!!!!!!!!!!!!!!!!!!!!!!!!!!" << " " << endl; exit(0); } }
#define check(msg) check_do(msg, __LINE__);
//------上面的代码是用来测试你的代码有没有问题的辅助代码,你无需关注------
//注意:禁止修改Vector的定义,包括禁止给Vector添加成员变量;
//可以添加私有成员函数,如果你认为需要的话
template<typename T>
struct Vector
{
public:
Vector();
Vector(int n, T value);
Vector(const Vector& from);
Vector(int* begin, int* end);
~Vector();
int size() const;
//只读元素
//参考 https://zhuanlan.zhihu.com/p/539451614
const T& operator[](int n)const { return m_data[n]; }
//写入元素
T& operator[](int n) { return m_data[n]; }
void push_back(T value);
bool empty() const;// your job 1
void clear();// your job 2
Vector& operator = (const Vector& from);// your job 4
private:
void copy(const Vector& from);// your job 3
private:
int m_element_cout;
int m_capacity;
T* m_data;//定义一个元素类型待定的数组起始元素的指针
//请忽略下面这个成员变量,这个成员变量不影响你的实现,当它不存在即可。
};
//模板类的成员函数都要以下面的template语法开始,和类声明的地方一样
template<typename T>
Vector<T>::Vector()
{
m_element_cout = 0;
m_capacity = 10;
m_data = new int[m_capacity];
}
template<typename T>
Vector<T>::Vector(int n, T value) :Vector()
{
for (int i = 0; i < n; i++)
push_back(value);
}
template<typename T>
Vector<T>::Vector(const Vector& from)
{
m_element_cout = from.m_element_cout;
m_capacity = from.m_capacity;
m_data = new int[m_capacity];
for (int i = 0; i < m_element_cout; i++)
{
m_data[i] = from.m_data[i];
}
}
template<typename T>
Vector<T>::Vector(int* begin, int* end) :Vector()
{
for (int* p = begin; p < end; p++)
{
push_back(*p);
}
}
template<typename T>
Vector<T>::~Vector()
{
delete[] m_data;
}
template<typename T>
int Vector<T>::size() const
{
return m_element_cout;
}
template<typename T>
void Vector<T>::push_back(T value)
{
if (m_element_cout < m_capacity)
{
m_data[m_element_cout] = value;
m_element_cout++;
}
else
{
int* p;
p = new T[2 * m_capacity];
for (int j = 0; j < m_element_cout; j++)
{
p[j] = m_data[j];
}
p[m_element_cout] = value;
m_element_cout = m_element_cout + 1;
m_capacity = 2 * m_capacity;
delete[]m_data;
m_data = p;
}
}
//(1) your code for : Vector empty()
//(2) your code for : Vector clear()
//(3) your code for : Vector copy()
//(4) your code for : Vector operator =
void test1(void)
{
Vector<int> v;//创建一个存放int变量的容器v
int i;
check(v.size() == 0);
for (i = 0; i < 10; i++)
{
v.push_back(i);
}
for (int i = 0; i < 10; i++)
{
check(v[i] == i);
}
check(v.size() == 10);
}
void test2(void)
{
int n = 100000;
Vector<int> v;
int i;
check(v.size() == 0);
for (i = 0; i < n; i++)
{
v.push_back(i);
}
for (int i = 0; i < n; i++)
{
assert(v[i] == i);
}
check(v.size() == n);
}
void print(Vector<int>& v, const std::string& msg)
{
std::cout << "The contents of " << msg.c_str() << " are:";
for (int i = 0; i != v.size(); ++i)
{
std::cout << ' ' << v[i];
}
std::cout << '\n';
}
void test3()
{
Vector<int> a;
Vector<int> first; // empty vector of ints
check(first.empty() == true && first.size() == 0);
Vector<int> second(4, 100); // four ints with value 100
check(second.empty() == false);
check(second.size() == 4);
Vector<int> fourth(second); // a copy of third
check(fourth.size() == second.size());
int myints[] = { 16,2,77,29 };
Vector<int> fifth(myints, myints + sizeof(myints) / sizeof(int));
check(fifth.empty() == false);
check(fifth[0] == 16);
check(fifth[3] == 29);
check(fifth.size() == sizeof(myints) / sizeof(int));
print(fifth, "fifth");//The contents of fifth are:16 2 77 29
fifth.push_back(30);
check(fifth[4] == 30);
check(fifth.size() == 5);
print(fifth, "fifth");//The contents of fifth are:16 2 77 29 30
check(fifth.size() == sizeof(myints) / sizeof(int) + 1);
first = fifth = fifth;
print(first, "first");//The contents of first are:16 2 77 29 30
check(first.empty() == false && first.size() == fifth.size());
Vector<int> a1(myints, myints + sizeof(myints) / sizeof(int));
//下面大括号是作用域,用来把代码分开互不干扰,这样就可以创建相同的变量名字了,就不用为了起很多不同的变量名而烦恼了。
{
Vector<int> b(a1);
b.push_back(2);
check(b[4] == 2);
}
{
Vector<int> c;
c = a1;
}
check(a1.size() == sizeof(myints) / sizeof(int));
{
Vector<int> c;
c = fifth;
c[0] = 1;
check(c[0] == 1);
}
}
int main()
{
test1();
test2();
test3();
return 0;
}