0

I have a queue that defined as

queue<CData*> queue1;

In which class CData is

typedef unsigned char  U8;
typedef unsigned int   U32;

class CData
{
private:
    U8* m_Data;
    U32  m_Len;
public:
    CData(void) : m_Data(NULL), m_Len(0)
    {
    }
    ~CData(void)
    {
        FreeData();
    }
    void FreeData()
    {
        if (m_Data)
        {
            delete[] m_Data;
            m_Data = NULL;
        }
    }
};

I assume that my queue1 is initialized values as the bellow code

for (U32 k = 0; k<5; k++)
{
    size_t data_size = 1;
    U8 *data_buf = new U8[data_size];
    for (size_t i = 0; i < data_size; ++i)
    {
        data_buf[i] = k;
    }
    CData* result = new CData(data_buf, data_size);
    queue1.push(result);
    delete[] data_buf;
    data_buf = NULL;
}

Now, my queue1 will contain the value

0 1 2 3 4

My question is that how can I insert 3 number of zero values at the beginning of the queue1, so that the result will be

0 0 0 0 1 2 3 4

Second, How can i free/delete queue1 , after the application done. And if I used

CData* result = new CData(); 

How can I delete result variable? I am using C++ in Ubuntu. Thanks in advance.

Jame
  • 3,746
  • 6
  • 52
  • 101
  • You should ask one question at a time. – juanchopanza Dec 28 '15 at 11:52
  • Thanks. Just considering adds value at the front of queue first – Jame Dec 28 '15 at 11:55
  • As far as I know you cannot add elements at the front of a queue. I use std::list or std::deque instead. std::queue is only a wrapper of one of these anyway. Another way would be to copy the whole queue at the end of a new queue which already includes the necessary front elements. – Martin Schlott Dec 28 '15 at 12:16
  • If you want to add at both ends, it's not a queue but a [deque](http://en.cppreference.com/w/cpp/container/deque). (If you have a habit of entering a queue at the front, you should stop immediately; it's very rude.) – molbdnilo Dec 28 '15 at 12:21
  • Thanks. So, Can I delete the result pointer? – Jame Dec 28 '15 at 12:24
  • Thanks. So, i have other idea that is first initial all values in queue as zeros value (4+4=8 zeros value). Then I replace 4th values in queue as 0,1,2...Is it possible – Jame Dec 28 '15 at 12:26
  • @user8430 You seem to not understand what a queue is. You add elements at the back and remove them at the front, that's it. It's not a random-access container. – molbdnilo Dec 28 '15 at 12:28
  • Thank all. How about my second problem. Can I delete the result pointer – Jame Dec 28 '15 at 13:39

1 Answers1

4

By definition from here:

The std::queue class is a container adapter that gives the programmer the functionality of a queue - specifically, a FIFO (first-in, first-out) data structure.

This means if you need to insert elements into beging you need an another abstraction. For example std::deque (double-ended queue).

αλεχολυτ
  • 4,792
  • 1
  • 35
  • 71
  • Thanks. So, i have other idea that is first initial all values in queue as zeros value (4+4=8 zeros value). Then I replace 4th values in queue as 0,1,2...Is it possible – Jame Dec 28 '15 at 12:25
  • @user8430 here is [answer](http://stackoverflow.com/a/7021544/3240681) that should help you to select appropriate container. – αλεχολυτ Dec 28 '15 at 12:30
  • @user8430 You can do it with `delete result;`. But better use a stack allocated variable (if it's possible). – αλεχολυτ Dec 28 '15 at 13:52