简体   繁体   English

C++ 向量 push_back 没有复制?

[英]C++ vector push_back not making a copy?

vector<minHeap> letters;
letters = countLetters(text);

while (letters.size() >= 2) {
    minHeap first = popMin(&letters);
    minHeap second = popMin(&letters);
    minHeap third('$');

    third.count = first.count + second.count;
    third.left = &first;
    third.right = &second;
    letters.push_back(third);
}

I tried to debug the code.我试图调试代码。 The 'first', 'second' and 'third' values are working correctly. “第一”、“第二”和“第三”值工作正常。 My problem is: When I push the 'third' into the vector, in the next round of the loop, the object that we pushed in the last round is changed to the new 'third' and is same with the 'third' in the new round.我的问题是:当我将“第三”推入向量时,在下一轮循环中,我们在上一轮推入的 object 更改为新的“第三”,并且与“第三”相同新一轮。

For example in the first round X is pushed into the vector, In the next round Y is pushed but the problem is it changes the X to Y too, so we have 2 Ys now.例如在第一轮 X 被推入向量中,在下一轮 Y 被推入,但问题是它也将 X 更改为 Y,所以我们现在有 2 个 Y。 and again in the next round we have 3 Z.在下一轮中,我们再次获得 3 Z。

Like it's not creating a copy of the 'third', so when I change it, it changes the previous objects too.就像它没有创建“第三个”的副本一样,所以当我更改它时,它也会更改以前的对象。

What am I doing wrong?我究竟做错了什么? :s :秒

Edit:编辑:

minHeap class:最小堆 class:

class minHeap {
public:
    char letter;
    int count = 1;
    minHeap* left = NULL;
    minHeap* right = NULL;

    minHeap(char letter) {
        this->letter = letter;
    }
};

popMin: (Finds the minimum 'count' in the minHeaps and destroys it, then returns a copy of it) popMin:(在 minHeaps 中找到最小的 'count' 并销毁它,然后返回它的副本)

minHeap popMin(vector<minHeap>* letters) {
    int index = 0;
    for (size_t i = 0; i < letters->size(); i++) {
        if (letters->at(i).count < letters->at(index).count)
            index = i;
    }
    minHeap res = letters->at(index);
    letters->erase(letters->begin() + index);
    return res;
}

countLetters: (Counts letters and creates a minHeap object for each, increases the 'count' (freq) if it's created before. countLetters:(计算字母并为每个字母创建一个 minHeap object,如果之前创建,则增加“计数”(频率)。

vector<minHeap> countLetters(string text) {
    vector<minHeap> letters; // Stores all the characters with their count
    for (size_t i = 0; i < text.length(); i++) {
        bool found = false;
        for (size_t j = 0; j < letters.size(); j++) {
            if (letters.at(j).letter == text[i]) {
                letters.at(j).count++; // Character exists in list, increase its count by one
                found = true;
                break;
            }
        }
        if (!found) { // Character doesn't exist in list, create it and push it to the list
            minHeap letter(text[i]);
            letters.push_back(letter);
        }
    }

    return letters;
}

In General: I'm trying to do the huffman coding using minHeap.一般来说:我正在尝试使用 minHeap 进行霍夫曼编码。

You're storing &first and &second, which become invalid pointers when the current iteration ends.您正在存储 &first 和 &second,当当前迭代结束时,它们成为无效指针。 If you dereference them after that, your program has undefined behaviour.如果在此之后取消引用它们,则您的程序具有未定义的行为。 @molbdnilo @molbdnilo

So I changed my code to:所以我将代码更改为:

vector<minHeap> letters;
    letters = countLetters(text);

    
    while (letters.size() >= 2) {
        minHeap* first = new minHeap('-');
        *first = popMin(&letters);

        minHeap* second = new minHeap('-');
        *second = popMin(&letters);

        minHeap third('$');

        third.count = first->count + second->count;
        third.left = first;
        third.right = second;
        letters.push_back(third);
    }

And it's fixed.它是固定的。 Thanks:)谢谢:)

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

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