相关疑难解决方法(0)

从常量内存创建输入流

我在const char*指针指向的缓冲区中有一些数据.数据只是一个ASCII字符串.我知道它的大小.我希望能够以与从流中读取数据相同的方式读取它.我正在寻找一个允许我编写如下代码的解决方案:

// for example, data points to a string "42 3.14 blah"
MemoryStreamWrapper in(data, data_size);
int x;
float y;
std::string w;
in >> x >> y >> w;
Run Code Online (Sandbox Code Playgroud)

重要条件:不得以任何方式复制或更改数据(否则我只使用字符串流.据我所知,不能在不复制数据的情况下从const char指针创建字符串流. )

c++ input stream

23
推荐指数
1
解决办法
1万
查看次数

直接将序列化提升为char数组

Boost序列化文档断言,序列化/反序列化项目的方法是使用二进制/文本存档以及基础结构上的流.如果我不想将序列化数据用作std :: string,这可以正常工作,但我的目的是将其直接转换为char*缓冲区.如何在不创建临时字符串的情况下实现此目的?

解决了!对于想要一个例子的人:

char buffer[4096];

boost::iostreams::basic_array_sink<char> sr(buffer, buffer_size);  
boost::iostreams::stream< boost::iostreams::basic_array_sink<char> > source(sr);

boost::archive::binary_oarchive oa(source);

oa << serializable_object; 
Run Code Online (Sandbox Code Playgroud)

c++ serialization boost

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

如何序列化包含指向原语的指针的类?

我正在尝试使用 boost 的功能来序列化指向原语的指针(这样我就不必自己取消引用并进行深度存储)。然而,当我尝试这样做时,我遇到了一堆错误。这是一个简单的类示例,该类应该包含saveload文件写入和读取类内容的方法。该程序无法编译:

#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>

#include <boost/serialization/shared_ptr.hpp>
#include <boost/shared_ptr.hpp>

#include <fstream>

class A
{
public:
    boost::shared_ptr<int> sp;
    int const * p;

    int const& get() {return *p;}

    void A::Save(char * const filename);
    static A * const Load(char * const filename);

        //////////////////////////////////
        // Boost Serialization:
        //
    private:
        friend class boost::serialization::access;
        template<class Archive>
        void serialize(Archive & ar,const unsigned int file_version)
        {
            ar & p & v;
        }
};

// save the world to a file:
void A::Save(char …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-serialization

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

标签 统计

c++ ×3

boost ×2

boost-serialization ×1

input ×1

serialization ×1

stream ×1