草庐IT

less-than-functor

全部标签

c++ - Lint 更喜欢 std :string rather than clear() 上的 erase() 方法

使用Lint,它反复反对在std字符串上使用clear,并希望建议使用不带参数的删除方法。查看文档并执行我想要的操作,即将字符串的大小调整为零个元素,以便该字符串为空且没有任何元素。此时我不想删除字符串,只需从中删除所有成员即可。我的问题是这两种方法有什么区别,有谁知道我应该使用erase而不是clear方法的原因吗? 最佳答案 我看不出任何原因。恰恰相反:我实际上不得不查看引用以查看erase()是否可以在没有参数的情况下调用。erase()用于从字符串中删除一些指定的字符。clear()用于删除字符串的全部内容(但不包括其容量)

c++ - 在 switch 语句中使用类类型 : is it better than using typeid operator?

我在下面看到了有关C++标准$6.4.2中switch语句的内容。Switch语句可以带一个条件。Theconditionshallbeofintegraltype,enumerationtype,orofaclasstypeforwhichasingleconversionfunctiontointegralorenumerationtypeexists(12.3).Iftheconditionisofclasstype,theconditionisconvertedbycallingthatconversionfunction,andtheresultoftheconversion

c++ - std::less<int> 的正确参数类型是什么?

我正在制作一个类——一个BST——它可以比较模板化节点,这需要一个比较器,例如std::less。树是这样的:templateclasstree{private:comparatorcompare;public:explicittree(comparatorfunctor);};但我似乎找不到应该在我的应用程序中输入哪种模板类型。treemy_bst(std::less);error:wrongnumberoftemplatearguments(1,shouldbe2)bst::treemy_bst(std::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++ - 'more than one instance of overloaded function "sqrt“匹配参数列表”怎么办?

我的代码在for循环中出错,for(j=3;j:morethanoneinstanceofoverloadedfunction"sqrt"matchestheargumentlist.我该如何解决?#include//determineifnumberisprimeboolisPrime(longn){intj,num=0;{if(num 最佳答案 尝试:for(j=3;j(num));j+=2)发生的事情是包含3个不同的definitionsofsqrt并且编译器不知道您要使用哪个。

c++ - 警告 C4114 : same type qualifier used more than once

在将VC++6.0开发的代码迁移到VisualStudio2008时,我在代码的下面一行中收到此警告。constintconstCImportContext::PACKETSIZE=4096;我知道如何修复指针staticconstintconst*PACKETSIZE;//C4114staticconstint*constPACKETSIZE;//Correct但我的问题是如何解决这个警告,如果它像下面的警告(没有指针),staticconstintconstPACKETSIZE; 最佳答案 指针有两种不同的const限定符是有意

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