[英]print member of struct of a vector
I try to print only a struct member from vector of my struct (struct pnt). 我尝试仅从我的结构(struct pnt)的向量中打印一个结构成员。 let me be clear by showing this:
通过显示以下内容让我很清楚:
struct pnt {
char _name;
int _type;
bool _aux;
};
boost::copy(pntVec.begin(),pntVec.end()|
boost::adaptors::transformed(bind(&pnt::_type, _1)),
std::ostream_iterator<int>(std::cout, "\n"));
but I get an error. 但我得到一个错误。 I will appreciate if you could please help me to find the reason for that.
如果您能帮助我找到原因,我将不胜感激。
Error : 错误 :
error: no type named ‘type’ in ‘boost::mpl::eval_if_c<true, boost::range_const_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> >, void>, boost::range_mutable_iterator<const __gnu_cxx::__normal_iterator<int*, std::vector<int> >, void> >::f_ {aka struct boost::range_const_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> >, void>}’
NOTE : I can olny use C++98, boost v.1.56 注意 :我可以使用C ++ 98,提升v.1.56
Boost Adaptor adapts ranges , not single iterators . Boost Adapter适应范围 ,而不是单个迭代器 。 Similarily, the
boost::copy
algorithm expects two arguments, where the first one is a range , and the second an output iterator . 类似地,
boost::copy
算法需要两个参数,其中第一个是范围 ,第二个是输出迭代器 。 Having said that, the correct syntax for extracting a single data member from a range , and copying it to an output iterator is: 话虽如此,用于从range提取单个数据成员并将其复制到输出迭代器的正确语法是:
boost::copy(pntVec | boost::adaptors::transformed(bind(&pnt::_type, _1))
, std::ostream_iterator<int>(std::cout, "\n"));
Alternatively, since Phoenix in a way supersedes Boost Bind + Boost Lambda, you could use Boost Phoenix: 另外,由于Phoenix可以取代Boost Bind + Boost Lambda,因此可以使用Boost Phoenix:
boost::for_each(pntVec, std::cout << bind(&pnt::_type, arg1) << "\n");
or even without bind
, if you don't fear a bit of arcane syntax: 甚至没有
bind
,如果您不用担心一些奥秘的语法:
boost::for_each(v, std::cout << (arg1->*&pnt::_type) << "\n");
That's pretty expressive. 这很富有表现力。 See it Live On Coliru
在Coliru上实时观看
Also, note that there's mem_fn
in Boost/c++11: pntVec | transformed(mem_fn(&pnt::_type))
另外请注意,还有的
mem_fn
在升压/ C ++ 11: pntVec | transformed(mem_fn(&pnt::_type))
pntVec | transformed(mem_fn(&pnt::_type))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.