functor

I thought it would be easy and convenient to define a small functor and perform a customized sort on some objects I have. However, I can‘t seem to get the functor to work. Here is some code that gives me a "no matching call" error:

class Cmp
{
    bool operator()(int x, int y) const
    {
        return x < y;
    }
};

vector<int> vec;
std::sort(vec.begin(), vec.end(), Cmp() );

As far as I can tell I followed the requirements exactly, but apparently I didn‘t. Can anyone tell me what is wrong with the above code? Thanks for your time.

时间: 2024-08-04 14:10:32

functor的相关文章

Functor仿函数

转载声明:本文转自网络,稍加整理以备学习和参考之用. 函数对象/仿函数 提到C++ STL,首先被人想到的是它的三大组件:Containers, Iterators, Algorithms,即容器,迭代器和算法.容器为用户提供了常用的数据结构,算法大多是独立于容器的常用的基本算法,迭代器是由容器提供的一种接口,算法通过迭代器来操控容器.接下来要介绍的是另外的一种组件,函数对象(Function Object,JJHou译作Functor仿函数). 什么是函数对象 顾名思义,函数对象首先是一个对象

C++ STL 学习 :for_each与仿函数(functor)

简单来将,仿函数(functor)就是一个重载了"()"运算符的struct或class,利用对象支持operator()的特性,来达到模拟函数调用效果的技术. 我们平时对一个集合类遍历的时候,例如vector,是这样做的: for(vector<int>::const_iterator iter = ivec.begin(); iter != ivec.end(); ++iter) { //do your whatever you want here } 例如下面的代码:

C++仿函数(functor)详解

C++仿函数(functor)详解 所谓的仿函数(functor),是通过重载()运算符模拟函数形为的类. 因此,这里需要明确两点: 1 仿函数不是函数,它是个类: 2 仿函数重载了()运算符,使得它的对你可以像函数那样子调用(代码的形式好像是在调用 函数). 看下面的实例: #include <iostream> using namespace std; const int CMP_LES = -1; const int CMP_EQU = 0; const int CMP_BIG = 1;

[Javascript] Either Functor

Either Functor: // API Right(val) // resolve the value Left(val) // return error message Examples: map(function(x) { return x + 1; }, Right(2)) //=> Right(3) map(function(x) { return x + 1; }, Left(‘some message)) //=> Left(‘some message’) var deter

泛函编程(26)-泛函数据类型-Monad-Applicative Functor Traversal

前面我们讨论了Applicative.Applicative 就是某种Functor,因为我们可以用map2来实现map,所以Applicative可以map,就是Functor,叫做Applicative Functor.我们又说所有Monad都是Applicative,因为我们可以用flatMap来实现map2,但不是所有数据类型的flatMap都可以用map2实现,所以反之不是所有Applicative都是Monad.Applicative注重于各种类型的函数施用,也就是map.包括普通函

Haskell抽象概念Functor剖析

在理解Functor之前,必须对抽象代数的范畴论有所了解,有范畴论的知识作为铺垫,感觉Functor也不是那么的难以理解了. 一个范畴C包括: 一个由对象所构成的类ob(C) 对象之间的态射所构成的类hom(C).每一个态射f都会有一个"源对象"a和"目标对象"b,且a和b都在ob(C)之内.因此写成f:a -> b,且称f为由a到b的态射.所有a到b的态射所构成的"态射类",其标记为hom(a,b)或者homC(a,b). 对三个对象a.

浅谈haskell中functor typeclass和普通typeclasses的区别

其实这个区别就好像普通函数和高阶函数的区别一样.这样是不是很好理解了呢,额,如果你说你还不知道啥是高阶函数,那么还是不要看这个文章了.下面来看看我是如何把他们类比起来的. 我们看看haskell中的Eq是如何定义的,这个我把它叫"普通typeclasses"(为了区分functor typeclasses,我就这么叫它了:P),这里定义了一个typeclasses并且在这个typeclasses里面定义了一个行为,普遍的说法就是你可以把这个typeclasses想象成java的inte

[Javascript] Functor law

Functor laws: 1. Identity: map(id) == id 2. Composition: compose(map(f), map(g)) == map(compose(f,g)) compose( map(toUpper), map(reverse), toArray )("bingo"); compose( map( compose(toUpper reverse), toArray ) )("bingo") Natural Transfo

[Javascript] Maybe Functor

In normal Javascript, we do undefine check or null check: var person = {age: 14, name: "Suvi"}; var name = person.name ? person.name: null; Sometime backend data return may contain or not contain 'name' prop. So let's see how to define a Maybe()

[Javascript] Functor Basic Intro

Well, this stuff will be a little bit strange if you deal with it first time. Container Object: Just a wrapper / contianer for values No Method No Nouns var _Container = function(val){ this.val = val; } var Container = function(x){ return new _Contai