久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用成員函數(shù)作為比較器進(jìn)行排序的問(wèn)題

problem sorting using member function as comparator(使用成員函數(shù)作為比較器進(jìn)行排序的問(wèn)題)
本文介紹了使用成員函數(shù)作為比較器進(jìn)行排序的問(wèn)題的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

嘗試編譯以下代碼時(shí)出現(xiàn)此編譯錯(cuò)誤,我該怎么辦?

trying to compile the following code I get this compile error, what can I do?

ISO C++ 禁止取地址不合格的或括號(hào)內(nèi)的非靜態(tài)成員函數(shù)形成一個(gè)指向成員函數(shù)的指針.

ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.

class MyClass {
   int * arr;
   // other member variables
   MyClass() { arr = new int[someSize]; }

   doCompare( const int & i1, const int & i2 ) { // use some member variables } 

   doSort() { std::sort(arr,arr+someSize, &doCompare); }

}; 

推薦答案

doCompare 必須是 static.如果 doCompare 需要來(lái)自 MyClass 的數(shù)據(jù),您可以通過(guò)更改將 MyClass 變成一個(gè)比較函子:

doCompare must be static. If doCompare needs data from MyClass you could turn MyClass into a comparison functor by changing:

doCompare( const int & i1, const int & i2 ) { // use some member variables } 

進(jìn)入

bool operator () ( const int & i1, const int & i2 ) { // use some member variables } 

并調(diào)用:

doSort() { std::sort(arr, arr+someSize, *this); }

另外,doSort 是不是缺少返回值?

Also, isn't doSort missing a return value?

我認(rèn)為應(yīng)該可以使用 std::mem_fun 和某種綁定將成員函數(shù)轉(zhuǎn)換為自由函數(shù),但目前我無(wú)法理解確切的語(yǔ)法.

I think it should be possible to use std::mem_fun and some sort of binding to turn the member function into a free function, but the exact syntax evades me at the moment.

Doh,std::sort 按值獲取函數(shù),這可能是一個(gè)問(wèn)題.為了解決這個(gè)問(wèn)題,將函數(shù)包裝在類中:

Doh, std::sort takes the function by value which may be a problem. To get around this wrap the function inside the class:

class MyClass {
    struct Less {
        Less(const MyClass& c) : myClass(c) {}
        bool operator () ( const int & i1, const int & i2 ) {// use 'myClass'} 
        MyClass& myClass;
    };
    doSort() { std::sort(arr, arr+someSize, Less(*this)); }
}

這篇關(guān)于使用成員函數(shù)作為比較器進(jìn)行排序的問(wèn)題的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉(zhuǎn)圖像而不使用 OpenCV 函數(shù))
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開(kāi) avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無(wú)法設(shè)置 SVM 參數(shù))
Convert a single color with cvtColor(使用 cvtColor 轉(zhuǎn)換單一顏色)
主站蜘蛛池模板: 天堂成人av | 亚洲精品 在线播放 | 一级片视频免费 | 999久久久国产精品 欧美成人h版在线观看 | 日本福利片 | av中文字幕网| 国产精品日韩欧美一区二区三区 | 久久久久国产精品 | 欧美成人精品二区三区99精品 | 久草热8精品视频在线观看 午夜伦4480yy私人影院 | 亚洲国产成人精品久久久国产成人一区 | 欧美精品中文 | 久久国产精品72免费观看 | 亚洲综合五月天婷婷 | 在线观看免费高清av | 久久久久久久久中文字幕 | www亚洲成人| 99久久精品国产一区二区三区 | 国产美女视频黄 | 蜜臀久久 | 国产精品欧美一区二区 | 久草热播 | 日韩中文字幕免费在线 | 国产在线精品一区二区三区 | 一区二区三区在线播放 | 欧美日韩综合一区 | 亚洲国产成人av好男人在线观看 | 亚洲精品v | 亚洲精品一区中文字幕乱码 | 欧美一区二区三区视频 | 91国内在线观看 | 在线免费观看黄网 | 日韩欧美中文在线 | 天天插天天操 | 久久综合久久久 | 久久午夜精品福利一区二区 | 国产精品久久久久久福利一牛影视 | 亚洲精品久久久久久久久久久久久 | 亚洲欧美日韩国产综合 | 亚洲狠狠爱| 国产大片一区 |