C++:迭代向量的向量

发布于 2024-08-12 06:39:59 字数 1948 浏览 5 评论 0 原文

嘿!我正在做这个项目,现在我正在尝试:

  1. 创建一些对象并将它们存储在向量中,这些向量存储在另一个向量中 V
  2. 迭代 V 内的向量
  3. 迭代各个向量内的对象

无论如何,我是只是在网上搜索,我遇到了 stl for_each 函数。它看起来很整洁,但我遇到了问题。我正在尝试以这种方式使用它:

for_each(V.begin(), V.end(), iterateThroughSmallVectors);

iterateThroug .... 只是对传递给它的向量执行相同的操作。

现在我收到一个奇怪的“向量迭代器不兼容”运行时错误。我看过它,但找不到任何有用的输入。

我不知道它是否有帮助,但 V 是一个私有向量<>存储在 A 类中,它有一个访问器,我试图通过执行以下操作在 B 类中迭代它:

A->getV().begin(), A->getV().end(), etc..

有人知道发生了什么吗?

编辑:好的,所以我认为最好只发布代码,以及可能出现问题的地方...

gameState.h 中的 getTiles:

vector<vector<tile*>> getTiles();

main.cpp 中的 for_each 循环:

for_each(currState->getTiles().begin(),currState->getTiles().end(), drawTiles);
.
.
void drawTiles(vector<tile*> row)
{
for_each(row.begin(), row.end(), dTile);
}
void dTile(tile *t)
{
t->draw();
}        

创建向量:

int tp = -1;
int bCounter = 0;
int wCounter = 0;
for (int i = 0; i < 8; i++)
{
vector<tile*> row(8);
    for (int j = 0; j < 8; j++)
    {
    tile *t = new tile(tp, (i+(SIDELENGTH/2))*SIDELENGTH,
        (j+(SIDELENGTH/2))*SIDELENGTH);
    row.push_back(t);
            tp *= -1;
    }
currState->setTiles(row);
    tp *= -1;
}

以防万一它可能相关:

void gameState::setTiles(vector<tile*> val)
{
    tiles.push_back(val);
}

现在是不是更容易发现问题了?我希望如此...如果您确实发现我可能在做的任何愚蠢的事情,请告诉我,我对 C++ 有点陌生,指针和引用仍然让我感到困惑。

编辑2:谢谢大家,这非常有效......对于这个问题来说很好,现在看来我在创建图块并将它们放入行向量中遇到问题......似乎即使通过向量被创建并正确传递,本来应该在其中的图块却没有(它们在 :

    for (int j = 0; j < 8; j++)
    {
    tile *t = new tile(tp, (i+(SIDELENGTH/2))*SIDELENGTH,
        (j+(SIDELENGTH/2))*SIDELENGTH);
    row.push_back(t);
            tp *= -1;
    }

循环后丢失了。如果你们中的任何人对解决这个问题有任何好主意,欢迎帮助我;)同时,我会继续努力修复它

Hey there! I'm doing this project and right now I'm trying to:

  1. create some of objects and store them in vectors, which get stored in another vector V
  2. iterate through the vectors inside V
  3. iterate through the objects inside the individual vectors

Anyway, I was just searching the web and I came accross the stl for_each function. It seems pretty neat but I'm having problems with it. I'm trying to use it in this way:

for_each(V.begin(), V.end(), iterateThroughSmallVectors);

the iterateThroug.... simply does the same on the vector passed to it..

Now I'm getting a weird "Vector iterators incompatible" runtime error. I've looked on it and can't find any useful input on this..

I don't know if it helps, but V is a private vector<> stored in class A, which has an accessor to it, and I'm trying to iterate through it in class B by doing:

A->getV().begin(), A->getV().end(), etc..

Anyone got any idea of what is going on?

EDIT: Ok, so I think it is better to just post the code, and where problems might be arrising...

getTiles in gameState.h:

vector<vector<tile*>> getTiles();

for_each loops in main.cpp:

for_each(currState->getTiles().begin(),currState->getTiles().end(), drawTiles);
.
.
void drawTiles(vector<tile*> row)
{
for_each(row.begin(), row.end(), dTile);
}
void dTile(tile *t)
{
t->draw();
}        

creating the vectors:

int tp = -1;
int bCounter = 0;
int wCounter = 0;
for (int i = 0; i < 8; i++)
{
vector<tile*> row(8);
    for (int j = 0; j < 8; j++)
    {
    tile *t = new tile(tp, (i+(SIDELENGTH/2))*SIDELENGTH,
        (j+(SIDELENGTH/2))*SIDELENGTH);
    row.push_back(t);
            tp *= -1;
    }
currState->setTiles(row);
    tp *= -1;
}

and just in case it might be relevant:

void gameState::setTiles(vector<tile*> val)
{
    tiles.push_back(val);
}

Is it easier to spot the problem now? I hope so... And if you do spot any stupid stuff I might be doing, please let me know, I'm kind of new to C++ and the pointers and references still confuse me.

EDIT2: Thanks guys, that worked perfectly... well for that problem, now it seems I have an issue with the creation of the tiles and stroing them in the row vector.. it seems that even through the vector is created and passes correctly, the tiles that were supposed to be in it aren't (they are lost after the :

    for (int j = 0; j < 8; j++)
    {
    tile *t = new tile(tp, (i+(SIDELENGTH/2))*SIDELENGTH,
        (j+(SIDELENGTH/2))*SIDELENGTH);
    row.push_back(t);
            tp *= -1;
    }

