转换 c++ std::向量到 std::vector(反之亦然)

发布于 2025-01-08 16:23:05 字数 220 浏览 3 评论 0原文

有没有一种简单的方法可以将 std::vector 转换为 std::vector (然后再转换回 >std::vector,而不必手动转换每个字符串并添加分隔符,例如逗号?

Is there an easy way to convert a std::vector<std::string> to a std::vector<unsigned char> (and back again to a std::vector<std::string>, without having to manually convert each string and add a delimiter, such as a comma?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

何处潇湘 2025-01-15 16:23:05

简短的回答是:不。

向量和字符串的实现方式是作为独立的堆分配数组。因此,您可以将 vector 转换为 char** (由 char 数组组成的锯齿状数组),以及 vector当考虑内部结构时,将其转换为 char* (一个 char 数组)。这将你的问题变成:有没有办法连接数组而不必复制它们?

不,不,没有。

The short answer is: no.

The way vectors and strings are implemented are as independant, heap-allocated arrays. Therefore, you could transform vector<string> into char** (a jagged array of arrays of char), and vector<unsigned char> into char* (an array of char) when thinking about internals. This turns your problem into: is there any way to concatenate arrays without having to copy them?

No. No there is not.

心房的律动 2025-01-15 16:23:05
std::vector<char> chars;
for (const std::string& s : strings)
{
    for (char c : s)
    {
        chars.push_back(c);
    }
    chars.push_back(',');
}

如果没有新的 for 循环语法,它会有点笨拙,但你明白了。

std::vector<char> chars;
for (const std::string& s : strings)
{
    for (char c : s)
    {
        chars.push_back(c);
    }
    chars.push_back(',');
}

It's a little more clumsy without the new for loop syntax, but you get the idea.

夜司空 2025-01-15 16:23:05

Boost Serialization 应该可以让你填充数据结构转换为 unsigned char 序列并再次重构。

Boost Serialization should let you stuff a data structure into a sequence of unsigned char and reconstitute it again.

余罪 2025-01-15 16:23:05

第一个问题是为什么,以及你想做什么?什么是
std::vector 代表什么,其语义应该是什么
转换是?如果你只是想连接,那么最简单的
解决方案类似于:将

std::vector<unsigned char> results;
for ( std::vector<std::string>::const_iterator iter = source.begin();
        iter != source.end();
        ++ iter ) {
    results.insert( results.end(), iter->begin(), iter->end() );
}

char 隐式转换为 unsigned char 将处理
结果。

如果您需要为每个插入某种分隔符或终止符
源中的字符串,您也可以在循环中执行此操作:
终止符,只需将其附加(push_back)在insert之后;对于一个
分隔符,我通常有条件地将其附加在 insert 之前,
例如:

std::vector<unsigned char> results;
for ( std::vector<std::string>::const_iterator iter = source.begin();
        iter != source.end();
        ++ iter ) {
    if ( iter != source.begin() ) {
        results.push_back( separator );
    }
    results.insert( results.end(), iter->begin(), iter->end() );
}

但问题是:为什么使用unsigned char?大概是因为你是
格式化为某些特定协议的缓冲区。是否有一些额外的
需要格式化吗?您的协议中字符串的格式是什么?
(通常,它将是长度 + 数据,或以 '\0' 终止。)
该协议是否需要某种调整? (对于 XDR——其中一个
,您需要类似: 。)

std::vector<unsigned char> results;
for ( std::vector<std::string>::const_iterator iter = source.begin();
        iter != source.end();
        ++ iter ) {
    size_t len = iter->size();
    results.push_back( (len >> 24) & 0xFF );
    results.push_back( (len >> 16) & 0xFF );
    results.push_back( (len >>  8) & 0xFF );
    results.push_back( (len      ) & 0xFF );
    results.insert( results.end(), iter->begin(), iter->end() );
    while ( results.size() % 4 != 0 ) {
        results.push_back( '\0' );
    }
}

使用最广泛的协议 -例如

The first question is why, and what are you trying to do? What does the
std::vector<std::string> represent, and what should the semantics of
the conversion be? If you just want to concatenate, then the simplest
solution would be something like:

std::vector<unsigned char> results;
for ( std::vector<std::string>::const_iterator iter = source.begin();
        iter != source.end();
        ++ iter ) {
    results.insert( results.end(), iter->begin(), iter->end() );
}

The implicit conversion of char to unsigned char will take care of
the reslt.

If you need to insert some sort of separator or terminator for each
string in the source, you can do that in the loop as well: for a
terminator, just append it (push_back) after the insert; for a
separator, I generally append it conditionally before the insert,
e.g.:

std::vector<unsigned char> results;
for ( std::vector<std::string>::const_iterator iter = source.begin();
        iter != source.end();
        ++ iter ) {
    if ( iter != source.begin() ) {
        results.push_back( separator );
    }
    results.insert( results.end(), iter->begin(), iter->end() );
}

But the question is: why unsigned char? Presumably, because you are
formatting into a buffer for some specific protocol. Is some additional
formatting required? What is the format of a string in your protocol?
(Typically, it will be either length + data, or '\0' terminated.)
Does the protocol require some sort of alignment? (For XDR—one of
the most widely used protocols—, you'd need something like:

std::vector<unsigned char> results;
for ( std::vector<std::string>::const_iterator iter = source.begin();
        iter != source.end();
        ++ iter ) {
    size_t len = iter->size();
    results.push_back( (len >> 24) & 0xFF );
    results.push_back( (len >> 16) & 0xFF );
    results.push_back( (len >>  8) & 0xFF );
    results.push_back( (len      ) & 0xFF );
    results.insert( results.end(), iter->begin(), iter->end() );
    while ( results.size() % 4 != 0 ) {
        results.push_back( '\0' );
    }
}

, for example.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文