来自std :: vector <double> </double>的未格式化的流输入

时间:2012-03-19 17:33:32

标签: c++ boost gnuplot

我正在使用boost :: iostream lib将posix管道包装到gnuplot。要将二进制内联数据发送到gnuplot,我现在正在做类似这样的事情

std::vector<double> d = test_data();
Gnuplot plt; //custom gnuplot class derived from boost::iostream::stream

plt << "plot '-' binary format='%double' notitle\n"
plt.write( (char*)( &c.front() ), sizeof(double)*c.size() ); // send binary data

它有效,但我想摆脱.write并使用迭代器接口来允许例如一个std :: list作为源。我知道std :: ostreambuf_iterator允许无格式输入,但只是使用std :: copy显然不起作用。

1 个答案:

答案 0 :(得分:2)

这是一个用于写出范围的天真包装模板:

#include <memory>
#include <iterator>

template <typename FwdIter>
write_range(Gnuplot & gp, FwdIter it, FwdIter end)
{
    typedef typename std::iterator_traits<FwdIter>::value_type type;
    for ( ; it != end; ++it)
    {
        gp.write(reinterpret_cast<char const *>(std::addressof(*it)), sizeof(type));
    }
}

用法:write_range(gp, mylist.begin(), mylist.end());