gpt4 book ai didi

c++ vector 未正确填充

转载 作者:搜寻专家 更新时间:2023-10-31 01:51:09 24 4
gpt4 key购买 nike

我正在制作一个引擎,该引擎应该读取格式化的文本文件并将它们输出为基于文本的冒险。世界正在被写入 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

您需要以从高到低的顺序展开维度,从 grid 开始。最终这就是访问它的方式。我个人会为此使用稀疏的 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_mapstd::mapoperator[]() 访问器有一个非常不错的特性:它添加引用的键槽如果它尚不存在。因此,您的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com