C++ c++;矢量填充不正确

C++ c++;矢量填充不正确,c++,vector,C++,Vector,我正在制作一个引擎,它应该能够读取格式化的文本文件,并将它们作为基于文本的冒险输出。世界正在被写入一个向量矩阵。然而,我的程序似乎只在一维中填充矩阵,并且只使用矩阵第一个单元格中的信息 WorldReader读取World文件并返回指定行: std::string WorldReader(std::string file,int line) { std::string out[n]; int i = 0; World.open(file + "World.txt");

我正在制作一个引擎,它应该能够读取格式化的文本文件,并将它们作为基于文本的冒险输出。世界正在被写入一个向量矩阵。然而,我的程序似乎只在一维中填充矩阵,并且只使用矩阵第一个单元格中的信息

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];
}
std::string世界读取器(std::string文件,int行)
{
std::字符串输出[n];
int i=0;
打开(文件+“World.txt”);
如果(!World.good())
返回“坏文件”;
除此之外(i x;
stringstream Coordy(WorldReader(loc,5+j*10));
协调>>y;
std::string Desc=WorldReader(loc,6+j*10);
W1.写单元(x,y,0,描述);
}
下面是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);
    }
}
std::向量值;
std::向量wH;
标准向量<标准向量>网格;
void World::writeCell(int-writelocW、int-writelocH、int-ValLoc、std::string输入)
{
if(wH.size()>writelocH)
{
Value.insert(Value.begin()+ValLoc,1,输入);
插入(wH.begin()+writelocH,1,Value);
grid.insert(grid.begin()+writelocW,1,wH);
}
其他的
{
insert(wH.begin(),1,Value);
grid.insert(grid.begin(),1,wH);
}
}
此外,矩阵变得非常膨胀,即使我将其调整为3x3

感谢您的提示和帮助。

好的。我想我知道您的问题在哪里。请注意,如果没有真正可运行的代码,这是非常难以分析的。最重要的是:您正在为您处理到
网格中的每个值插入一个新的2D矩阵,我希望能够清楚为什么会出现这种情况。它解释了这一点你正在经历的大量膨胀(和不准确的数据)

您的原始代码

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;
你需要从最重要到最不重要的顺序展开维度,从
网格开始
。最终这就是它的访问方式。我个人会使用稀疏的std::map系列来实现这一点,因为空间利用率会更高,但我们正在使用你所拥有的。我在不使用c的情况下即兴编写这篇文章让我检查一下错误,给我一点自由


建议的解决方案

毫无疑问,这是世界级的精简版。我已将参数的名称更改为传统的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;
    }
};
显然,这将从根本上改变您使用容器的方式,因为您必须意识到未使用的“跳过”索引,并且在使用所用维度的适当迭代器枚举时会检测到这一点。无论如何,您的存储效率会更高


无论如何,我希望这至少有一点帮助。

您只提供了代码片段,因此很难判断这里发生了什么。尝试创建一个。我不想让它太长,您还需要看什么?您是对的,不要发布比这更多的代码。相反,请尝试将您的问题简化为一个简短、自包含的示例这说明了具体的问题。然后将其发布!使其更简短、更完整。自我包含、简短、编译、编辑;问题现在可以理解了吗?@user1931745别忘了,通过操作符[]访问地图将默认在索引位置构造项,因此您应该使用迭代器遍历映射;而不是索引循环。否则您也可以使用第一个解决方案。我希望我已经讲清楚了。非常高兴能够提供帮助!
writeocW * writelocH * ValLoc
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;
void writeCell(size_t x, size_t y, size_t z, const std::string& val)
{
    grid[x][y][z] = val;
}