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

More effective way to load a std::queue<uint8_t*> with elements?

我试图找到一种更有效的方法来将 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

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

这是您重写代码以使用 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

最终,我可以完全控制我可以使用的队列类型,而我对数据的呈现方式则零控制。具体来说,数据显示为 uint8_t* 和 size_t 长度。感谢@PaulMcKenie 的示例代码,我能够想出以下解决方案(顺便说一句,它非常快):

std::queue<std::vector<uint8_t>> myQueue;
while(true)
{
    // Pointer (myBuffer) and length (myLength) magically appear here
    myQueue.push({myBuffer, (uint8_t*) (myBuffer + myLength)});
}

问题已解决。