我想在库中输入一组点,其中集合中的每个点都包含一组坐标。我希望输入尽可能灵活,以便库的用户选择如何表示他们的数据。
所以我希望能够调用以下伪代码
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);
}
育。有可行的方法吗?如果没有,有没有办法以某种方式将模板噪声整理到库中,所以我不必为我写的每个容器编写这样的代码。
答案 0 :(得分:0)
好的,找不到这样做的优雅方式。
这是我到目前为止的解决方案,似乎涵盖了大多数基础:
#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());
}
答案 1 :(得分:0)
没有标准的方法来做你要求的事情,因为如果一个双*代表一个点(如在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是双打的容器。)