C++ C++;指针和对象初始化
我正在设计一个如下所示的数据结构: 我使用重定向输入来获取填充数据结构所需的数据。 这是代码。我知道这很费劲,但重要的东西是接近底部的代码C++ C++;指针和对象初始化,c++,arrays,templates,pointers,data-structures,C++,Arrays,Templates,Pointers,Data Structures,我正在设计一个如下所示的数据结构: 我使用重定向输入来获取填充数据结构所需的数据。 这是代码。我知道这很费劲,但重要的东西是接近底部的代码 #include <iostream> using namespace std; template <class DT> class Cell { protected: DT* info; Cell<DT>* right; public: Cell(); Cell(const DT&am
#include <iostream>
using namespace std;
template <class DT>
class Cell {
protected:
DT* info;
Cell<DT>* right;
public:
Cell();
Cell(const DT& info);
Cell(const DT& info, Cell<DT>* next);
Cell(const Cell<DT>& copy);
void copy(const Cell<DT>& copy);
~Cell();
void operator=(const Cell<DT>& check);
bool isEmpty();
DT getInfo();
Cell<DT>* next();
int size();
DT& find(const DT& key);
DT& infoAt(int position);
void add(const DT& object);
void insertAt(const DT& newObj, int position);
Cell<DT>* setNext(Cell<DT>* next);
DT remove();
DT removeAt(int position);
};
template <class DT>
Cell<DT>::Cell()
{
info = NULL;
}
template <class DT>
Cell<DT>::Cell(const DT& info) {
this->info = new DT(info);
right = NULL;
}
template <class DT>
Cell<DT>::Cell(const DT& info, Cell<DT>* next) {
this->info = new DT(info);
right = next;
}
template <class DT>
Cell<DT>::~Cell() {
if (info != NULL) {
delete info;
info = NULL;
}
if (right != NULL) {
delete right;
right = NULL;
}
}
template <class DT>
void Cell<DT>::copy(const Cell<DT>& copy) {
if (copy.info == NULL) info = NULL;
else info = new DT(*(copy.info));
if (copy.right == NULL) right = NULL;
else right = new Cell<DT>(*(copy.right));
}
template <class DT>
Cell<DT>::Cell(const Cell<DT>& copy) {
this->copy(copy);
}
template <class DT>
void Cell<DT>::operator=(const Cell<DT>& check) {
if (info != NULL) delete info;
if (right != NULL) delete right;
copy(check);
}
template <class DT>
bool Cell<DT>::isEmpty() {
return (info == NULL);
}
template <class DT>
DT Cell<DT>::getInfo() {
return *info;
}
template <class DT>
Cell<DT>* Cell<DT>::next() {
return right;
}
template <class DT>
int Cell<DT>::size() {
if (next == NULL) {
if (info == NULL) return 0;
else return 1;
}
else return 1 + right->size();
}
template <class DT>
DT& Cell<DT>::find(const DT& key) {
if (key == *info) return *info;
return right->find(key);
}
template <class DT>
DT& Cell<DT>::infoAt(int position) {
if (isEmpty()) return NULL;
if (position == 0) return *info;
if (right == NULL) return NULL;
return right->infoAt(position - 1);
}
template <class DT>
void Cell<DT>::add(const DT& object) {
if (info == NULL) info = new DT(object);
else {
Cell<DT>* newList = new Cell<DT>(*info, right);
*info = object;
right = newList;
}
}
template <class DT>
void Cell<DT>::insertAt(const DT& newObj, int position) {
if (position == 0) add(newObj);
else {
if (right == NULL) right = new Cell<DT>(newObj);
else right->insertAt(newObj, position - 1);
}
}
template <class DT>
Cell<DT>* Cell<DT>::setNext(Cell<DT>* next) {
Cell<DT>* temp = this->right;
this->right = (Cell<DT>*) next;
return temp;
}
template <class DT>
DT Cell<DT>::remove() {
DT temp = *info;
delete info;
if (right == NULL) info = NULL;
else {
Cell<DT>* oldnext = right;
info = right->info;
right = right->right;
oldnext->info = NULL;
oldnext->next = NULL
delete oldnext;
}
return temp;
}
template <class DT>
DT Cell<DT>::removeAt(int position) {
if (position == 0) return remove();
return right->removeAt(position - 1);
}
template <class DT1, class DT2>
class CellNode {
protected:
DT1* info;
Cell<DT2>* myCell;
public:
CellNode();
CellNode(DT1 title, DT2* info);
CellNode(DT1 title);
CellNode(DT2* info);
};
template <class DT1, class DT2>
CellNode<DT1, DT2>::CellNode() {
info = new DT1();
myCell = new Cell<DT2>();
}
template <class DT1, class DT2>
CellNode<DT1, DT2>::CellNode(DT1 title, DT2* info) {
this->info = new DT1(title);
myCell = new Cell<DT2>(info[0]);
for (int i = 1; info[i] != NULL; i++) myCell->add(info[i]);
}
template <class DT1, class DT2>
CellNode<DT1, DT2>::CellNode(DT1 title) {
this->info = new DT1(title);
myCell = new Cell<DT2>();
}
template <class DT1, class DT2>
CellNode<DT1, DT2>::CellNode(DT2* info) {
this->info = new DT1();
if (myCell == NULL) {
myCell = new Cell<DT2>(info[0]);
}
for (int i = 0; info[i] != NULL; i++) myCell->add(info[i]);
}
template <class DT1, class DT2>
class MasterCell {
protected:
CellNode<DT1, DT2>* myCellNodes;
int cellsFilled;
public:
MasterCell();
MasterCell(int size);
void add(DT1 title, DT2* info);
};
template<class DT1, class DT2>
MasterCell<DT1, DT2>::MasterCell() {
//This can't do anything.
}
template<class DT1, class DT2>
MasterCell<DT1, DT2>::MasterCell(int size) {
myCellNodes = new CellNode<DT1, DT2>[size];
cellsFilled = 0;
}
template<class DT1, class DT2>
void MasterCell<DT1, DT2>::add(DT1 title, DT2* info) {
myCellNodes[0] = new CellNode<DT1, DT2>(title, info);
}
int main() {
MasterCell<char, char*>* master;
char * check = new char[100000];
char* buffer;
int size = 0;
int lineAmount = 0;
int cellCounter = 0;
while (!cin.eof()) {
cin.get(check[size]);
size++;
}
size;
buffer = new char[size - 1];
for (int i = 0; i < size - 1; i++) {
buffer[i] = check[i];
if (buffer[i] == '\n') lineAmount++;
}
delete[] check;
lineAmount++;
cout << size << endl;
for (int i = 0; i < size - 1; i++) {
cout << buffer[i];
}
master = new MasterCell<char, char*>(lineAmount);
cout << endl << lineAmount << endl;
char* test = new char[5];
test = "hello";
char** test2 = new char*[2];
test2[0] = new char[5];
test2[0] = "hello";
test2[1] = new char[5];
test2[1] = "world";
master->add((*test), test2);
return 0;
}
#包括
使用名称空间std;
模板
类单元{
受保护的:
DT*信息;
单元格*对;
公众:
细胞();
单元(常数DT和信息);
单元格(常量DT和信息,单元格*下一个);
单元格(常量单元格和副本);
无效副本(常量单元格和副本);
~Cell();
void运算符=(常量单元格和检查);
bool是空的();
DT getInfo();
单元格*next();
int size();
DT&find(常数DT&key);
DT&infoAt(内部位置);
无效添加(常量DT和对象);
无效插入(常量DT和newObj,内部位置);
单元格*设置下一步(单元格*下一步);
DT-remove();
DT removeAt(内部位置);
};
模板
Cell::Cell()
{
info=NULL;
}
模板
单元::单元(常数DT和信息){
此->信息=新的DT(信息);
右=空;
}
模板
单元格::单元格(常量DT和信息,单元格*下一个){
此->信息=新的DT(信息);
右=下一个;
}
模板
单元格::~Cell(){
如果(信息!=NULL){
删除信息;
info=NULL;
}
if(右!=NULL){
删除权利;
右=空;
}
}
模板
空单元格::复制(常量单元格和复制){
如果(copy.info==NULL)info=NULL;
else info=新的DT(*(copy.info));
如果(copy.right==NULL)right=NULL;
else right=新单元格(*(copy.right));
}
模板
单元格::单元格(常量单元格和副本){
此->复制(复制);
}
模板
空单元格::运算符=(常量单元格和检查){
如果(info!=NULL)删除信息;
如果(right!=NULL),则删除right;
复印件(支票);
}
模板
布尔单元格::isEmpty(){
返回(info==NULL);
}
模板
DT Cell::getInfo(){
返回*信息;
}
模板
Cell*Cell::next(){
返还权;
}
模板
int单元格::大小(){
if(next==NULL){
如果(info==NULL)返回0;
否则返回1;
}
否则返回1+右->大小();
}
模板
DT&Cell::find(常量DT&key){
如果(键==*info)返回*info;
返回右键->查找(键);
}
模板
DT&Cell::infoAt(int位置){
if(isEmpty())返回NULL;
如果(位置==0)返回*信息;
if(right==NULL)返回NULL;
返回右->信息站(位置-1);
}
模板
空单元格::添加(常量DT和对象){
如果(info==NULL)info=new DT(对象);
否则{
单元格*新列表=新单元格(*信息,右);
*信息=对象;
右=新列表;
}
}
模板
空单元格::插入(常量DT和newObj,int位置){
如果(位置==0)添加(newObj);
否则{
if(right==NULL)right=newcell(newObj);
else right->insertAt(newObj,位置-1);
}
}
模板
单元格*单元格::设置下一步(单元格*下一步){
单元格*温度=此->右侧;
此->右=(单元格*)下一步;
返回温度;
}
模板
DT单元::删除(){
DT温度=*信息;
删除信息;
如果(right==NULL)info=NULL;
否则{
单元格*oldnext=右;
信息=右->信息;
右=右->右;
oldnext->info=NULL;
oldnext->next=NULL
删除下一步;
}
返回温度;
}
模板
DT单元::移除(内部位置){
如果(位置==0)返回remove();
返回右侧->移除(位置-1);
}
模板
类单元节点{
受保护的:
DT1*信息;
细胞*菌丝体;
公众:
CellNode();
CellNode(DT1标题、DT2*信息);
CellNode(DT1标题);
小区节点(DT2*info);
};
模板
CellNode::CellNode(){
信息=新的DT1();
迈塞尔=新细胞();
}
模板
CellNode::CellNode(DT1标题,DT2*信息){
此->信息=新DT1(标题);
迈塞尔=新单元(信息[0]);
对于(inti=1;info[i]!=NULL;i++)myCell->add(info[i]);
}
模板
CellNode::CellNode(DT1标题){
此->信息=新DT1(标题);
迈塞尔=新细胞();
}
模板
CellNode::CellNode(DT2*信息){
此->信息=新DT1();
如果(myCell==NULL){
迈塞尔=新单元(信息[0]);
}
对于(inti=0;info[i]!=NULL;i++)myCell->add(info[i]);
}
模板
类主细胞{
受保护的:
细胞节*菌丝节;
内部单元格填充;
公众:
母细胞();
主单元格(整数大小);
无效添加(DT1标题、DT2*信息);
};
模板
MasterCell::MasterCell(){
//这没什么用。
}
模板
MasterCell::MasterCell(整数大小){
myCellNodes=新的CellNode[大小];
cellsFilled=0;
}
模板
void MasterCell::add(DT1标题、DT2*信息){
myCellNodes[0]=新的CellNode(标题、信息);
}
int main(){
MasterCell*master;
字符*检查=新字符[100000];
字符*缓冲区;
int size=0;
int-lineAmount=0;
int-cellCounter=0;
而(!cin.eof()){
cin.get(检查[尺寸]);
大小++;
}
大小;
缓冲区=新字符[大小-1];
对于(int i=0;i cout您可能希望保留指针的动态数组:
CellNode*myCellNodes;
应该是:
CellNode<DT1, DT2>** myCellNodes;
CellNode**myCellNodes;
及
myCellNodes=newcellnode[size];
应该是
myCellNodes = new CellNode<DT1, DT2> *[size];
myCellNodes=newcellnode*[size];
有人能在这里解释一下解决方案吗?我相信这可能是一个类似于我所需要的解决方案,但我很难理解其中的一些代码。
myCellNodes = new CellNode<DT1, DT2>[size];
myCellNodes = new CellNode<DT1, DT2> *[size];