2012-03-19 29 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());

相關問題