我有这样的结构(类型被简化以延续这一点),生活在 std::vector:
struct Region {
int first;
int count;
struct Metadata region_metadata;
};
在 vector 中,它们按first排序。如果将first和count相加,则得到下一个区域的first;所以基本上这个结构 vector 描述了连续数字范围的元数据。
现在给定一个整数,我想查找元数据。对区域进行排序后,我可以使用 std::upper_bound。我是这样实现的:
struct Comp
{
inline bool operator()(const Region ®ion, int index) const
{
return region.first < index;
}
inline bool operator()(int index, const Region ®ion) const
{
return index < region.first;
}
};
这有效,当调用 std::upper_bound 时:
auto iter = std::upper_bound(m_regions.begin(),
m_regions.end(),
index,
Comp());
现在这恰好可以工作,因为 upper_bound 可以在内部选择符合其要求的重载,因为它同时调用 Comp()(Region, int) 和 Comp()(int, Region)(这就是 [](const Region ®, int index){…} 不起作用的原因)。
我实际上是通过在使用我之前提到的 lambda 时跟踪错误消息来提出解决方案的。 docs for std::upper_bound at cppreference.com写下第四个论点:
comparison function object (i.e. an object that satisfies the requirements of Compare) which returns true if the first argument is less than the second.
The signature of the comparison function should be equivalent to the following:
bool cmp(const Type1 &a, const Type2 &b);
The signature does not need to haveconst &, but the function object must not modify the objects passed to it. The typesType1andType2must be such that an object of typeTcan be implicitly converted to bothType1andType2, and an object of typeForwardItcan be dereferenced and then implicitly converted to bothType1andType2.The type
Type1must be such that an object of typeTcan be implicitly converted toType1. The typeType2must be such that an object of typeForwardItcan be dereferenced and then implicitly converted toType2.
(cppreference has been fixed 因为我发布了这个问题,谢谢@T.C.)
这里,T 是 std::upper_bound 的第三个参数,ForwardIt 是前两个参数的类型。这句话并没有提到函数对象实际上是一个结构,它重载了它的 operator() 以涵盖“正向”和“反向”情况。
那么在写成的规则中,这是合法的,还是我的特定编译器/标准库组合 (g++ 5.3.1) 的产物?
我对 C++14 或 C++17 的特定答案感兴趣。
完整示例:
#include <algorithm>
#include <iostream>
#include <vector>
struct Region {
Region(int first, int count, int n):
first(first),
count(count),
n(n)
{
}
int first;
int count;
int n; // think struct Metadata
};
struct Comp
{
inline bool operator()(const Region ®ion, int index) const
{
return region.first < index;
}
inline bool operator()(int index, const Region ®ion) const
{
return index < region.first;
}
};
int main() {
std::vector<Region> regions;
regions.emplace_back(0, 10, 1);
regions.emplace_back(10, 10, 2);
regions.emplace_back(20, 10, 3);
const int lookup = 10;
auto iter = std::upper_bound(
regions.begin(),
regions.end(),
lookup,
Comp());
// yes, I omitted error checking here, with error being iter == regions.begin()
std::cout << lookup << " is in region with n = " << (iter-1)->n << std::endl;
}
最佳答案
Now this happens to work, because
upper_boundcan internally pick the overload which matches its requirements, as it calls bothComp()(Region, int)andComp()(int, Region)(which is the reason why a[](const Region ®, int index){…}would not work).
不,upper_bound 只调用 Comp 的第二个重载。这正是您的(固定 - 感谢@T.C.!)引用的内容:比较器的第一个参数始终是 upper_bound 的第三个参数。应该交换 lambda 的参数。
在 upper_bound/lower_bound 的比较器中重载 operator() 本质上是没有意义的,因为这些算法只会选择一个重载.
operator() 应该像您在使用 equal_range 时显示的那样被重载,并且这样做是合法的,因为比较器(或任何仿函数)与库无关:您只需要确保排序是严格的(即正确的语义)并且重载是明确的。
关于c++ - 我可以合法地使用重载 operator() 的结构作为 std::upper_bound 的比较吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36007500/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的
我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法
我们的git存储库中目前有一个Gemfile。但是,有一个gem我只在我的环境中本地使用(我的团队不使用它)。为了使用它,我必须将它添加到我们的Gemfile中,但每次我checkout到我们的master/dev主分支时,由于与跟踪的gemfile冲突,我必须删除它。我想要的是类似Gemfile.local的东西,它将继承从Gemfile导入的gems,但也允许在那里导入新的gems以供使用只有我的机器。此文件将在.gitignore中被忽略。这可能吗? 最佳答案 设置BUNDLE_GEMFILE环境变量:BUNDLE_GEMFI
我喜欢使用Textile或Markdown为我的项目编写自述文件,但是当我生成RDoc时,自述文件被解释为RDoc并且看起来非常糟糕。有没有办法让RDoc通过RedCloth或BlueCloth而不是它自己的格式化程序运行文件?它可以配置为自动检测文件后缀的格式吗?(例如README.textile通过RedCloth运行,但README.mdown通过BlueCloth运行) 最佳答案 使用YARD直接代替RDoc将允许您包含Textile或Markdown文件,只要它们的文件后缀是合理的。我经常使用类似于以下Rake任务的东西:
我想让一个yaml对象引用另一个,如下所示:intro:"Hello,dearuser."registration:$introThanksforregistering!new_message:$introYouhaveanewmessage!上面的语法只是它如何工作的一个例子(这也是它在thiscpanmodule中的工作方式。)我正在使用标准的rubyyaml解析器。这可能吗? 最佳答案 一些yaml对象确实引用了其他对象:irb>require'yaml'#=>trueirb>str="hello"#=>"hello"ir
当谈到运行时自省(introspection)和动态代码生成时,我认为ruby没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资