c/c++ 重載運算符 函數調用運算符

重載運算符 函數調用運算符

把一個類的對象a,當成函數來使用,比如a(),所以需要重載operator()方法。重載了函數調用運算符的類的對象,就是函數對象了。

還有什么是函數對象呢???

  • lambda是函數對象
  • std::bind函數的返回值是函數對象
  • 函數是函數對象
  • 函數指針是函數對象

函數對象是做什么用的呢???

  • 在標準算法中使用,比如std::sort(b, e, 函數對象);

標準庫提供了下面的函數對象,它們都是模板形式的,它們放在functional頭文件中

算術關系邏輯
plus<Type>equal_to<Type>logical_and<Type>
minus<Type>not_equal_to<Type>logical_or<Type>
multiplies<Type>greater<Type>logical_not<Type>
divides<Type>greater_equal<Type>
modulus<Type>less<Type>
negate<Type>less_equal<Type>

例子:

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <functional>class LineStr{
public:LineStr(std::istream& in = std::cin) : is(in){}std::string operator()(){std::string str;std::getline(is, str);return is ? str : std::string();}
private:std::istream& is;
};class Isequ{
public:Isequ(int i = 0) : val(i){}bool operator()(int t){return val == t;}
private:int val;
};class StableSort{
public:bool operator()(const std::string& a, const std::string& b){return a.size() < b.size();}
};class SizeCmp{
public:SizeCmp(std::size_t s) : sz(s){}bool operator()(const std::string& str)const{return str.size() > sz;}
private:std::size_t sz;
};int main(){/*LineStr ls;std::cout << ls() << std::endl;*//*std::vector<int> vi{23,3,5,6,78,3};Isequ iq(3);std::replace_if(vi.begin(), vi.end(), iq, 9);for(int i : vi)std::cout << i << " ";std::cout << std::endl;*//*std::vector<std::string> vs{"1234", "123", "a", "bc"};//stable_sort(vs.begin(), vs.end(), [](const std::string& a,//                   const std::string& b){//        return a.size() < b.size();//          });//std::stable_sort(vs.begin(), vs.end(), StableSort());StableSort ss;std::stable_sort(vs.begin(), vs.end(), ss);std::size_t sz = 2;//auto b = std::find_if(vs.cbegin(), vs.cend(), [sz](const std::string& a){//    return a.size() > sz;//  });SizeCmp sc(3);auto b = std::find_if(vs.cbegin(), vs.cend(), sc);for_each(b, vs.cend(), [](const std::string& s){std::cout << s << " ";});std::cout << std::endl;for(auto s : vs)std::cout << s << " ";std::cout << std::endl;*/using std::placeholders::_1;std::vector<int> iv {12,213,123123,434344,213232};int cnt = std::count_if(iv.cbegin(), iv.cend(),std::bind(std::greater<int>(), _1, 1024));std::cout << cnt << std::endl;std::vector<std::string> sv{"pooh", "pooh", "11","pooh","22"};auto idx = std::find_if(sv.cbegin(), sv.cend(),std::bind(std::not_equal_to<std::string>(),_1, "pooh"));std::cout << *idx << std::endl;std::vector<int> iv2 {12,3,12,4,21};std::transform(iv2.cbegin(), iv2.cend(), iv2.begin(),std::bind(std::multiplies<int>(), _1, 2));for(auto i : iv2)std::cout << i << " ";std::cout << std::endl;}

github

c/c++ 學習互助QQ群:877684253

1414315-20181106214320230-961379709.jpg

本人微信:xiaoshitou5854

轉載于:https://www.cnblogs.com/xiaoshiwang/p/10176824.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/395614.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/395614.shtml
英文地址,請注明出處:http://en.pswp.cn/news/395614.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

matlab 萬能,matlab 萬能實用的線性曲線擬合方法

在科學計算和工程應用中&#xff0c;經常會遇到需要擬合一系列的離散數據&#xff0c;最近找了很多相關的文章方法&#xff0c;在這里進行總結一下其中最完整、幾乎能解決所有離散參數線性擬合的方法第一步&#xff1a;得到散點數據根據你的實際問題得到一系列的散點例如&#…

socket websocket

1.websocket客戶端 websocket允許通過JavaScript建立與遠程服務器的連接&#xff0c;從而實現客戶端與服務器間雙向的通信。在websocket中有兩個方法&#xff1a;      1、send() 向遠程服務器發送數據    2、close() 關閉該websocket鏈接  websocket同時還定義了幾…

javascript原型_JavaScript的原型:古怪,但這是它的工作原理

javascript原型by Pranav Jindal通過普拉納夫金達爾 JavaScript的原型&#xff1a;古怪&#xff0c;但這是它的工作原理 (Prototype in JavaScript: it’s quirky, but here’s how it works) The following four lines are enough to confuse most JavaScript developers:以下…

mysql函數之SUBSTRING_INDEX(str,/,-1)

SUBSTRING_INDEX的用法&#xff1a; ?SUBSTRING_INDEX(str,delim,count) 在定界符 delim 以及count 出現前&#xff0c;從字符串str返回自字符串。若count為正值,則返回最終定界符(從左邊開始) 若為-1則是從后往前截取 SELECT substring_index(Hn_P00001, P, -1) -- 結果是…

mysql8.0主從配置,MySQL 8.0主從服務器(Master-Slave)配置

