(这是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. NSURLSession学习笔记

    NSURLSession学习笔记(一)简介 一.URL Session的基本概念 1.三种工作模式: 默认会话模式(default):工作模式类似于原来的NSURLConnection,使用的是基于磁 ...

  2. Android应用开发中如何使用隐藏API(转)

    一开始需要说明的是,Google之所以要将一些API隐藏(指加上@hide标记的public类.方法或常量)是有原因的.其中很大的原因就是Android系统本身还在不断的进化发展中.从1.0.1.1到 ...

  3. usb驱动开发1之学习准备

    此系列是http://blog.csdn.net/fudan_abc/博文的整理,同时加入了自己的理解.很敬佩fudan_abc的文章,仔细学习和分析受益很多.注:fundan_abc所分析linux ...

  4. 传递消息--第三方开源--EventBus的简单使用

    EventBus下载地址:https://github.com/greenrobot/EventBus MyEvent: package com.zzw.testeventbus; public cl ...

  5. C++ 栈的基本操作

    // zhan.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using namesp ...

  6. 三层ViewPager嵌套 的事件处理

    这么多ViewPager嵌套在一起肯定会遇到冲突 不信你试试(笑脸) 下面来说怎么解决.....太为难我这个菜b了 设置外部的父控件不要拦截我子控件的事件,通过重写ViewPager的 @Overri ...

  7. Spring如何处理线程并发

    Spring如何处理线程并发   我们知道Spring通过各种DAO模板类降低了开发者使用各种数据持久技术的难度.这些模板类都是线程安全的,也就是说,多个DAO可以复用同一个模板实例而不会发生冲突.我 ...

  8. Eclipse系列: Eclipse设置Tomcat启动超时时间

    在eclipse的workspace目录下,找到如下文件: .metadata\.plugins\org.eclipse.wst.server.core\servers.xml 如下图所示,然后将它修 ...

  9. [CareerCup] 13.10 Allocate a 2D Array 分配一个二维数组

    13.10 Write a function in C called my2DAlloc which allocates a two-dimensional array. Minimize the n ...

  10. 20145215《Java程序设计》实验一实验报告

    实验一 Java开发环境的熟悉 实验内容及步骤 使用JDK编译.运行简单的Java程序 命令行下程序开发: 在命令行下建立实验目录,进入该目录后创建exp1目录 敲入以下代码: package exp ...