相关疑难解决方法(0)

使用协议缓冲区发送图标/小图像

我有一个关于std :: string和google协议缓冲库的简单问题.我已经定义了这样的消息:

message Source
{
    required string Name = 1;
    required uint32 Id = 2;
    optional string ImplementationDLL = 3;
    optional bytes  Icon = 4;
}
Run Code Online (Sandbox Code Playgroud)

我想使用Icon字段发送图像,它很可能是一个png图像.在将其提供给protobuf编译器后,我得到了类似的东西来访问/操作Icon字段.

inline bool has_icon() const;
inline void clear_icon();
static const int kIconFieldNumber = 4;
inline const ::std::string& icon() const;
inline void set_icon(const ::std::string& value);
inline void set_icon(const char* value);
inline void set_icon(const void* value, size_t size);
inline ::std::string* mutable_icon();
Run Code Online (Sandbox Code Playgroud)

std :: string*mutable_icon()函数让我很头疼.它返回一个std :: string,但我相信字符串不能保存二进制数据!或者他们可以吗?

我可以使用set_icon(const void*,size_t)函数来放置二进制数据,但是我如何在另一端获取它?

我认为std :: string可能能够保存二进制数据,但是如何????

c++ stl protocol-buffers

4
推荐指数
1
解决办法
7212
查看次数

为什么不允许字符数组初始化std :: string?

在C ++中,您可以std::string从a char *和a 初始化一个对象const char *,这隐式地假定字符串将在NUL指针之后的第一个字符处结束。

在C ++中,字符串文字是数组,并且即使字符串文字包含Embedded,也可以使用模板构造函数来获取正确的大小NUL。例如,请参见以下玩具实现:

#include <stdio.h>
#include <string.h>
#include <vector>
#include <string>

struct String {
    std::vector<char> data;
    int size() const { return data.size(); }

    template<typename T> String(const T s);

    // Hack: the array will also possibly contain an ending NUL
    // we don't want...
    template<int N> String(const char (&s)[N])
        : data(s, s+N-(N>0 && s[N-1]=='\0')) {}

    // The non-const array removed as probably a lot of code …
Run Code Online (Sandbox Code Playgroud)

c++ arrays string string-literals

2
推荐指数
1
解决办法
1092
查看次数

标签 统计

c++ ×2

arrays ×1

protocol-buffers ×1

stl ×1

string ×1

string-literals ×1