简体   繁体   中英

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.

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. and again in the next round we have 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 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)

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.

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.

You're storing &first and &second, which become invalid pointers when the current iteration ends. If you dereference them after that, your program has undefined behaviour. @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:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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