Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 打印向量结构的成员_C++_Boost - Fatal编程技术网

C++ 打印向量结构的成员

C++ 打印向量结构的成员,c++,boost,C++,Boost,我试图从我的结构向量(structpnt)中只打印一个结构成员。让我清楚地表明这一点: 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"

我试图从我的结构向量(structpnt)中只打印一个结构成员。让我清楚地表明这一点:

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"));
struct pnt{
字符名称;
int_型;
布尔奥克斯;
};
boost::copy(pntVec.begin(),pntVec.end())|
boost::adapters::transformed(绑定(&pnt::_-type,_-1)),
std::ostream_迭代器(std::cout,“\n”);
但是我犯了一个错误。如果你能帮我找到原因,我将不胜感激

错误

 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>}’
错误:“boost::mpl::eval_if_c::f_{aka struct boost::range_const_iterator}”中没有名为“type”的类型
注意:我可以使用C++98,boost v.1.56

boost适配器适应范围,而不是单个迭代器。类似地,
boost::copy
算法需要两个参数,其中第一个是范围,第二个是输出迭代器。话虽如此,从范围中提取单个数据成员并将其复制到输出迭代器的正确语法是:

boost::copy(pntVec | boost::adaptors::transformed(bind(&pnt::_type, _1))
          , std::ostream_iterator<int>(std::cout, "\n"));
boost::copy(pntVec | boost::adapters::transformed(绑定(&pnt:_-type,_-1))
,std::ostream_迭代器(std::cout,“\n”);

或者,由于Phoenix在某种程度上取代了Boost Bind+Boost Lambda,您可以使用Boost Phoenix:

boost::for_each(pntVec, std::cout << bind(&pnt::_type, arg1) << "\n");
这很有表现力。查看它



另外,请注意Boost/c++11中有
mem\u fn
:pntVec | transformed(mem\u fn(&pnt::_type))

“我得到一个错误”。请在问题中包含您收到的确切错误消息。错误已被删除added@H您是指
boost::copy(pntVec | boost::adapters::transformed(bind(&pnt:_type,_1)),std::ostream_迭代器(std::cout,“\n”)?@PiotrS。是的,我想打印所有向量的整数部分member@BartekBanachewicz
std::basic_ostream
没有
push_back
成员函数,没有理由添加
back_inserter
语法变量以确保完整性
boost::for_each(v, std::cout << (arg1->*&pnt::_type) << "\n");