字符数组的队列 [英] Queue of array of characters

查看:187
本文介绍了字符数组的队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

现在,我想有一个队列可以存储5个这样的msg。因此,它是一个大小为5的队列,包含5个字符数组,每个数组最多可以包含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;


推荐答案

Edit:std :: vector可能是更好的选择,只是重读你的问题,看到字符数组的大小。如果你使用它来存储二进制数据, std :: queue< std :: vector< char> > msgs 可能是你最好的选择。

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;
}

您也可以只使用std :: string并避免错误。如果你使用 char * 你想要一个函数将msgs添加到队列中,它们不能在栈上(即你需要用 new malloc ),您必须记住在处理队列时删除它们。没有简单的方法来确定一个是在全局空间,一个在堆栈上,或一个是用新的。当未处理正确时,将导致未定义的行为或内存泄漏。 std :: string 会避免所有这些问题。

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;
}

如果只有5个标准消息,则 const char * 将是一个不错的选择,但如果他们总是相同的消息,你应该考虑一个整数的队列,引用你想要的消息。这样你可以关联更多的动作。但比你也可以考虑对象的队列。

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;
}

这篇关于字符数组的队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