一、介紹MySQL 主從復制的方式有多種&#xff0c;本文主要演示基于基于日志(binlog)的主從復制方式。MySQL 主從復制(也稱 A/B 復制) 的原理&#xff1a;Master將數據改變記錄到二進制日志(binary log)中&#xff0c;也就是配置文件log-bin指定的文件&#xff0c; 這些記錄叫做…

第十二章 Shell腳本編寫及常見面試題(三)

本章目錄&#xff1a;12.21 FTP下載文件#!/bin/bash if [ $# -ne 1 ]; thenecho "Usage: $0 filename" fi dir$(dirname $1) file$(basename $1) ftp -n -v << EOF # -n 自動登錄 open 192.168.1.10 user admin adminpass binary # 設置ftp傳輸模式為二進制…

亞馬遜面試有幾輪_經過幾個月的Google面試準備,我被亞馬遜錄用

亞馬遜面試有幾輪by Googley as Heck由Googley飾演Heck 經過幾個月的Google面試準備&#xff0c;我被亞馬遜錄用 (After months of preparing for the Google interview, I got hired by Amazon) As you may know, the last 11 months have been very difficult for me. As a …

省選前的考試記錄

日拱一卒功不唐捐 什么沙雕玩意兒 2018.12.24 T1 如果對 \(A\) 數組求出來高度遞減的單調棧的話&#xff0c;會發現只有單調棧里的元素是有用的。因為如果有 \(A[i]<A[j] \And i<j\)&#xff0c;那電梯就可以在帶 \(j\) 上樓的時候順便把 \(i\) 帶上并不會影響結果。所以…

軟件工程課設-----日程管理系統

這學期進行了軟件工程課設&#xff0c;題目是&#xff1a;日程管理系統&#xff08;JavaWeb&#xff09;&#xff0c;為期3周。這三周只有前兩天是企業老師講解是企業老師講解相關的基礎知識(老師講的水平實在是不可恭維。。。。。。)。 多的不多說。直接進行對相關項目的介紹。…

matlab中的神經網絡訓練,MATLAB中的神經網絡訓練

我試圖向前饋送反向傳播&#xff0c;但是在網絡訓練之后&#xff0c;當模擬和打印模擬輸出時&#xff0c;我看不到任何靠近目標的值&#xff0c;但它只是一個數字。代碼如下。什么是錯&#xff0c;什么是問題&#xff1f;前饋反向傳播&#xff1a;>> load(E:/Inputdata.t…

Spring For All 頂級Spring綜合社區服務平臺

Spring For All 玩最純粹的技術&#xff01;做最專業的 Spring 民間組織~ 歡迎加入&#xff1a;http://spring4all.com/ image.png

chromium 桌面_如何使用Chromium和PyInstaller將Web應用程序轉換為桌面應用程序

chromium 桌面Packaging and distributing your app sounds simple in principle. It’s just software. But in practice, it’s quite challenging.打包和分發應用程序在原理上聽起來很簡單。 這只是軟件。 但是在實踐中&#xff0c;這非常具有挑戰性。 I’ve been working …

PHP面向對象(三)

一、繼承概念 繼承性也是面向對象程序設計中的重要特性之一。它是指建立一個新的派生類&#xff0c;從一個先前定義的類中繼承數據和函數&#xff0c;而且可以重新定義新的數據類型和函數&#xff0c;從而建立累的層次或等級關系。 格式&#xff1a;     [修飾符] class 子…

python數據結構的應用場景不包括,Python 數據結構學習

Python 數據結構學習列表list.append(x)在列表的末尾添加一個元素。相當于 a[len(a):] [x] 。list.extend(iterable)使用可迭代對象中的所有元素來擴展列表。相當于 a[len(a):] iterable 。list.insert(i, x)在給定的位置插入一個元素。第一個參數是要插入的元素的索引&#…

[Jinkey 原創]震驚!iOS 系統居然自帶懸浮窗口調試工具

原文鏈接 : 震驚&#xff01;iOS 系統居然自帶懸浮窗口調試工具 —— Jinkey 原創原文作者 : Jinkey1 背景 英文原文&#xff1a;http://ryanipete.com/blog/ios/swift/objective-c/uidebugginginformationoverlay/ 我寫得這個并不是翻譯而是用自己的理解重新表述這個功能&…

盲人編程_盲人如何編碼

盲人編程About one out of every 200 software developers is blind. We know this because Stack Overflow asked 64,000 developers about this a few months ago.每200名軟件開發人員中大約有1名是盲人。 我們之所以知道這一點&#xff0c;是因為幾個月前 Stack Overflow 向…

hadoop環境搭建筆記

一、配置Linux &#xff08;1&#xff09;cat /etc/networks &#xff08;2&#xff09;cat /etc/sysconfig/network &#xff08;3&#xff09;vi /etc/udev/rules.d/70-persistent-net.rules eth1 改為eth0 &#xff08;4&#xff09;vi /etc/sysconfig/network-scripts/ifc…

邊分治講解

前言&#xff1a; 邊分治和點分治一樣屬于樹分治的一部分&#xff0c;相比于點分治&#xff0c;邊分治對于與度數相關的問題有著很大的優勢&#xff0c;同時邊分治也是解決樹上最優化問題的一種重要的算法。 分治過程&#xff1a; 邊分治的分治過程與點分治類似&#xff0c;同樣…

準確性 敏感性 特異性_如何掌握類型特異性的藝術

準確性 敏感性 特異性Do more specific definitions result in less flexibility?更具體的定義會導致靈活性降低嗎&#xff1f; In this post I will try to avoid the debate about strong/static vs. weak/dynamic types (what more could possibly be said?), or even sc…