簡體   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