loop. If any of you has any good ideas about solving this you're welcome to help me ;) In the mean time, I'll keep trying to fix it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请持续率性 2024-08-19 06:39:59

A::getV() 的原型是什么?

我只是推测,但如果 A::getV() 不返回引用,那么它可以解释“向量迭代器不兼容”错误消息。

事实上,A->getV().begin()A->getV().end() 将是不同向量上的两个迭代器 strong>:每个 A->getV() 调用都会返回私有成员的不同副本。

希望这能帮助您调试您的问题。


编辑:看起来我的预期是正确的:编辑您提供详细信息的问题后,我可以看到您正在定义

vector; > getTiles();

因此,在以下语句中:

for_each(currState->getTiles().begin(),currState->getTiles().end(), drawTiles);< /code>

正如上面所预期的,每次调用 getTiles() 都会返回成员向量的一个单独的临时副本。因此,从 begin()end() 返回的迭代器来自不同的向量,因此您在运行时会遇到错误消息。

另外,正如查尔斯在他的详细回答中指出的 ,这些临时向量将在到达 for_each 函数体时被销毁。

考虑通过 const 引用返回向量,如下所示:

const vector >& getTiles() const;

您也可以更改 drawTiles 以避免更多副本:

void drawTiles(const vector& row)

What's the prototype for A::getV()?

I'm only speculating but if A::getV() doesn't return a reference then it can explain the "Vector iterators are incompatible" error message.

Indeed A->getV().begin() and A->getV().end() would be two iterators over different vectors: each A->getV() invocation returning a different copy of the private member.

Hope this will help you debug your problem.


EDIT: it looks like I anticipated it right: after editing your question providing details, I can see you're defining

vector<vector<tile*> > getTiles();

As a consequence, in the following statement:

for_each(currState->getTiles().begin(),currState->getTiles().end(), drawTiles);

As anticipated above, each call to getTiles() will return a separate temporary copy of the member vector. As a consequence the iterators returned from begin() and end() come from different vectors, hence the error message you're facing at runtime.

Also, as pointed by Charles in his detailed answer, these temporary vectors will be destroyed by the time the function body of for_each is reached.

Consider returning the vector by const reference like this:

const vector<vector<tile*> >& getTiles() const;

And you may as well change drawTiles to avoid even more copies:

void drawTiles(const vector<tile*>& row)

寄风 2024-08-19 06:39:59

我所做的是:直接的方式

vector<vector<int> > vvi;
vector<vector<int> >::iterator vvi_iterator;
vector<int>::iterator vi_iterator;

for(vvi_terator = vvi.begin();vvi_iterator!=vvi.end();++vvi_iterator) {
    for(vi_iterator = (*vvi_iterator).begin();vi_iterator!=(*vvi_iterator).end();++vi _iterator) {
     cout<<*vi_iterator<<" ";
    }  
}

这是粗略的想法。我发现 for_each 方法对于仅执行双循环来说很麻烦。当您想要真正对每个元素进行一些计算(例如每个元素的某种映射)时,for_each 非常有用

What i do is this: the direct way

vector<vector<int> > vvi;
vector<vector<int> >::iterator vvi_iterator;
vector<int>::iterator vi_iterator;

for(vvi_terator = vvi.begin();vvi_iterator!=vvi.end();++vvi_iterator) {
    for(vi_iterator = (*vvi_iterator).begin();vi_iterator!=(*vvi_iterator).end();++vi _iterator) {
     cout<<*vi_iterator<<" ";
    }  
}

This is the rough idea. I find the for_each method cumbersome for just doing a double-loop. for_each is useful when you want to really do some computation on each element (like some kind of mapping for each element)

只是一片海 2024-08-19 06:39:59

你有几个严重的错误,但首先是一个小错误。

vector<vector<tile*>> getTiles();

在下一个标准出现之前,您需要在 > 之间留一个空格。

vector< vector<tile*> > getTiles();

此函数按值返回一个向量,这意味着它会创建传递给函数中 return 语句的任何向量的新副本。 (我假设这个函数声明是 curState 的实例。)

当您这样做时:

for_each(currState->getTiles().begin(),currState->getTiles().end(), drawTiles);

每次调用 getTiles 将返回一个向量的单独临时副本。这不仅意味着 begin()end() 的迭代器来自不同的向量,而且当 的函数体被调用时,这些向量将被销毁>for_each 已达到。

看起来您需要研究引用并通过引用传递,因为您需要先了解这些,然后才能在这些场景中正确使用 std::for_each

You have a couple of serious errors, but first a minor one.

vector<vector<tile*>> getTiles();

Until the next standard comes out you need a space between the to >.

vector< vector<tile*> > getTiles();

This function returns a vector by value which means that it creates a new copy of whatever vector is passed to the return statement in the function. (I assume that this function declaration is the whatever class curState is an instance of.)

When you then do:

for_each(currState->getTiles().begin(),currState->getTiles().end(), drawTiles);

Each call to getTiles will return a separate temporary copy of a vector. Not only does this mean that your iterators from begin() and end() come from difference vectors, but the vectors will be destroyed by the time the function body of for_each is reached.

It looks like you need to research references and pass by reference, because you need to understand these before you can correctly use std::for_each in these scenarios.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文