Using a Comparison Function for the Key Type
(这是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的更多相关文章
- Effective STL 学习笔记 Item 21:Comparison Function 相关
Effective STL 学习笔记 Item 21:Comparison Function 相关 */--> div.org-src-container { font-size: 85%; f ...
- 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 ...
- ssh:no matching host key type found. Their offer: ssh-dss
最近突然ssh 服务连接出现 no matching host key type found. Their offer: ssh-dss 以前一直没有问题 可能的原因 openssh 服务升级,加密算 ...
- 配置 git公钥报错:unknown key type -rsa
配置 git公钥的时候出现:ssh-keygen unknown key type -rsa 一个解决办法是去本地寻找.ssh文件,参考路径(C:\Users\Administrator.ssh),把 ...
- ES5 function & ES6 class & method type
ES5 function & ES6 class & method type ES5 function "use strict"; /** * * @author ...
- 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 ...
- 记一次使用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 ...
- 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版本然后 ...
- Function overloading and return type
In C++ and Java, functions can not be overloaded if they differ only in the return type. For example ...
随机推荐
- 关于eclipse入门开发c/c++文章推荐
1. 关于编译说明. http://www.ibm.com/developerworks/cn/linux/opensource/os-ecc/ 2. 关于快捷键与代码阅读 http://www.cn ...
- sublime修改字体大小
/Packages/Theme\ -\ Default/Default.sublime-theme { "class": "label_control", , ...
- 对于a标签点击之后可以发邮件和打电话的功能实现
<ul> <li><i class="phone"></i><a href="tel:021-69976089&qu ...
- redmine Windows装配
原文:http://www.myexception.cn/windows/1219064.html redmine Windows安装 官方地址:http://www.redmine.org/ 所需环 ...
- Linux 字符集转化
命令行"iconv --list" 查看Linux操作系统支持的字符集 iconv_open 设置字符集转化 --iconv_t iconv_open(const char *to ...
- php基础26:文件与目录1
<meta charset="utf-8"> <?php //绝对路径 $path = "E:\AppServ\www\php\/33-catalog. ...
- C#实现对指定文件夹中文件按修改时间排序
string path = "~/Document/Introduction/团队管理制度/"; DirectoryInfo dirinfo = new Di ...
- navigator.sendBeancon方法简介
之所以介绍这个还在草案中的方法,是源于最近新做的一个活动.该活动有个需求,就是用户离开该页面的某个时间段之后,发个请求给该用户送券.后来是通过setTimeout来做的,用户离开该页面,该页面进入后台 ...
- Linux常用指令---工作
查看所有用户cat /etc/passwd 复制整个目录cp -ri A/B/* A1/B1/ 若复制过程中询问是否覆盖,输入y按回车 另外若A A1不在同一目录下,最好填绝对路径,就是/xxx/xx ...
- [CareerCup] 11.4 Sort the File 文件排序
11.4 Imagine you have a 20 GB file with one string per line. Explain how you would sort the file. 这道 ...