(这是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. request模块提交数据

    http://ctf8.shiyanbar.com/jia/ #coding:utf-8import re,requestsurl = r"http://ctf8.shiyanbar.com ...

  2. Mac上安装node.js

    1.下载node for mac并一路默认安装 2.测试成功否 3.copy this file to test(save as javascript file) var http = require ...

  3. 应用python编写简单新浪微博应用(一)

    转载至:http://blog.sina.com.cn/s/blog_6c39196501016o7n.html 首先,你要有一个新浪微博账号. 申请页面:http://weibo.com 其次,你要 ...

  4. cocoaPod相关问题

    cocoap简介: 1. 简介 CocoaPods是一个负责管理iOS项目中第三方开源代码的工具,其源码在Github上开源.使用CocoaPods可以节省设置和更新第三方开源库的时间并提高工作效率. ...

  5. Java反射机制的学习

    Java反射机制是Java语言被视为准动态语言的关键性质.Java反射机制的核心就是允许在运行时通过Java Reflection APIs来取得已知名字的class类的相关信息,动态地生成此类,并调 ...

  6. 未能解析此远程名称:'nuget.org' 的解决方法

    今天用Nuget下一个程序包时,发现Nuget挂了: 未能解析此远程名称:'nuget.org' . 浏览器打开  http://nuget.org  失败. 使用cmd命令 输入nslookup n ...

  7. [android界面]android中src和background区别——前景与背景

    ImageView中XML属性src和background的区别: background会根据ImageView组件给定的长宽进行拉伸,而src就存放的是原图的大小,不会进行拉伸.src是图片内容(前 ...

  8. 关于个人博客和Github地址提交

    请大家尽快按照http://www.cnblogs.com/SivilTaram/p/5857858.html的要求提交个人博客和Github地址.谢谢!

  9. 20145208 《Java程序设计》第7周学习总结

    20145208 <Java程序设计>第7周学习总结 教材学习内容总结 Lambda 认识Lambda语法 什么是Lambda语法 以下是维基百科上的解释: a function (or ...

  10. Jenkins入门系列之——00答疑解惑

    写在最前的总结:Jenkins其实就是一个工具,这个工具的作用就是调用各种其他的工具来达成你的目的.比如你要获取Subversion上最新的源代码,Jenkins会去调用SVNKIT(插件的核心Jar ...