Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Queue of array of characters

include <queue>
using namespace std;
char msg[1000];

Now, I want to have a queue that can store 5 of this kind of msg. So, it is a queue of size 5 that contains 5 arrays of characters, each array can contain up to 1000 chars.

How can I initiate the queue? I tried this but it did not work.

char msg[1000];
queue<msg> p;
like image 586
JJ Liu Avatar asked Oct 25 '25 02:10

JJ Liu


2 Answers

struct msg {
  char data[1000];
};
queue<msg> p;
like image 87
smparkes Avatar answered Oct 26 '25 17:10

smparkes


Edit: std::vector may be better choice, just reread you question and saw the size of the character array. If you are using it to store binary data, a std::queue< std::vector <char> > msgs is probably your best choice.

You cannot use a variable as a type. You could have a queue of character pointers though.

#include <iostream>
#include <queue>

std::queue <char*> msgs;

int main()
{
    char one[50]="Hello";
    msgs.push(one);
    char two[50]="World\n\n";
    msgs.push(two);

    msgs.push("This works two even though it is a const character array, you should not modify it when you pop it though.");


    while(!msgs.empty())
    {
        std::cout << msgs.front();
        msgs.pop();
    }

return 1;
}

You could also just use std::string and avoid mistakes. If you use a char* you want a function to add msgs to queue, they cannot be on the stack (ie you would need to create them with new or malloc) than you would have to remember to delete them when you processed the queue. There would be no easy way to determine if one is in global space, one is on the stack, or one was made with new. Would lead to undefined behavior or memory leaks when not handled correct. std::string would avoid all of these problems.

#include <iostream>
#include <queue>
#include <string>

std::queue <std::string> msgs;

int main()
{

    msgs.push("Hello");
    msgs.push("World");

    while(!msgs.empty())
    {
        std::cout << msgs.front();
        msgs.pop();
    }

return 1;
}

If it is just 5 Standard messages, then const char* would be a decent choice, but if they are always the same messages, you should consider a queue of integers that refer to the message you want. This way you could associate more actions with it. But than you could also consider a queue of objects.

#include <iostream>
#include <queue>

std::queue <int> msgs;

int main()
{

    msgs.push(1);
    msgs.push(2);

    while(!msgs.empty())
    {
        switch(msgs.front())
        {
        case 1:
            std::cout << "Hello";
        break;
        case 2:
            std::cout << "World";
        break;
        default:
            std::cout << "Unkown Message";
        }

        msgs.pop();
    }

    return 1;
}
like image 38
Joe McGrath Avatar answered Oct 26 '25 16:10

Joe McGrath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!