网站标题写什么作用,网站建设维护岗位,南通的电商网站建设,网站建设翻译成英文嵌套使用模板类 嵌套使用模板类最常见的场景数组容器中有栈容器栈容器中有数组容器递归使用模板类 嵌套使用模板类最常见的场景
容器中有容器 数组的元素可以是栈#xff0c;栈中的元素可以是数组。先来看一下Stack和Vector的基本代码#xff0c;定长数组Array的代码也给出来… 嵌套使用模板类 嵌套使用模板类最常见的场景数组容器中有栈容器栈容器中有数组容器递归使用模板类 嵌套使用模板类最常见的场景
容器中有容器 数组的元素可以是栈栈中的元素可以是数组。先来看一下Stack和Vector的基本代码定长数组Array的代码也给出来但是不会用到代码如下
#include iostream
using namespace std;// 定长数组 Array
template class T, int len10
class Array {
private:T* items[len];
public:Array() {}~Array() {}T operator[](int index) {return items[index];}const T operator[] (int index) const {return items[index];}
};// 栈 Stack
template class DataType
class Stack {
private:DateType* items;int stacksize; int top;
public:Stack(int size3): stacksize(size), top(0) {items new DataType[stacksize];}~Stack() {delete [] items; items nullptr;}bool isEmpty() {return top 0;}bool isFull() {return top stacksize;}bool push(const DateType item) {if(top stacksize) {items[top] item; return true;}return false;}bool pop() {if(top 0) {item items[--top]; return true;}return false;}
};// 动态数组 Vector
template class T
class Vector {
privatet:int len; // 数组元素的个数T* items; // 数组元素
public:Vector(int size2): len(size) { // 元素个数的缺省值为 2items new T[len];}~Vector() {delete [] items; items nullptr;}void resize(int size) {if(size len) return; // 只能往更大的方向扩容T* tmp new T[size]; // 分配更大的内存空间for(int i 0; i len; i) tmp[i] items[i]; // 复制旧数组元素到新数组delete [] items; // 释放旧数组items tmp;len size;}int size() {return len;}T operator [] (int index) {if(index len) resize(index 1);return items[index];}const T operator [] (int index) const {return items[index];} // 重载操作符[]不能修改数组中的元素。
};int main() {return 0;
}数组容器中有栈容器
目前的代码中Vector容器的大小缺省值是2Stack容器的大小缺省值是3。现在演示数组容器Vector中有栈Stack容器的情况代码如下
#include iostream
using namespace std;// 栈 Stack
template class DataType
class Stack {
private:DataType* items;int stacksize; int top;
public:Stack(int size3): stacksize(size), top(0) {items new DataType[stacksize];}~Stack() {delete [] items; items nullptr;}bool isEmpty() {return top 0;}bool isFull() {return top stacksize;}bool push(const DataType item) {if(top stacksize) {items[top] item; return true;}return false;}bool pop(DataType item) {if(top 0) {item items[--top]; return true;}return false;}
};// 动态数组 Vector
template class T
class Vector {
private:int len; // 数组元素的个数T* items; // 数组元素
public:Vector(int size2): len(size) { // 元素个数的缺省值为 2items new T[len];}~Vector() {delete [] items; items nullptr;}void resize(int size) {if(size len) return; // 只能往更大的方向扩容T* tmp new T[size]; // 分配更大的内存空间for(int i 0; i len; i) tmp[i] items[i]; // 复制旧数组元素到新数组delete [] items; // 释放旧数组items tmp;len size;}int size() {return len;}T operator [] (int index) {if(index len) resize(index 1);return items[index];}const T operator [] (int index) const {return items[index];} // 重载操作符[]不能修改数组中的元素。};int main() {// 创建Vector对象vsvs是一个VectorVector中存储Stackstring类型的元素。VectorStackstring vs;// 往容器中插入数据vs[0].push(hello world!); vs[0].push(hihihi); vs[0].push(123456); // vs[0]是一个栈往vs[0]中插入数据vs[1].push(你好); vs[1].push(abcdef); vs[1].push(xxxxx); // vs[1]也是一个栈往vs[1]中插入数据// 用嵌套循环遍历vs中的所有元素for(int i 0; i vs.size(); i) {while(vs[i].isEmpty() false) {string item;vs[i].pop(item);cout pop item item endl;}}return 0;
}运行的结果如下 pop item 123456 pop item hihihi pop item hello world! pop item xxxxx pop item abcdef pop item 你好 容器中的容器就是二维容器但不能简单把它说成二维数组。因为不同的容器可以实现不同的数据结构。像二维数组但不是二维数组。目前的程序Stack是没有扩展功能的而Vector是有resize()扩展功能的那就往Vector容器中再多加一个元素只入栈两个元素其他代码不变main()函数代码如下
int main() {VectorStackstring vs;vs[0].push(hello world!); vs[0].push(hihihi); vs[0].push(123456);vs[1].push(你好); vs[1].push(abcdef); vs[1].push(xxxxx);vs[2].push(Happy Everyday!); vs[1].push(吃好喝好身体健康); // 往Vector中再增加一个Stack数据for(int i 0; i vs.size(); i) {while(vs[i].isEmpty() false) {string item;vs[i].pop(item);cout pop item item endl;}}return 0;
}我的电脑编译运行的结果如下你的电脑可能编译不通过或者编译运行的结果与我的不同总之是不正常的 pop item 123456 pop item hihihi pop item pop item xxxxx pop item abcdef pop item pop item Happy Everyday! free(): double free detected in tcache 2 Aborted (core dumped) 目前这个代码异常的原因在Vector类中具体在扩展数组内存空间的resize()函数里面。 tmp[i] items[i];这条语句的意思是把原数组中的数据拷贝到新数组中去。如果复制的是C内置的数据类型则不存在问题如果复制的是自定义的类并且类中使用了堆区内存就存在浅拷贝的问题。 具体解释一下在Stack类中根据语句DataType* items;可以确定items这个成员变量是指针使用了堆区内存。这样的话对Stack类用浅拷贝是不行的要用深拷贝。也就是说对于Stack这种类一定要重写拷贝构造函数和赋值函数。在此实例中未用到Stack类的拷贝构造函数无需在意但这条语句tmp[i] items[i];用到了Stack类的赋值函数。所以应该为Stack类重写赋值函数实现深拷贝。具体的做法是在Stack类中添加如下代码
Stack operator (const Stack v) {delete [] items;stacksize v.stacksize;items new DataType[stacksize];for(int i 0; i stacksize; i) {items[i] v.items[i];}top v.top;return *this;
}再次编译运行结果如下 pop item 123456 pop item hihihi pop item hello world! pop item xxxxx pop item abcdef pop item 你好 pop item Happy Everyday! 栈容器中有数组容器
给Vector类也加上赋值运算符的重载函数实现深拷贝以便演示栈容器Stack中有数组容器Vector的情况代码如下
#include iostream
using namespace std;// 栈 Stack
template class DataType
class Stack {
private:DataType* items;int stacksize; int top;
public:Stack(int size3): stacksize(size), top(0) {items new DataType[stacksize];}~Stack() {delete [] items; items nullptr;}Stack operator (const Stack v) {delete [] items;stacksize v.stacksize;items new DataType[stacksize];for(int i 0; i stacksize; i) {items[i] v.items[i];}top v.top;return *this;}bool isEmpty() {return top 0;}bool isFull() {return top stacksize;}bool push(const DataType item) {if(top stacksize) {items[top] item; return true;}return false;}bool pop(DataType item) {if(top 0) {item items[--top]; return true;}return false;}
};// 动态数组 Vector
template class T
class Vector {
private:int len; // 数组元素的个数T* items; // 数组元素
public:Vector(int size2): len(size) { // 元素个数的缺省值为 2items new T[len];}~Vector() {delete [] items; items nullptr;}Vector operator (const Vector v) {delete [] items;len v.len;items new T[len];for(int i 0; i len; i) items[i] v.items[i];return *this;}void resize(int size) {if(size len) return; // 只能往更大的方向扩容T* tmp new T[size]; // 分配更大的内存空间for(int i 0; i len; i) tmp[i] items[i]; // 复制旧数组元素到新数组delete [] items; // 释放旧数组items tmp;len size;}int size() {return len;}T operator [] (int index) {if(index len) resize(index 1);return items[index];}const T operator [] (int index) const {return items[index];} // 重载操作符[]不能修改数组中的元素。};int main() {// 创建Stack对象sv是一个栈栈中存储Vectorstring类型的元素。StackVectorstring sv;// 先创建Vectorstring对象再插入到Stack中Vectorstring tmp;// 第一次把字符串数据放到临时容器tmp中再第一次入栈。tmp[0] hi ~; tmp[1] hello ~; sv.push(tmp);// 第二次把字符串数据放到临时容器tmp中再第二次入栈。tmp[0] 666; tmp[1] 888; sv.push(tmp);// 第二次把字符串数据放到临时容器tmp中再第二次入栈。tmp[0] qqqqq; tmp[1] 324fwre; tmp[2] 09ji; sv.push(tmp);// 用嵌套循环遍历sv中的所有元素while(sv.isEmpty() false) {sv.pop(tmp);for(int i 0; i tmp.size(); i) {cout sv[ i ] tmp[i] endl;}}return 0;
}运行结果如下 sv[0] qqqqq sv[1] 324fwre sv[2] 09ji sv[0] 666 sv[1] 888 sv[0] hi ~ sv[1] hello ~ 递归使用模板类
递归使用模板类属于嵌套使用模板类的特殊情况自己嵌套自己。对于以上代码仅需修改main函数中的调用代码即可参考如下
int main() {// Vector嵌套VectorVectorVectorstring vv; // 递归使用模板类vv[0][0] hello; vv[0][1] world; vv[0][2] !;vv[1][0] 你好; vv[1][1] 世界;vv[2][0] Happy; vv[2][1] Everyday; vv[2][2] !; vv[2][3] 吃好喝好;for(int i 0; i vv.size(); i){for(int j 0; j vv[i].size(); j) {cout vv[ i ][ j ] vv[i][j] endl;}}return 0;
}运行结果如下 vv[0][0] hello vv[0][1] world vv[0][2] ! vv[1][0] 你好 vv[1][1] 世界 vv[2][0] Happy vv[2][1] Everyday vv[2][2] ! vv[2][3] 吃好喝好 注意这跟二维数组不同。二维数组是一个矩阵第二维的大小是固定的。而vv的大小不是固定的。也可以调整一下输出格式更能直观感受vv第二维不固定的特点代码如下
int main() {// Vector嵌套VectorVectorVectorstring vv; // 递归使用模板类vv[0][0] hello; vv[0][1] world; vv[0][2] xxx;vv[1][0] 你好; vv[1][1] 世界;vv[2][0] Happy; vv[2][1] Everyday; vv[2][2] qqq; vv[2][3] 吃好喝好;for(int i 0; i vv.size(); i){for(int j 0; j vv[i].size(); j) {cout vv[i][j] ;}cout endl;}return 0;
}运行的结果如下 hello world xxx 你好 世界 Happy Everyday qqq 吃好喝好 由此可以看出vv的第一行有3个元素、第二行有2个元素、第三行有4个元素。故VectorVectorstring vv;的第二个纬度是不固定的。
感谢浏览一起学习