dimanche 30 août 2020

deque unable to insert after erase

Why does this not work as expected?

Output: a

Expected Output: a c

I'm assuming that erase should return a valid iterator which I can use to insert more elements. If I add more inserts, I get duplicates of the first element.

#include <iostream>
#include <deque>

using namespace std;

int main()
{
    deque<char> tempQueue ;
    deque<char>::iterator it = tempQueue.begin();
    char temp = 'a';
    
    it = tempQueue.insert(it, temp);
    it++;
    
    temp = 'b';
    it = tempQueue.insert(it, temp);
    it++;
    
    it = tempQueue.erase(it);
    
    temp = 'c';
    it = tempQueue.insert(it, temp);
    it++;
    
    for (deque<char>::iterator tempIt = tempQueue.begin(); tempIt != tempQueue.end(); tempIt++)
    {
        cout << *tempIt << "\t";
    }
    
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire