2012-03-19 51 views
1

我正在使用boost :: iostream lib将posix管道包装到gnuplot。要发送的二进制在线数据GNUPLOT,我正在做这样的事情来自std :: vector的未格式化流输入<double>

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的,并使用一个Iterator接口允许例如作为源的std :: list。我知道std :: ostreambuf_iterator允许无格式输入,但简单地使用std :: copy显然不起作用。

+0

的事情是,这将只是没有*作*与任何容器,而是一个'VECTOR',因为你是只需写入连续的内存范围而不考虑对象类型。不过,一个基于迭代器的包装器很不值钱。 – 2012-03-19 17:38:46

回答

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());

相关问题