简体   繁体   English

c++ 中的双指针向量

[英]Double pointers vector in c++

everyone.每个人。 I was doing my home task and faced some confusing stuff with vectors in c++.我正在做我的家庭任务,并在 c++ 中遇到了一些令人困惑的问题。 I've got a vector of double pointers and I'm filling it in a loop.我有一个双指针向量,我正在循环填充它。 The problem is that each iteration the value of all members of vector changes somehow.问题是每次迭代向量的所有成员的值都会以某种方式发生变化。


每次迭代的向量内容 . .


I don't understand why it's happening what I'm doing wrong and how It should be done the proper way.我不明白为什么会发生我做错的事情以及应该如何以正确的方式完成。 This things are happening in this part of code:在这部分代码中发生了这些事情:

std::queue<TreeNode*> vertexQueue;
std::vector<TreeNode**> vertexVector;
TreeNode* tmp;
vertexQueue.push(Tree);

while (!vertexQueue.empty()) {
    tmp = vertexQueue.front();
    vertexQueue.pop();
    vertexVector.emplace_back(&tmp);

    //this loop is for the output
    for (int i = 0; i < vertexVector.size(); i++) {
        std::cout << (*vertexVector[i])->info.weigth << ' ';
    }
    std::cout << std::endl;

    if (!isLeave(tmp)) {
        vertexQueue.push(tmp->leftBranch);
        vertexQueue.push(tmp->rigthBranch);
    }
}

Pointer tmp is declared outside of while loop.指针tmpwhile循环之外声明。

In vertexVector.emplace_back(&tmp) you add pointer containing address of tmp into vector which is always the same.vertexVector.emplace_back(&tmp)中,您将包含tmp地址的指针添加到始终相同的向量中。 In each iteration, you change only value of tmp what results in printing value under pointer previously assigned to tmp .在每次迭代中,您只更改tmp的值,这会导致在先前分配给tmp的指针下打印值。

I want to understand why double pointer vector works in such way in my program.我想了解为什么双指针向量在我的程序中以这种方式工作。

Then draw on paper:然后在纸上画:

vector: [pointer]  [pointer]  [pointer]
            |          |          |
             --------  |  --------
                     | | |
                     V V V
                    [ tmp ]
                       |
                       |
                       V
                  <tree node>

Now what happens if you change the value of tmp?现在如果你改变 tmp 的值会发生什么?

vector: [pointer]  [pointer]  [pointer]
            |          |          |
             --------  |  --------
                     | | |
                     V V V
                    [ tmp ]
                       |
                        ------------
                                    |
                                    V
                 <tree node>  <another node>

Correct solution is not using double, but single pointers:正确的解决方案不是使用双指针,而是使用单指针:

vector:           [pointer]
         (copies) /   |
            [ tmp ]   |
               |      |
               -----  |
                    | |
                    V V
              <tree node>

And one step later:一步之后:

vector:           [pointer]        [pointer]
                      |   (copies) / |
                      |     [ tmp ]  |
                      |        |     |
                      |        ----  |
                      |            | |
                      V            V V
              <tree node>     <another node>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM