我正在制作一个引擎,该引擎应该读取格式化的文本文件并将其输出为基于文本的冒险。世界被写入 vector 矩阵。但是,我的程序似乎仅在一维中填充矩阵,并且仅使用矩阵第一个单元格的信息。
WorldReader读取World文件并返回指定行:
std::string WorldReader(std::string file,int line)
{
std::string out[n];
int i = 0;
World.open(file + "World.txt");
if(!World.good())
return "Bad File";
else while(i<n && getline(World, out[i]))
{
i++;
}
World.close();
return out[line];
}
这是写循环:
for(j=0; j<(width*height); j++)
{
int x;
int y;
stringstream Coordx(WorldReader(loc, 4+j*10));
Coordx >> x;
stringstream Coordy(WorldReader(loc, 5+j*10));
Coordy >> y;
std::string Desc = WorldReader(loc, 6+j*10);
W1.writeCell(x,y,0,Desc);
}
这是 writeCell 函数:
std::vector<std::string> Value;
std::vector<std::vector<std::string> > wH;
std::vector< std::vector<std::vector<std::string> > > grid;
void World::writeCell(int writelocW, int writelocH, int ValLoc, std::string input)
{
if (wH.size() > writelocH)
{
Value.insert(Value.begin()+ValLoc,1,input);
wH.insert(wH.begin() + writelocH,1,Value);
grid.insert(grid.begin() + writelocW,1,wH);
}
else
{
wH.insert(wH.begin(),1,Value);
grid.insert(grid.begin(),1,wH);
}
}
即使我将其大小调整为 3x3,矩阵也变得非常膨胀。
感谢提示和帮助。
最佳答案
好的。我认为我知道你的问题出在哪里。请注意,如果没有真正可运行的代码,分析起来非常困难。最重要的是:您要为您处理到网格
中的每个值插入一个新的二维矩阵,我希望它是清楚为什么会这样。它解释了您所经历的大规模膨胀(和不准确的数据)。
您的原始代码
void World::writeCell(int writelocW, int writelocH, int ValLoc, std::string input)
{
if (wH.size() > writelocH)
{
// inserts "input" into the Value member.
Value.insert(Value.begin()+ValLoc,1,input);
// inserts a **copy** of Value into wH
wH.insert(wH.begin() + writelocH,1,Value);
// inserts a **copy** of wH into the grid.
grid.insert(grid.begin() + writelocW,1,wH);
}
else
{ // inserts a **copy** of Value into wH
wH.insert(wH.begin(),1,Value);
// inserts a **copy** of wH into the grid.
grid.insert(grid.begin(),1,wH);
}
}
正如你所看到的。这里发生了大量无意识的复制。您有三个变量,每个变量都是独立的。
std::vector<std::string> Value;
std::vector<std::vector<std::string> > wH;
std::vector< std::vector<std::vector<std::string> > > grid;
在 writeCell
过程中,您尝试将字符串插入 3D 位置,但最多仅“取消引用”其中一个维度。节日拷贝随之而来
根据您的变量名称,我假设您的网格维度基于:
writeocW * writelocH * ValLoc
您需要按照从高到低的顺序展开维度,从网格
开始。最终这就是它的访问方式。我个人会为此使用稀疏的 std::map<> 系列,因为空间利用率会更加更有效,但我们正在使用您所拥有的。我是即兴编写的,附近没有编译器来检查错误,所以请给我一点自由度。
建议的解决方案
这是您毫无疑问拥有的世界级的精简版。我已将参数名称更改为传统 3D 坐标 (x,y,z),以便清楚地说明如何执行我认为您想要的操作:
class World
{
public:
typedef std::vector<std::string> ValueRow;
typedef std::vector<ValueRow> ValueTable;
typedef std::vector<ValueTable> ValueGrid;
ValueGrid grid;
// code omitted to get to your writeCell()
void writeCell(size_t x, size_t y, size_t z, const std::string& val)
{
// resize grid to hold enough tables if we would
// otherwise be out of range.
if (grid.size() < (x+1))
grid.resize(x+1);
// get referenced table, then do the same as above,
// this time making appropriate space for rows.
ValueTable& table = grid[x];
if (table.size() < (y+1))
table.resize(y+1);
// get referenced row, then once again, just as above
// make space if needed to reach the requested value
ValueRow& row = table[y];
if (row.size() < (z+1))
row.resize(z+1);
// and finally. store the value.
row[z] = val;
}
};
我认为这会让你到达你想要的地方。请注意,使用大坐标可以快速增大此立方体。
替代解决方案
如果由我决定,我会使用这样的东西:
typedef std::map<size_t, std::string> ValueMap;
typedef std::map<size_t, ValueMap> ValueRowMap;
typedef std::map<size_t, ValueRowMap> ValueGridMap;
ValueGridMap grid;
由于您在使用此网格执行任何操作时都会枚举这些内容,因此键的顺序(基于 0 的索引)很重要,因此使用 std::map
而不是 std::unordered_map
。 std::map
的 operator[]()
访问器有一个非常很好的功能:它会添加引用的键槽,如果它还不存在。因此,您的 writeCell 函数将折叠为:
void writeCell(size_t x, size_t y, size_t z, const std::string& val)
{
grid[x][y][z] = val;
}
显然,这会从根本上改变您使用容器的方式,因为您必须意识到您没有使用的“跳过”索引,并且您会在使用维度的适当迭代器进行枚举时检测到这一点) 正在使用。无论如何,您的存储效率将会大大提高。
无论如何,我希望这至少有一点帮助。
关于C++ vector 未正确填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14102536/