第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

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

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

C++
狐的傳說(shuō) 2019-12-15 14:08:07
嘗試編譯以下代碼時(shí),出現(xiàn)此編譯錯(cuò)誤,該怎么辦?ISO C ++禁止使用不合格或帶括號(hào)的非靜態(tài)成員函數(shù)的地址形成指向成員函數(shù)的指針。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); }}; 
查看完整描述

3 回答

?
慕婉清6462132

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個(gè)贊

doCompare必須是static。如果doCompare需要來(lái)自MyClass您的數(shù)據(jù),可以MyClass通過(guò)更改以下內(nèi)容將其用作比較函子:


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 } 

并致電:


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

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


我認(rèn)為應(yīng)該可以使用std::mem_fun某種綁定將成員函數(shù)轉(zhuǎn)換為自由函數(shù),但是目前確切的語(yǔ)法讓我回避了。


編輯: Doh,std::sort按值取函子可能是個(gè)問(wèn)題。為了解決這個(gè)問(wèn)題,將函子包裝到類(lèi)中:


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)); }

}



查看完整回答
反對(duì) 回復(fù) 2019-12-16
?
慕田峪4524236

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超5個(gè)贊

正如Andreas Brinck所說(shuō),doCompare必須為靜態(tài)(+1)。如果您必須在比較器函數(shù)中使用狀態(tài)(使用該類(lèi)的其他成員),則最好使用函子而不是函數(shù)(這樣會(huì)更快):


class MyClass{


   // ...

   struct doCompare

   { 

       doCompare( const MyClass& info ) : m_info(info) { } // only if you really need the object state

       const MyClass& m_info;


       bool operator()( const int & i1, const int & i2  )

       { 

            // comparison code using m_info

       }

   };


    doSort() 

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

};

使用函子總是更好,鍵入的時(shí)間更長(zhǎng)(這可能不方便,但是很好...)


我認(rèn)為您也可以將std :: bind與成員函數(shù)一起使用,但是我不確定如何并且那也不容易閱讀。


今天,我們可以使用c ++ 11編譯器,因此您可以改用lambda,代碼雖然較短,但語(yǔ)義完全相同。




查看完整回答
反對(duì) 回復(fù) 2019-12-16
?
子衿沉夜

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

Rob提出的解決方案現(xiàn)在是有效的C ++ 11(無(wú)需Boost):


void doSort()

{

  using namespace std::placeholders;

  std::sort(arr, arr+someSize, std::bind(&MyClass::doCompare, this, _1, _2));

}

確實(shí),正如Klaim所提到的,lambda是一個(gè)選項(xiàng),有點(diǎn)冗長(zhǎng)(您必須“重復(fù)”這些參數(shù)是整數(shù)):


void doSort()

{

  std::sort(arr, arr+someSize, [this](int l, int r) {return doCompare(l, r); });

}

C ++ 14 auto在這里支持:


void doSort()

{

  std::sort(arr, arr+someSize, [this](auto l, auto r) {return doCompare(l, r); });

}

但是,您仍然聲明參數(shù)是通過(guò)副本傳遞的。


那么問(wèn)題是“哪一個(gè)是最有效的”。特拉維斯·高克爾(Travis Gockel)處理了該問(wèn)題:Lambda vs Bind。他的基準(zhǔn)測(cè)試程序在我的計(jì)算機(jī)上提供(OS X i7)


                        Clang 3.5    GCC 4.9

   lambda                    1001        7000

   bind                3716166405  2530142000

   bound lambda        2438421993  1700834000

   boost bind          2925777511  2529615000

   boost bound lambda  2420710412  1683458000

其中l(wèi)ambda的lambda直接使用,lambda bound是存儲(chǔ)在中的lambda std::function。


因此看來(lái),lambda是更好的選擇,這并不奇怪,因?yàn)闉榫幾g器提供了可從中獲利的更高級(jí)別的信息



查看完整回答
反對(duì) 回復(fù) 2019-12-16
  • 3 回答
  • 0 關(guān)注
  • 387 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)