繁体   English   中英

如何优雅地输入一组积分

[英]How to input of an set of points elegantly

我想在库中输入一组点,其中每个点都包含一组坐标。 我希望输入内容对于库的用户如何选择代表其数据尽可能地灵活。

所以我希望能够调用以下伪代码

template<int dimenension> //the dimension of each of the points
struct set_of_points
{
  void insert(Iterator first_point, Iterator last_point);
}

从以下任何一项,

struct set_of_points<2> s;
double points1[3][2] = {
  {1,2},
  {2,3},
  {4,5}
};
s.insert(points1, points1+3);

double p1[2] = {1,2}, p2[2] = {2,3}, p3[2] = {4,5};
double* points2[3] = {p1,p2,p3};
s.insert(points2, points2+3);

std::vector<double*> points3;
points3[0] = p1; points3[1] = p2; points3[2] = p3;
s.insert(points3.begin(), points3.end())

我也可以将vector<vector<double> >vector< boost::array<double,2> >到该列表中。

我能想到的唯一方法是使用扩展的,丑陋的和手工制作的模板魔术。 例如,可以像这样完成数组的指针和指针。

#include<iostream>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_array.hpp>
#include <boost/type_traits/is_pointer.hpp>
#include <boost/mpl/if.hpp>
#include <boost/type_traits/remove_pointer.hpp>
#include <boost/mpl/and.hpp>
#include <boost/mpl/or.hpp>

template<int dimenension> //the dimension of each of the points
struct set_of_points
{
  //  The coordinates of each point are represented as an array or a set of pointers,
  //   And each point is in an array.
  template<typename PointsItr>
  void
  insert(PointsItr P_begin, PointsItr P_end,
     typename boost::enable_if<  //enable if
       typename boost::mpl::and_<  
         boost::is_pointer< PointsItr >, //The set of points is a pointer, AND
         typename boost::mpl::or_< //either
         boost::is_array<typename boost::remove_pointer< PointsItr >::type >, //The points are an array
           boost::is_pointer<typename boost::remove_pointer< PointsItr >::type > //or are pointers
          >::type  //close or
         >::type //close and
       >::type* dummy = 0)
  {
    std::cout<<"inserted pointer of (pointers OR array)"<<std::endl;
  }

};

int
main  (int ac, char **av)
{
  struct set_of_points<2> s;
  double points1[3][2] = {
    {1,2},
    {2,3},
    {4,5}
  };
  s.insert(points1, points1+3);

  double p1[2] = {1,2}, p2[2] = {2,3}, p3[2] = {4,5};
  double* points2[3] = {p1,p2,p3};
  s.insert(points2, points2+3);

}

Yu 有没有一种可维护的方式来做到这一点? 如果没有,是否有办法以某种方式将模板噪音整理到库中,因此我不必为我编写的每个容器编写此类代码。

好吧,找不到一种优雅的方式来做到这一点。

这是我到目前为止获得的解决方案,它似乎涵盖了大多数基础知识:

#include<iostream>

#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_array.hpp>
#include <boost/type_traits/is_pointer.hpp>
#include <boost/type_traits/is_class.hpp>
#include <boost/type_traits/remove_pointer.hpp>
#include <boost/mpl/and.hpp>
#include <boost/mpl/or.hpp>
#include <boost/array.hpp>

#include <vector>


template<int dimenension> //the dimension of each of the points
struct set_of_points
{
  //  The coordinates of each point are represented as an array or a set of pointers,
  //   And each point is in an array.
  template<typename PointsItr>
  void
  insert(PointsItr P_begin, PointsItr P_end,
     typename boost::enable_if<  //enable if
       typename boost::mpl::and_<  
         boost::is_pointer< PointsItr >, //The set of points is a pointer, AND
         typename boost::mpl::or_< //either
         boost::is_array<typename boost::remove_pointer< PointsItr >::type >, //The points are an array
           boost::is_pointer<typename boost::remove_pointer< PointsItr >::type > //or are pointers
              >::type  //close or
         >::type //close and
       >::type* dummy = 0)
  {
    //get the type of the array
    std::cout<<"inserted pointer of (pointers OR array)"<<std::endl;
  }

