c++ - 使用元素加载 std::queue<uint8_t*> 的更有效方法?

标签 c++ queue push std

我试图找到一种更有效的方法来将 uint8_t 字节的可变长度数组加载到 std::queue

以下代码片段试图将实际代码简化为更有用的示例;如果它过于复杂,请原谅我。

代码片段有效,但我无法确定 std::queue 的每个元素的实际长度,而它们仍在队列的前面。我的问题是,“有没有什么方法可以将指向无符号字节数组的指针插入队列,而无需创建本地数组、将传递的参数复制到其中然后将本地指针插入队列的中间步骤(请参阅代码中的注释)?

#include <queue>
#include <string>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

using namespace std;

std::queue<uint8_t*> _q;

void routineSubroutine(uint8_t array_a[], int size_a)
{
    /*
     * Is there anyway to push the uint8 array_a into the queue (_q) without
     * creating a new pointer to a byte array, copying the passed
     * argument into it and the pushing it?
     */

    uint8_t* a = new uint8_t[size_a];
    memcpy((void*) a, (const void*) array_a, size_a);
    _q.push(a);
}

int main(int argc, char** argv)
{
    uint8_t myArray[512];

    char cfa[] = {"I wish I was at Chick-Fil-A right now"};
    memset((void*) myArray, 0x00, sizeof (myArray));
    memcpy((void*) myArray, (const void*) cfa, strlen(cfa));
    routineSubroutine(myArray, strlen(cfa));

    char five[] = {"Five Guys will do in a pinch"};
    memcpy((void*) myArray, (const void*) five, strlen(five));
    routineSubroutine(myArray, strlen(five));

    while (_q.size() > 0)
    {
        printf("Queue string value = %s\n", (char*) _q.front());
        /*
         * How do I go about determining the number of bytes in the uint8_t
         * array, whose address is at the front of the queue?
         */
        _q.pop();
    }

    return 0;
}

最佳答案

The code snippet works, with the exception of my inability to determine the actual length of each of the elements of the std::queue while they are still at the front of the queue

使用知道其长度/大小的适当容器并调用适当的成员函数。单纯的指针做不到这一点。

以下是您使用 std::vector 重写的代码示例:

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

std::queue<std::vector<uint8_t>> _q;

void routineSubroutine(const std::vector<uint8_t>& a)
{
    _q.push(a);
}

int main(int argc, char** argv)
{
    char cfa[] = {"I wish I was at Chick-Fil-A right now"};
    routineSubroutine({std::begin(cfa), std::end(cfa)}); // creates a temp uint8_t vector

    char five[] = {"Five Guys will do in a pinch"};
    routineSubroutine({std::begin(five), std::end(five)}); // creates a temp uint8_t vector

    while ( !_q.empty() )
    {
        // use the `write()` function to control the number of characters
        std::cout.write(reinterpret_cast<const char *>(_q.front().data()), _q.front().size());
        std::cout << "\n";
        _q.pop();
    }
    return 0;
}

输出:

I wish I was at Chick-Fil-A right now
Five Guys will do in a pinch

关于c++ - 使用元素加载 std::queue<uint8_t*> 的更有效方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56908778/

相关文章:

android - 如何在不使用静态变量中的硬编码值的情况下为 Android C2DM 初始化发件人 ID?

Android FCM 通知问题

c# - 获取内存值数组的最快方法

C++ access_once

java - 入队/出队或提供/轮询

c++ - 具有订单保证的阻塞队列

javascript - 添加相同的属性:value pair to multiple objects in array

c++ - 在 C++ 中设计一个高性能的数值函数

c++ - 替代标准 std::string 数据类型和 print()

linux - linux命令行的顺序FIFO队列