Boost 将结构向量的范围插入到结构成员类型的向量中

Boost 将结构向量的范围插入到结构成员类型的向量中,boost,vector,struct,c++03,Boost,Vector,Struct,C++03,是否可以将结构的范围直接插入相同类型的向量(结构成员的相同类型) 让我们有这样一个结构和向量: struct pnt { char _name; int _type; bool _aux; }; std::vector<pnt> pnts; std::vector<int> pntType; pntType.insert(pntType.begin(),pnts.begin(),pnts.end()); #include <boost/bind

是否可以将结构的范围直接插入相同类型的向量(结构成员的相同类型)

让我们有这样一个结构和向量:

struct pnt {
  char _name;
  int _type;
  bool _aux;
  };

std::vector<pnt> pnts;
std::vector<int> pntType;
pntType.insert(pntType.begin(),pnts.begin(),pnts.end());
#include <boost/bind.hpp>
#include <boost/iterator/transform_iterator.hpp>

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

struct A {
  int x;
};

int main() {
  A arr[] = {
    { 0 }, { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }
  };

  std::vector<A> v1(arr, arr + 6);
  std::vector<int> v2;

  v2.insert(v2.begin(),
            boost::make_transform_iterator(v1.begin() + 2, boost::bind(&A::x, _1)),
            boost::make_transform_iterator(v1.begin() + 4, boost::bind(&A::x, _1)));

  std::copy(v2.begin(), v2.end(), std::ostream_iterator<int>(std::cout, "\n"));
}
甚至是Boost库。 由于我经常在代码的不同部分使用它,所以我试图避免在循环中使用它。最后一个选项是为此定义一个函数

编辑


我知道插入语法。我不能做的是如何从pnts(仅每个成员的_类型)插入到pntType

将一个容器插入到其他容器中的工作如下:

struct pnt {
  char _name;
  int _type;
  bool _aux;
  };

std::vector<pnt> pnts;
std::vector<int> pntType;
pntType.insert(pntType.begin(),pnts.begin(),pnts.end());
#include <boost/bind.hpp>
#include <boost/iterator/transform_iterator.hpp>

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

struct A {
  int x;
};

int main() {
  A arr[] = {
    { 0 }, { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }
  };

  std::vector<A> v1(arr, arr + 6);
  std::vector<int> v2;

  v2.insert(v2.begin(),
            boost::make_transform_iterator(v1.begin() + 2, boost::bind(&A::x, _1)),
            boost::make_transform_iterator(v1.begin() + 4, boost::bind(&A::x, _1)));

  std::copy(v2.begin(), v2.end(), std::ostream_iterator<int>(std::cout, "\n"));
}
为了能够插入正确的类型,您应该将转换运算符添加到结构的
int

struct pnt {
  char _name;
  int _type;
  bool _aux;

  operator int (){
    return _type;
  }     
};

更新:有一个比我的第一个建议(见下图)更好的方法,因为我们已经在使用Boost了。std::transform和std::insert_迭代器的问题是v2的大小被调整了好几次,考虑到我们事先知道范围的宽度,这是浪费。使用boost::transform_迭代器和boost::bind,可以避免如下问题:

struct pnt {
  char _name;
  int _type;
  bool _aux;
  };

std::vector<pnt> pnts;
std::vector<int> pntType;
pntType.insert(pntType.begin(),pnts.begin(),pnts.end());
#include <boost/bind.hpp>
#include <boost/iterator/transform_iterator.hpp>

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

struct A {
  int x;
};

int main() {
  A arr[] = {
    { 0 }, { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }
  };

  std::vector<A> v1(arr, arr + 6);
  std::vector<int> v2;

  v2.insert(v2.begin(),
            boost::make_transform_iterator(v1.begin() + 2, boost::bind(&A::x, _1)),
            boost::make_transform_iterator(v1.begin() + 4, boost::bind(&A::x, _1)));

  std::copy(v2.begin(), v2.end(), std::ostream_iterator<int>(std::cout, "\n"));
}
#包括
#包括
#包括
#包括
#包括
#包括
结构A{
int x;
};
int main(){
A arr[]={
{ 0 }, { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }
};
标准::向量v1(arr,arr+6);
std::向量v2;
v2.insert(v2.begin(),
boost::make_transform_迭代器(v1.begin()+2,boost::bind(&A::x,_1)),
boost::make_transform_迭代器(v1.begin()+4,boost::bind(&A::x,_1));
std::copy(v2.begin()、v2.end()、std::ostream_迭代器(std::cout,“\n”);
}
旧建议:

boost::bind使用数据成员指针,因此使用C++98和boost,您可以在不更改结构的情况下执行以下操作:

#include <boost/bind.hpp>

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

struct A {
  int x;
};

int main() {
  A arr[] = {
    { 0 }, { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }
  };

  std::vector<A> v1(arr, arr + 6);
  std::vector<int> v2;

  // one-liner here:
  std::transform(v1.begin() + 2,
                 v1.begin() + 4,
                 std::insert_iterator<std::vector<int> >(v2, v2.begin()),
                 boost::bind(&A::x, _1));

  std::copy(v2.begin(), v2.end(), std::ostream_iterator<int>(std::cout, "\n"));
}
#包括
#包括
#包括
#包括
#包括
结构A{
int x;
};
int main(){
A arr[]={
{ 0 }, { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }
};
标准::向量v1(arr,arr+6);
std::向量v2;
//这里有一条班轮:
std::transform(v1.begin()+2,
v1.begin()+4,
std::insert_迭代器(v2,v2.begin()),
boost::bind(&A::x,_1));
std::copy(v2.begin()、v2.end()、std::ostream_迭代器(std::cout,“\n”);
}
使用增压范围:

boost::copy(pnts | transformed(std::mem_fn(&pnt::_type)), std::back_inserter(pntType));
甚至

boost::copy_range<std::vector<int>>(pnts | transformed(std::mem_fn(&pnt::_type)));

好的。请看Eddia.虽然这工作,隐式类型转换从代码> PNT <代码>到一个类型如<代码> int >代码>看起来是一个很糟糕的主意。好主意,但是“Auto'需要更高的C++标准。那么不要使用Autoto?我已经使用boost复制了所有成员,并更新了示例(老实说,我认为std::mem_fn也是c++11,但我没有看到a)。是吗?是的。我在举例。你可以提供任何范围,当然(
boost::make_iterator_range(f,l)| transformed(…)
works)你有点慢:)我在第一次回复中提到了这一点。无论如何。我的观点是,您仍然可以提高boost范围,以节省重复键入
boost::make_transform\u iterator
。不需要boost绑定,AFAICT
std::mem_fn
也做同样的事情(代码更少)
std::mem_fn
不是C++98。