我有一个无法修改的遗留类层次结构。由于外部库的要求,我需要为 Line 和 Ring 定义 Boost.Ranges,其中两者都只在一次运行中公开点(即对于 Line 和 Ring,它应该是一个 Boost.Range of Points) .
伪代码来说明:
Line l1 = Line{{1.0,2.0},{3.0,4.0},{5.0,6.0}} // init Line with three Points
Line l2 = Line{{7.0,8.0},{9.0,10.0},{11.0,12.0}} // init Line with three Points
auto lit = boost::begin(l1); // points to the Point{1.0,2.0}
++lit; // points to the Point{3.0,4.0}
Ring r1 = Ring{l1,l2} // init Ring with two Lines
auto rit = boost::begin(r1); // points to the Point{1.0,2.0}
++rit; // points to the Point{3.0,4.0}
++rit; // points to the Point{5.0,6.0}
++rit; // points to the Point{7.0,8.0}
++rit; // points to the Point{9.0,10.0}
// etc...
直线很简单,因为点是直接存储的(我已经用 Boost.Range see the example 成功地做到了这一点)。但是,我不知道如何使用 Ring 执行此操作,因为我需要直接到达每条线的点。
class Point
{
public:
double x, y;
}
class Line
{
public:
std::vector<Point> points;
}
class Ring
{
public:
std::vector<Line> lines;
}
最佳答案
您需要extend Boost.Range 以便它将 Ring 识别为有效范围。但在此之前,您需要定义一个自定义迭代器来展平 vector< vector<T> >。进入一维范围。
此示例使用 Method 2扩展 Boost.Range。它还使用 boost::iterator_facade 方便编写自定义迭代器,并假设迭代器只需要支持前向遍历。
#include <iostream>
#include <vector>
#include <boost/assign/std/vector.hpp> // for 'operator+=()'
#include <boost/foreach.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <boost/range.hpp>
struct Point
{
Point(double x, double y) : x(x), y(y) {}
double x, y;
};
struct Line {std::vector<Point> points;};
struct Ring {std::vector<Line> lines;};
/* Custom iterator type that flattens a 2D array into a 1D array */
template <class I, // Line iterator type
class R // Point reference type
>
class RingIteratorImpl : public boost::iterator_facade<
RingIteratorImpl<I,R>, Point, boost::forward_traversal_tag, R>
{
public:
RingIteratorImpl() : lineIter_(0), pointIndex_(0) {}
explicit RingIteratorImpl(I lineIter)
: lineIter_(lineIter), pointIndex_(0) {}
private:
friend class boost::iterator_core_access;
void increment()
{
++pointIndex_;
if (pointIndex_ >= lineIter_->points.size())
{
++lineIter_;
pointIndex_ = 0;
}
}
bool equal(const RingIteratorImpl& other) const
{
return (lineIter_ == other.lineIter_) &&
(pointIndex_ == other.pointIndex_);
}
R dereference() const {return lineIter_->points[pointIndex_];}
I lineIter_;
size_t pointIndex_;
};
typedef RingIteratorImpl<std::vector<Line>::iterator, Point&> RingIterator;
typedef RingIteratorImpl<std::vector<Line>::const_iterator, const Point&>
ConstRingIterator;
namespace boost
{
// Specialize metafunctions. We must include the range.hpp header.
// We must open the 'boost' namespace.
template <>
struct range_mutable_iterator<Ring> { typedef RingIterator type; };
template<>
struct range_const_iterator<Ring> { typedef ConstRingIterator type; };
} // namespace 'boost'
// The required Range functions. These should be defined in the same namespace
// as Ring.
inline RingIterator range_begin(Ring& r)
{return RingIterator(r.lines.begin());}
inline ConstRingIterator range_begin(const Ring& r)
{return ConstRingIterator(r.lines.begin());}
inline RingIterator range_end(Ring& r)
{return RingIterator(r.lines.end());}
inline ConstRingIterator range_end(const Ring& r)
{return ConstRingIterator(r.lines.end());}
int main()
{
Line l1, l2;
Ring ring;
{
using namespace boost::assign; // bring 'operator+=()' into scope
typedef Point P;
l1.points += P(1.1,1.2), P(1.3,1.4), P(1.5,1.6);
l2.points += P(2.1,2.2), P(2.3,2.4), P(2.5,2.6);
ring.lines += l1, l2;
}
// Boost Foreach treats ring as a Boost Range.
BOOST_FOREACH(Point p, ring)
{
std::cout << "(" << p.x << ", " << p.y << ") ";
}
std::cout << "\n";
}
我得到以下输出:
(1.1, 1.2) (1.3, 1.4) (1.5, 1.6) (2.1, 2.2) (2.3, 2.4) (2.5, 2.6)
关于c++ - 如何创建一个隐藏多层 vector 并将其公开为单个范围的 Boost.Range?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9251537/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何