How to serialize boost::rational

前端 未结 2 1313
别跟我提以往
别跟我提以往 2021-01-25 04:23

I cannot serialize boost::rational. I searched for a boost/serialize/rational.h header but it does not exist.

 /usr/include/boost/seriali         


        
相关标签:
2条回答
  • 2021-01-25 04:57

    Use the provided input/output functions: https://www.boost.org/doc/libs/1_64_0/libs/rational/rational.html#Input%20and%20Output

    Serialize to a std::string:

    template <typename I>
    std::string serialize(boost::rational<I>& rational) const {
      std::stringstream rational_ss;
    
      rational_ss << rational;
      return rational_ss.str();
    }
    

    This serializes the rational to a string that looks like "3/5" e.g.

    To deserialize, use boost::rational's >>.

    0 讨论(0)
  • 2021-01-25 05:20

    Just serialize the numerator and denominator.

    Here's the legwork, in semi-generic form (supports archives with named nodes, like XML serialization, too): Live On Coliru

    #include <boost/archive/xml_iarchive.hpp>
    #include <boost/archive/xml_oarchive.hpp>
    #include <boost/serialization/serialization.hpp>
    #include <boost/rational.hpp>
    #include <iostream>
    
    namespace boost { namespace serialization {
    
        template <typename Archive, typename T>
            void save(Archive& ar, ::boost::rational<T> const& r, unsigned /*version*/)
            {
                int n = r.numerator(), d = r.denominator();
                ar & boost::serialization::make_nvp("numerator", n);
                ar & boost::serialization::make_nvp("denominator", d);
            }
    
        template <typename Archive, typename T>
            void load(Archive& ar, ::boost::rational<T>& r, unsigned /*version*/)
            {
                int n, d;
                ar & boost::serialization::make_nvp("numerator", n);
                ar & boost::serialization::make_nvp("denominator", d);
    
                r = ::boost::rational<T>(n, d);
            }
    
    } }
    
    BOOST_SERIALIZATION_SPLIT_FREE(boost::rational<int>);
    
    using namespace boost;
    #include <iostream>
    #include <sstream>
    
    int main()
    {
        rational<int> number(2, 3), other;
    
        std::stringstream ss;
        {
            archive::xml_oarchive oa(ss);
            oa << serialization::make_nvp("rational", number);
        }
    
        std::cout << "Serialized: '" << ss.str() << "'\n";
    
        {
            archive::xml_iarchive ia(ss);
            ia >> serialization::make_nvp("rational", other);
        }
    
        std::cout << "Deserialized: " << other;
    }
    

    Prints

    Serialized: '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <!DOCTYPE boost_serialization>
    <boost_serialization signature="serialization::archive" version="10">
    <rational class_id="0" tracking_level="0" version="0">
        <numerator>2</numerator>
        <denominator>3</denominator>
    </rational>
    </boost_serialization>
    
    '
    Deserialized: 2/3
    
    0 讨论(0)
提交回复
热议问题