multiset 自定义比较函数

#include <string>
#include <iostream>
#include <set>
#include <memory>

using std::shared_ptr;
using std::multiset;
using std::string;
using std::cout;
using std::endl;

class MS{
    bool compareIsbn(const shared_ptr<string> &lhs,const shared_ptr<string> &rhs)
    {
        return *lhs < *rhs;
    }
    multiset<shared_ptr<string>, decltype(compareIsbn)*> item(compareIsbn);
};
int main(){ MS bbb;} 

上面这一段代码在g++ 和 vs2015下都无法编译,错误提示为:

error:compareIsbn 不是类型名

调试了几个小时,推测有两个主要的原因:

  • 非静态成员函数compareIsbn不能作为自定义比较函数
  • 静态成员函数无法作为copy constructor参数,如果想使用静态成员函数作为比较函数,需要使用braced Initialization形式。
    附修改后的正确代码(只列出修改部分):
static bool compareIsbn(const shared_ptr<string> &lhs,const shared_ptr<string> &rhs)
    {
        return *lhs < *rhs;
    }
multiset<shared_ptr<string>, decltype(compareIsbn)*> item{compareIsbn};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 再读高效c++,颇有收获,现将高效c++中的经典分享如下,希望对你有所帮助。 1、尽量以const \enum\i...
    橙小汁阅读 4,983评论 0 1
  • 接着上节 condition_varible ,本节主要介绍future的内容,练习代码地址。本文参考http:/...
    jorion阅读 14,983评论 1 5
  • C++运算符重载-下篇 本章内容:1. 运算符重载的概述2. 重载算术运算符3. 重载按位运算符和二元逻辑运算符4...
    Haley_2013阅读 5,320评论 0 49
  • 1. 让自己习惯C++ 条款01:视C++为一个语言联邦 为了更好的理解C++,我们将C++分解为四个主要次语言:...
    Mr希灵阅读 7,926评论 0 13
  • 我想解除跟Jade的位置关系。 我发现我很不喜欢被人不尊重被如此粗鲁对待。 不管她是我的学生关系,哪怕是任何人对我...
    校准中阅读 1,229评论 0 0

友情链接更多精彩内容