interpreting non ascii codepoint

ProtoBuf 在Windows VS2019 C++平台上的使用

这两天想着了解一下protobuf,搜索了一篇博客照着弄了一下

https://blog.csdn.net/weixin_44780793/article/details/120572110

有个问题是,我对着弄后定义的.proto文件生成的时候报错了

 

 一开始我以为是文件里有中文注释要弄成ANSI格式,发现还是不行

 

怎么改都不行,后边发现是用了中文状态下的;

改回去英文的;就通过了...

 

=============分割线===============

自己弄了个.proto,想着试试是否支持map和vector,发现map和c++的map写法一样,要用vector(数组)得用repeated +类型,这个测试文件如下

syntax = "proto3";

message ItemData {
    int32   ItemID = 1;
    map<int32,string> pos = 2;
    int32   ItemNum = 3;
    repeated int64 arr = 4;
}

测试代码:

    ItemData item;
    item.set_itemid(100);
    item.set_itemnum(25);
    
    // 设置map
    item.clear_pos();
    auto pos = item.mutable_pos();
    pos->insert({ 1,"first" });
    
    // 设置数组
    item.clear_arr();
    item.add_arr(666);
    item.add_arr(888);

    std::string str;
    str = item.SerializeAsString();
    std::cout << "str:" << str << std::endl;
    std::cout << "反序列化:" << std::endl;

    ItemData item1;
    item1.ParseFromString(str);
    std::cout<<"Id:"<<item1.itemid();
    std::cout << " num:" << item1.itemnum() << std::endl;;
    
    // 解析map
    for (auto it : item1.pos())
    {
        std::cout << " key:" << it.first << " value:" << it.second << std::endl;
    }
    // 解析数组
    for (auto it : item.arr())
    {
        std::cout << "v:" << it;
    }

ps:发现map不是直接用的std的map,操作接口有些不一样

结果正常:

 

 

关于protobuf的简单探讨先到此为止了

 

posted @ 2022-04-17 15:33  乐swap火  阅读(800)  评论(0编辑  收藏  举报