草庐IT

less-than-functor

全部标签

c++ - 为什么 "greater than"不适用于 char 类型?

我正在OTB(一个C++库)中实现一个图像处理过滤器。我在进行一些非常基本的测试时出现了奇怪的行为。当我将图像读取为“char”时,以下代码始终输出像素值(在0-200范围内),即使它大于150。但是,当我使用“shortint”时它工作正常。“char”是否有特殊行为(例如字母比较而不是其数值)或者是否有任何其他原因?当然,我的图像像素是以字节存储的,所以我更喜欢处理“char”而不是“int”,因为图像非常大(>10Gb)。if(pixelvalue>150){out=255;}else{out=pixelvalue;} 最佳答案

c++ - 为什么这段代码会出现 "exception spec is more lax than base"错误?

尝试使用Xcode6.1中的clang版本(基于LLVM3.5svn的clang-600.0.54)编译以下代码,使用-std=c++11和-stdlib=libc++给我一些我不太明白的错误。#includestructImpl{typedefstd::functionL;Ll;inti;};structHndl{Impl*impl;Hndl(Impl*i):impl(i){}~Hndl()noexcept(false){}};intmain(intargc,char*argv[]){Hndlh(newImpl());h.impl->l=[=]{h.impl->i=42;};retu

c++ - std::less 枚举

标准是否保证std::less会订MyEnumType好像值为MyEnumType被转换为适当大小的整数类型?enumMyEnumType{E1=0,E2=6,E3=3}; 最佳答案 是的,std::less::operator()定义为(§20.8.5/5):operator()returnsx对于在枚举类型上使用关系运算符,声明如下(§5.9/2):Theusualarithmeticconversionsareperformedonoperandsofarithmeticorenumerationtype.对于无作用域的枚举类

python - 基准矩阵乘法性能 : C++ (eigen) is much slower than Python

我正在尝试评估与C++相比,Python的性能有多好。这是我的Python代码:a=np.random.rand(1000,1000)#typeisautomaicallyfloat64b=np.random.rand(1000,1000)c=np.empty((1000,1000),dtype='float64')%timeita.dot(b,out=c)#15.5ms±560µsperloop(mean±std.dev.of7runs,100loopseach)这是我在发布机制中使用Xcode编译的C++代码:#include#include#includeusingnamespa

c++ - Functor 的实例变量在对 generate_n 的连续调用之间不保留

我使用以下结构作为STL的generate_n算法的输入:structGenerateNumber{GenerateNumber():i(0){}intoperator()(void){returni++;}private:inti;};使用这个仿函数的代码示例是:std::vectorv1(3);std::vectorv2(3);GenerateNumbergenerateNumber;std::generate_n(v1.begin(),3,generateNumber);std::generate_n(v2.begin(),3,generateNumber);然而,结果是v1和v

c++ - "less than"ifstream 与 GCC 4 与 6 的比较

我偶然发现了这段代码:std::stringexport_str="/path/to/file";std::ofstreamexport(export_str.c_str());if(export这可以在GCC4.9.3上正常编译和运行,但在GCC6.1.1上会出现此错误:error:nomatchfor‘operator}’and‘int’)if(export我尝试使用GCC6:-std=c++98(编译)-std=c++03(编译)-std=c++11(不编译)编辑:但是,在GCC4中,它仍然使用-std=c++11进行编译。.这个具体事实也在下面的答案中解释。:)所以我猜这方面的

c++ - "more than one instance of overloaded function "标准::战俘 "matches the argument list"

使用C++,我尝试#defineTINYstd::pow(10,-10)我为定义了TINY的类(.h)提供了带有#include和命名空间信息的代码#pragmaonce#include"MMath.h"#include#include#includeusingnamespacestd;#defineTINYstd::pow(10,-10)我在.cpp文件中的一些函数实现中使用了TINY,而TINY给出了错误IntelliSense:morethanoneinstanceofoverloadedfunction"std::pow"matchestheargumentlist什么是正确的

c++ - map 在运行时选择 `std::greater` 或 `std::less`

这一行:std::map>current_book;我想用以下逻辑等价物替换它:intSide=...if(Side==1){std::map>current_book;}else{std::map>current_book;} 最佳答案 您可以为此使用std::function:usingmymap=std::map>;autom=Side?mymap(std::less()):mymap(std::greater());liveexample 关于c++-map在运行时选择`std:

c++ - std::greater<double> 和 std::less<double> 使用安全吗?

比较double时C++中的值,使用,=,!=运算符,我们不能总是确定结果的正确性。这就是为什么我们使用其他技术来比较doubles,例如,我们可以通过测试它们的差异是否真的接近于零来比较两个doublea和b。我的问题是,C++标准库是否实现了std::less和std::greater使用这些技术,还是仅使用不安全的比较运算符? 最佳答案 您可以100%确定这些运算符结果的正确性。只是先前的计算可能导致截断,因为double不是无限的。所以运算符非常好,只是您的操作数不是您期望的那样。因此,您使用什么进行比较并不重要。

c++ - std::set<K, C>::operator<(const std::set<K, C>&) 不使用 C() 但 std::less()

无法删除我自己的问题,所以改写它... 最佳答案 这实际上不是实现中的错误,尽管它可以说是标准中的错误:23.2.1Generalcontainerrequirements[container.requirements.general]13Table98listsoperationsthatareprovidedforsometypesofcontainersbutnotothers.Thosecontainersforwhichthelistedoperationsareprovidedshallimplementtheseman