gpt4 book ai didi

c++ - 如何优雅地输入一组点

转载 作者:太空宇宙 更新时间:2023-11-04 14:20:05 27 4
gpt4 key购买 nike

我想将一组点输入到库中,其中每个点都包含一组坐标。我希望输入尽可能灵活,以适应图书馆用户选择如何表示他们的数据。

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

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);

}

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

最佳答案

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

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

#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());


}

关于c++ - 如何优雅地输入一组点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8324895/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com