  //  The coordinates of each point are represented as an array or a set of pointers,
  //   And each point is in a container.
  template<typename PointsItr>
  void
  insert(PointsItr P_begin, PointsItr P_end,
     typename boost::enable_if<  //enable if
       typename boost::mpl::and_<  
         boost::is_pointer< PointsItr >, //The set of points is a pointer, AND
         boost::is_class<typename boost::remove_pointer< PointsItr >::type> //The points are wrapped in a class, assume has a begin and end method
         >::type //close and
       >::type* dummy = 0)
  {
    //get the type of the array
    std::cout<<"inserted pointer of container"<<std::endl;
  }

  //  The coordinates of each point are represented as a class,
  //   And each point is an array or pointer
  template<typename PointsItr>
  void
  insert(PointsItr P_begin, PointsItr P_end,
     typename boost::enable_if<  //enable if
       typename boost::mpl::and_<  
         boost::is_class< PointsItr >, //The set of points is a class, AND
         typename boost::mpl::or_< //either
           boost::is_array<typename PointsItr::value_type >, //The points are an array
           boost::is_pointer<typename PointsItr::value_type> //or are pointers
           >::type  //close or
         >::type //close and
       >::type* dummy = 0)
  {
    //get the type of the array
    std::cout<<"inserted container of pointers"<<std::endl;
  }

  //  The coordinates of each point are represented as a class,
  //   And each point is a class
  template<typename PointsItr>
  void
  insert(PointsItr P_begin, PointsItr P_end,
     typename boost::enable_if<  //enable if
       typename boost::mpl::and_<  
         boost::is_class< PointsItr >, //The set of points is a class, AND
           boost::is_class<typename PointsItr::value_type > //The points are a class
         >::type //close and
       >::type* dummy = 0)
  {
    //get the type of the array
    std::cout<<"inserted container of containers"<<std::endl;
  }
};

int
main  (int ac, char **av)
{
  struct set_of_points<2> s;
  double points1[3][2] = {
    {1,2},
    {2,3},
    {4,5}
  };
  s.insert(points1, points1+3);

  double p1[2] = {1,2}, p2[2] = {2,3}, p3[2] = {4,5};
  double* points2[3] = {p1,p2,p3};
  s.insert(points2, points2+3);

  boost::array<double,2> p4,p5,p6;
  p4[0] = 1.0; p4[1] = 2.0;
  p5[0] = 1.0; p5[1] = 2.0;
  p6[0] = 1.0; p6[1] = 2.0;
  boost::array<double,2> points3[3] = {p4,p5,p6};
  s.insert(points3, points3+3);

  std::vector<double*> points4(3);
  points4[0]=p1; points4[1]=p2; points4[2]=p3;
  s.insert(points4.begin(), points4.end());

  std::vector<boost::array<double,2> > points5(3);
  points5[0]=p4; points5[1]=p5; points5[2]=p6;

  s.insert(points5.begin(), points5.end());


}

没有标准的方法可以执行您要的操作,因为如果double *表示一个点(如points2,points3),则您不知道数组的大小。 当然,除非您信任库的用户提供正确的大小并且不会引起段错误(不要这样做:-))。

但是,如果要允许向量,c样式双精度数组或任何双精度容器表示一个点,请考虑使用BOOST_FOREACH (或C ++ 11 foreach循环):

template <typename T>
Point MakePoint(const T& pointData)
{
    Point p;
    BOOST_FOREACH(double& x, pointData)
    {
        // check p isn't full
        p.Add(x);
    }

    return p;
}

(不必担心花哨的enable_if东西,BOOST_FOREACH或c ++ 11 foreach将确保T是双精度容器。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM