类对象指针C 的2D向量
2D Vector of Class Object Pointers C++
我正在尝试创建类对象指针的动态2D向量。我正在尝试为基于文本的游戏制作随机地图。我知道那里有解决方案,但我想手工制造这只小狗。我只是..吮吸指针。
我试图创建一个2D的类指针,但是语法很难遵循。我真的不知道从哪里开始。我的最后一个C 课是一年前,我们只是短暂地进入了媒介。
我试图自己进行一些研究,但是我似乎无法将这些概念融合在一起。我已经可以参考以下帖子/页面:
对象指针的向量,一般帮助和混乱
http://www.cplusplus.com/doc/tutorial/pointers/
类指针初始化的向量
https://www.geeksforgeeks.org/2d-vector-in-cpp-with-user-defined-size/
现在,我正在踏上我的计划的第一步。我知道,一旦我将2D矢量语法降低,其余的就位。但是,这不是我以前做过的事情。我敢肯定,我的编码大多数人都没有完全满足鼻烟,但是已经有一段时间了...
如果我可以要求澄清以下内容,我认为这对我来说是巨大的帮助。
1。您如何通过参考将2D向量传递给函数,而语法是什么是操纵正确的指针?
2。您如何访问2D向量中指针的类成员功能?
3。您如何动态创建2D向量中指向的类对象?
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <bits/stdc++.h>
using namespace std;
enum Dir{n, e, s, w};
class Object
{
private:
string objName;
public:
void setName(string n)
{
objName=n;
}
string getName() const
{
return objName;
}
Object()
{
objName="";
}
Object(string n)
{
objName=n;
}
virtual ~Object()
{
cout << "The " << objName << " was destroyed.n";
}
};
class Room : public Object
{
private:
Room *north, *east, *south, *west;
public:
void setDir(Room *d, Dir a)
{
switch(a)
{
case n: north=d; break;
case e: east=d; break;
case s: south=d; break;
case w: west=d; break;
}
}
Room *getDir(Dir a)
{
switch(a)
{
case n: return north; break;
case e: return east; break;
case s: return south; break;
case w: return west; break;
}
}
Room(){}
Room(string rName, Room *n, Room *e, Room *s, Room *w) : Object(rName)
{
north=n;
east=e;
south=s;
west=w;
}
};
Room Wall;
void RoomRandomizer(vector<Room *> map, string rName)
{
int x=0, y=0, entX=0, extY=0;
bool entFound = false;
Room * tempRoom;
string rN = rName;
srand(time(NULL));
if(rName == "Entrance")
{
x=rand() % 7+1;
y=rand() % 5;
tempRoom = new Room(rName, &Wall, &Wall, &Wall, &Wall);
map[x][y]= tempRoom;
}
};
int main(){
int row=9, colom[]={9,9,9,9,9,9,9,9,9};
Wall.setName("Wall");
vector<vector<Room *>> map(row);
for (int i = 0; i < row; i++) {
// size of column
int col;
col = colom[i];
// declare the i-th row to size of column
map[i] = vector<Room *>(col);
//map.resize(9, vector<Room *>(9, 0));
}
map[0][0] = new Room("Entrance", Wall, Wall, Wall, Wall);
cout << map[0][0]->getName;
return 0;
}
这是一个简短的代码示例。
// Passing in the vector of vectors by reference
void function(std::vector<std::vector<Room*>>& referece_to_2d_vector) {
// Call member function
Room* north = referece_to_2d_vector[0][0]->getDir(n);
// Just like you already do in main, dynamically allocating a new
// room and make one of the pointers point to it
referece_to_2d_vector[0][0] = new Room("New room", &Wall, &Wall, &Wall, &Wall);
// Is this what you mean when you say "manipulate the pointers held within" ?
referece_to_2d_vector[0][1] = north;
}
想到的第一件事是,如果可能的话,您应该尝试避免使用new
。您应该考虑使用std::unique_ptr
和std::make_unique
而不是向量中的原始指针。
它基本上是一个指针,也拥有它指向的对象,因此,当指针被破坏时,您无需手动delete
。
std::vector<std::vector<std::unique_ptr<Room>>> map(1);
map[0].push_back( std::make_unique<Room>("Room1", &Wall, &Wall, &Wall, &Wall) );
map[0].push_back( std::make_unique<Room>("Room2", &Wall, &Wall, &Wall, &Wall) );
// You can get a raw pointer to the object owned by a `std::unique_ptr` with it's `get` function.
map[0][0]->setDir( map[0][1].get(), n);
- 在 2D 向量中使用第三个 [ ] 有什么意义?
- 如何在 C++ 中使用它的构造函数初始化 unique_ptrs 的 2D 向量?
- 哪个更快:在 1d 向量中按字符串搜索还是在 2d 向量中按向量搜索?
- 分配具有 2D 向量大小的变量的内存
- 在统一内存 CUDA C/C++ 中分配 2D 向量
- 索引 2D 向量时'Reference to type const..'编译错误
- 查找 2D 向量的位置,该向量是类C++的一个元素
- 如何在 C++ 中的函数中传递 2D 向量的子向量
- 为 2D 向量类创建标量乘法运算符
- 无法使用push_back将整数插入1D/2D向量
- 如何将列附加到现代C 中的2D向量
- parallel_for lambda 函数填充 2D 向量后出错
- 类对象指针C 的2D向量
- 将1D或2D向量传递给同一类的构造函数
- 如何初始化2D向量C
- 在类的构造函数中初始化 2D 向量
- 对通过引用传递的 2d 向量进行排序
- 如何在C 2D向量中找到对象(而不是int)的位置
- 如何在C 中打印一个对象的2D向量
- 双打分类/编辑问题的2D向量