- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想将一组点输入到库中,其中每个点都包含一组坐标。我希望输入尽可能灵活,以适应图书馆用户选择如何表示他们的数据。
所以我希望能够调用下面的伪代码
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/
您如何优雅编码同一tableView中的两种类型的单元格? 显然我可以这样: NSDictionary *cellInfo = [_userInformation objectAtIndex:inde
假设我正在编写一个仅包含标题或主要包含标题的库,并且具有以下代码: using my_type = int; namespace detail { inline void foo() { my
我正在使用复选框和输入进行一系列启用/禁用选择,我想知道我是否可以使用循环、变量或复合语句来简单地处理这个js?感觉就像是使用大量代码来实现相对简单的功能。 这是我正在做的事情的一个 fiddle :
我正在尝试为来自维基百科的 API 响应编写一个解析器。它真的很困惑,我已经求助于旧的 RegEx 来清理大部分东西。然而,我坚持这一点。考虑一个字符串: var a ="[[December 1]
我正在通过一个 channel 接收多个消息,并在对其进行迭代之后,我想保留最后一个元素以供进一步使用。我的第一个(可能很糟糕!)方法是声明一些变量,然后在每个循环中分配它。 let last = 0
我正在编写一个 PHP Web 应用程序,它将在不久的将来在生产环境下运行,而不是使用非用户友好的 die() , 我想我会想出一个 Class处理错误消息。 基本上,我的思考过程是这样的: 如果 W
我们有 elb 负载平衡 2 台运行 tomcat 作为应用程序服务器的 WAS 机器。要实现AWS环境下的不间断部署,我们应该, 选择部署目标 WAS。 让它停止来自 elb 的交易。(elb 暂停
何为pythonic? pythonic如果翻译成中文的话就是很python。很+名词结构的用法在中国不少,比如:很娘,很国足,很CCTV等等。 我的理解为,很+名词表达了一种特殊和强调的意味。
认为已经有对此的答案,但找不到。我一直在以某种方式解析方法选项,并想检查并确保它是最优雅/最简洁的方式。 这是我通常做的: def some_method *args options = args
我正在清理我的一个旧项目。它必须做的一件事是——给定笛卡尔网格系统和网格上的两个正方形,找到所有正方形的列表,连接这两个正方形中心的线将通过这些正方形。 这里的特殊情况是所有起点和终点都被限制在正方形
如何使系统 ( SystemB1 ) 访问另一个系统 ( SystemA::sub ) 的字段,就好像它是自己的字段一样? SystemA是一个拥有自己领域的实用系统 Sub* sub . Syste
我有一个包含约 8.000.000 条记录的 MySQL 数据库。因为我需要处理所有这些,所以我使用 BlockingQueue 作为生产者从数据库读取数据并将 1000 条记录放入队列中。 Cons
我正在让我的 HTTP 服务器正常关闭。我从帖子中获取了提示 here ,到目前为止,我的代码是这样设置的: func start() { //...... //START HTTP/
示例脚本只是“wc -m”命令的包装器,简单的符号计数器。我尝试只用“teststrings” slice 元素提供输入。并在输出监听器 goroutine 接收每个字符串的符号数。寻找一种让“wc”
我想干净/优雅地关闭 Internet Explorer。 taskkill 会关闭它,但是当重新打开它时,它会询问您是否要重新打开上一个 session 。 最佳答案 尝试 CloseMainWin
Haskell 的简洁和优雅给我留下了深刻的印象。但我在 .Net 公司工作,所以当我可以使用 F# 时我会使用它——我可能是全国数百个使用它的人中唯一的一个。 ADO.NET 或 F# 是否提供像
如果我们不想在我们的类中实现 init 方法,并且记住 NSObject 中的 init 只返回一个没有初始化的对象实例,如果我们已经得到了,我不明白调用 init 的意义带有分配的实例。我已经尝试过
我们的组织中有许多初级 Delphi 开发人员,作为向他们教授 Delphi 过程的一部分,我希望他们能够看到“干净”、编写良好、设计良好的 Delphi 代码。 我要寻找的一些标准包括: 优秀的类(
我有一个 3D 图像扫描(形状:335x306x306,总元素:31368060),我想用相同大小的 3D bool 掩码来掩盖它以返回相同大小的蒙版图像。 当我简单地用掩码索引数组时: masked
如何使适配器类适本地支持 const 和非 const 底层数据? 具体例子 RigidBody是描述对象物理属性的类。 这是其非常简化的版本(1D):- class RigidBody{ f
我是一名优秀的程序员,十分优秀!