繁体   English   中英

转换向量 <unsigned long> 到char缓冲区[]

[英]converting vector<unsigned long> to char buffer[]

如何转换vector<unsigned long> v; char buffer[sizeof(unsigned long)*v.size()] 反之亦然。

我尝试过

std::copy(b.begin(), b.end(), buffer);

v.insert()但结果不明确。 任何人都可以建议如何做到这一点。

如果您只需要将数据传递给需要char *的某些库函数,

assert(v.size());
char *p = (char*) &v[0];

否则,这里是一些示例代码,它们从std :: vector和char *来回复制数据,尽管我建议您坚持使用一个接口,除非您有理由这样做。

#include <iostream>
#include <vector>
#include <memory>
#include <assert.h>

int main()
{
    size_t count = 20;
    std::vector<unsigned int> v;
    v.resize(count);
    assert(v.size());

    // make a vector with the numbers 1 through count
    for (size_t index = 0; index < count; ++index)
        v[index] = index + 1;

    // make a buffer of char[] (using unique_ptr to handle cleanup)
    std::unique_ptr<char> buffer(new char[v.size() * sizeof(v[0])]);
    // copy from v into a buffer of char[]
    memcpy(buffer.get(), &v[0], v.size() * sizeof(v[0]));

    // next we get rid of v, and copy the elements back in from the char buffer
    v.clear();

    // now suppose we have a char array of count unsigned ints (which we do)
    // (the count better be right)
    // just say it's an unsigned int* and you get pointer arithmetic for unsigned int
    unsigned int * pInt = reinterpret_cast<unsigned int*>(buffer.get());
    for (size_t index = 0; index < count; ++index)
        v.push_back(*pInt++);

    // print out as proof
    for (auto &x : v)
        std::cout << x << " ";

    return 0;
}

例如,您可以尝试以下方法

#include <iostream>
#include <vector>
#include <cstring>
#include <numeric>

int main() 
{
    std::vector<unsigned long> v = { 1, 2, 3 };
    char *p = new char[sizeof( unsigned long ) * v.size()];

    std::accumulate( v.begin(), v.end(), p,
                     []( char *p, unsigned long x)
                     {
                        return memcpy( p, &x, sizeof( x ) ), p + sizeof( x );
                     } );

    std::vector<unsigned long> v2( v.size() );

    char *q = p;

    for ( auto &x : v2 )
    {
        memcpy( &x, q, sizeof( x ) );
        q += sizeof( x );
    }

    for ( auto x : v2 ) std::cout << x << ' ';
    std::cout << std::endl;

    delete []p;

    return 0;
}

输出是

1 2 3

lambda表达式中的return语句也可以这样写

return ( char * )memcpy( p, &x, sizeof( x ) ) + sizeof( x );

或者确实可以按照以下方式将整个向量复制到字符缓冲区中

std::memcpy( p, v.data(), v.size() * sizeof( unsigned long ) );

例如

#include <iostream>
#include <cstring>
#include <vector>

int main() 
{
    std::vector<unsigned long> v = { 1, 2, 3 };
    char *p = new char[sizeof( unsigned long ) * v.size()];

    memcpy( p, v.data(), v.size() * sizeof( unsigned long ) );

    std::vector<unsigned long> v2( v.size() );

    char *q = p;

    for ( auto &x : v2 )
    {
        memcpy( &x, q, sizeof( x ) );
        q += sizeof( x );
    }

    for ( auto x : v2 ) std::cout << x << ' ';
    std::cout << std::endl;

    delete []p;

    return 0;
}

而不是这个循环

    char *q = p;

    for ( auto &x : v2 )
    {
        memcpy( &x, q, sizeof( x ) );
        q += sizeof( x );
    }

您也可以使用memcpy。 例如

    memcpy( v2.data(), p, v2.size() * sizeof( unsigned long ) );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM