(这是C++系列随笔的第二篇,这一系列是我练习C++而查的资料)

extracted from C++ Primer 5th. edition pp. 425


Using a Comparison for the Key Type

The type of the operation that a container uses to organize its elements is part of the type of that container. To specify our own operation, we must supply the type of that operation when we define the type of an associative container (including prioroty_queue). The operation type is specified following the element type inside the angle brackets that we use to say which type of container we are defining. (We are actually required to define a parameter type rather than to pass an argument.)

Each type inside the angle brackets is just that, a type (please pay attention to this sentence). We supply a particular comparison operation (more generally, a callable object: a pointer to function or a function object) of the specified type (that must have the same type as specified inside the angle brackets, and conversely we msut specify the type inside the angle brackets the same type as the constructor argument we are going to and allowed to supply (from outside? sounds subtle...)) when we create a container.

(C++ Primer, 5th. Ed. pp. 249)


Related Topic:

Function Pointer Parameters

Just as with arrays, we cannot define parameters of function type but can have a parameter that is a pointer to function. As with arrays, we can write a parameter that looks like a function type (when declaring or defining a function), but it will be treated as a pointer:

//third parameter is a function type and is automatically treated as a pointer to function
void useBIgger(const string &s1, const string &s2,
bool pf(const string &, const string &));
//equivalent declaration: explicitly define the parameter as a pointer to function
void useBIgger(const string &s1, const string &s2,
bool (*pf)(const string &, const string &));

However we cannot do so when defining the type of an associative container (a parameter that looks like a functoin type will not be treated as a pointer, and thus illegal):

//error
priority_queue<P, vector<P>, bool (const P&, const P&)> que(cmp);
//OK
priority_queue<P, vector<P>, bool (*) (const P&, const P&)> que(cmp);
//a simplified declaration using decltype
priority_queue<P, vector<P>, decltype(cmp)*> que(cmp);

Using a Comparison Function for the Key Type的更多相关文章

  1. Effective STL 学习笔记 Item 21:Comparison Function 相关

    Effective STL 学习笔记 Item 21:Comparison Function 相关 */--> div.org-src-container { font-size: 85%; f ...

  2. c++ how to make your own class a valid key type for std::map?

    In Java, if you want your own class to be a valid key type of the container, you just need to make i ...

  3. ssh:no matching host key type found. Their offer: ssh-dss

    最近突然ssh 服务连接出现 no matching host key type found. Their offer: ssh-dss 以前一直没有问题 可能的原因 openssh 服务升级,加密算 ...

  4. 配置 git公钥报错:unknown key type -rsa

    配置 git公钥的时候出现:ssh-keygen unknown key type -rsa 一个解决办法是去本地寻找.ssh文件,参考路径(C:\Users\Administrator.ssh),把 ...

  5. ES5 function & ES6 class & method type

    ES5 function & ES6 class & method type ES5 function "use strict"; /** * * @author ...

  6. Unable to negotiate with xx.xxx.xxxx port 22: no matching host key type found. Their offer: ssh-rsa(解决的两种方式)

    异常问题: 下班之前升级了一下Git的版本,结果第二天过来拉取远程最新代码的时候就提示了下面的异常问题: Unable to negotiate with xx.xxx.xxxx port 22: n ...

  7. 记一次使用git报错,解决Unable to negotiate with **** port 22: no matching host key type found. Their offer: ssh-rsa

    windows电脑重装系统,去官网下载了最新的git安装,一路next下来,打开bash按老路子设置,生成公钥 git config --global user.name "yourname ...

  8. Visual Studio 2022 git error Unable to negotiate with xx.xxx.xxxx port 22: no matching host key type found. Their offer: ssh-rsa

    前言 前两天因为升级了Git导致git提交拉取的时候都提示下面这个异常,然后经过一番折腾以后终于把这个问题解决了.但是今天我升级了下Visual Studio 2022将其升级到了17.1.3版本然后 ...

  9. Function overloading and return type

    In C++ and Java, functions can not be overloaded if they differ only in the return type. For example ...

随机推荐

  1. git 添加文件

    git 添加文件三步骤 git add filename git commit -m 'remarks' git push origin master

  2. vuejs使用技巧

    组件创建自定义标签时,自定义的标签不要用驼峰写法,vue否者报错,例如<my-template></my-template>合法或者去掉中间的斜杠全部小写,只要出现大写字母就会 ...

  3. 课程2——变量修饰关键字

    声明:本系列随笔主要用于记录c语言的常备知识点,不能保证所有知识正确性,欢迎大家阅读.学习.批评.指正!!你们的鼓励是我前进的动力.严禁用于私人目的.转载请注明出处:http://www.cnblog ...

  4. Protocol Buffer多态

    java中有多态的概念,protobuf本身没有多态的概念,但是它有一个扩展的概念. 以聊天消息为例,先看下面这个类图,基类是ChatMessage,子类TextMessage和ImageMessag ...

  5. Java NIO框架Mina、Netty、Grizzly介绍与对比(zz)

    Mina:Mina(Multipurpose Infrastructure for Network Applications) 是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络应用 ...

  6. 【MySQL】10条SQL优化语句,让你的MySQL数据库跑得更快!

    慢SQL消耗了70%~90%的数据库CPU资源: SQL语句独立于程序设计逻辑,相对于对程序源代码的优化,对SQL语句的优化在时间成本和风险上的代价都很低: SQL语句可以有不同的写法: 1 不使用子 ...

  7. 关于git托管的一些心得

    GIT托管的一些心得 熟练运用软件进行GIT托管的好处 在上一周的学习中,我提出来了一个疑惑,就是为什么一定要用软件托管而不选择web托管,在这周的学习中,我通过实践体会到了一些运用软件托管的好处: ...

  8. 小白安装linux(虚拟机)red hat enterprise linux 6

    额,这篇貌似是我名义上的第一篇博客,但是我好像没有第一写他,没事,都一样.(我会假装它是人生中第一篇博客的) 上大学之后,很久之后才发现自己听喜欢linux的,因为感觉很高大上,所以自己自学了很多关于 ...

  9. 高校手机签到系统——zxing.net生成二维码(补充)

    高校手机签到系统——第一部分Authority权限系统(上) 高校手机签到系统——第一部分Authority权限系统(下) 高校手机签到系统——手机客户端 关于zxing.net的使用网上已有很多说明 ...

  10. Android开发者资源大汇总

    本文总结了最新的Android开发资源.下面列出的资源都是常用的,每个Android程序员都应该知道,能大大方便App开发.Enjoy~ 来源:Android开发周刊 中文的Android开发信息,资 ...