草庐IT

owner_less

全部标签

c++ - hash_map : why it defines less, 而不是 equal_to

C++,使用VisualStudio2010。关于为什么hash_map的用户定义特征的问题实际上需要总排序。我有一个简单的结构,比如说FOO,它只有一些整数。我想使用hash_map,这是一个哈希表,其键无序,用于存储FOO的结构。.我只需要快速搜索它的关联值,所以这是一个正确的选择:hash_map.但是,我需要为FOO实现自己的哈希函数和一些比较函数.这是hash_map的定义,摘自MSDN:template>,classAllocator=allocator>>classhash_map原来我需要实现hash_compare仿函数:template>classhash_comp

c++ - SFINAE 检查 std::less 是否有效

在我的代码中,如果一个对象小于另一个对象,我希望一个操作先于另一个操作发生。但是,如果类型不可比较,则顺序无关紧要。为此,我尝试使用SFINAE:template>()(std::declval(),std::declval()))>boolComparableAndLessThan(constT&lhs,constT&rhs){returnstd::less()(lhs,rhs);}boolComparableAndLessThan(...){returnfalse;}structfoo{};intmain(){fooa,b;if(ComparableAndLessThan(a,b)

c++ - std::less<> 不适用于我的 std::map

我想用我自己的结构“Point2”作为键创建一个map,但是我收到错误并且我不知道是什么导致了它,因为我为Point2结构声明了一个“operator代码:std::mapm_Props_m;std::mapm_Orders;structPoint2{unsignedintPoint2::x;unsignedintPoint2::y;Point2&Point2::operator=(constPoint2&b){if(this!=&b){x=b.x;y=b.y;}return*this;}boolPoint2::operator==(constPoint2&b){return(x==b

c++ - 为什么 C++ STL 容器使用 "less than"operator< 而不是 "equal equal"operator== 作为比较器?

在std::map的自定义类中实现比较运算符时,我遇到了这个问题,但看不到任何被问到的地方。除了上述问题,也有兴趣简要了解,如何operator适用于std::map.问题来源:structAddress{longm_IPv4Address;boolisTCP;booloperator 最佳答案 std::map需要能够排序。默认情况下使用std::less,对于非指针使用1。使用您对用户的要求最少的规则,它从综合“等价”当它需要它时(!(a表示a和b是等价的,即两者都不小于另一个)。这使得编写用作map的关键组件的类变得更加容易,

c++ - std::map unique std::less<> 函数,用于 2D 点作为键

好吧,经过四个小时的调试,尽管我很困惑,但我找到了问题的原因......我正在制作一些程序,在std::map中保存一些点并在我的窗口中呈现这些点。但奇怪的是,有些点未能进入map。std::mapm_Props_m;voidAddProp(std::pairp){m_Props_m.insert(p);}structPoint2{unsignedintPoint2::x;unsignedintPoint2::y;//--------Point2::Point2():x(0),y(0){}boolPoint2::operator(constPoint2&b)const{return(x

c++ - 什么是 "template<class T> using owner = T;"?

以下摘自Microsoft的gsl库(https://github.com/microsoft/gsl)的gsl.h:namespacegsl{////GSL.owner:ownershippointers//usingstd::unique_ptr;usingstd::shared_ptr;templateusingowner=T;...};我无法理解以下别名模板的含义:templateusingowner=T;有什么解释吗? 最佳答案 这意味着对于每个T,owner是T的别名. 关于

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.对于无作用域的枚举类

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++ - 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不是无限的。所以运算符非常好,只是您的操作数不是您期望的那样。因此,您使用什么进行比较并不重要。