亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 2295 | 回復(fù): 3
打印 上一主題 下一主題

[C++] [請(qǐng)教]這個(gè)仿函數(shù)為什么必須加const [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2013-04-26 22:51 |只看該作者 |倒序?yàn)g覽
本帖最后由 kingoftime3 于 2013-04-26 22:53 編輯
  1. #include <list>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <functional>
  5. #include <string>

  6. using namespace std;

  7. template<class T>
  8. struct IsSame : binary_function<pair<string,string>, T, bool>
  9. {
  10.         bool operator()(const pair<string,string> &p, const T &s) const
  11.         {
  12.                 return p.first==s;
  13.         }
  14. };

  15. static void print(const pair<string, string> &p)
  16. {
  17.         cout << p.first << "\t" << p.second << endl;
  18. }

  19. int main()
  20. {
  21.         list<pair<string,string> > mlist;
  22.         mlist.push_back(pair<string,string>("aaa", "aaa"));
  23.         mlist.push_back(pair<string,string>("aaa1", "aaa"));
  24.         mlist.push_back(pair<string,string>("aaa2", "aaa"));
  25.         mlist.push_back(pair<string,string>("aaa3", "aaa"));
  26.         mlist.remove_if(binder2nd<IsSame<string> >(IsSame<string>(), "aaa"));
  27.         for_each(mlist.begin(), mlist.end(), print);
  28.         system("pause");
  29.         return 0;

  30. }
復(fù)制代碼
如果
  1. bool operator()(const pair<string,string> &p, const T &s) const
復(fù)制代碼
寫(xiě)成
  1. bool operator()(const pair<string,string> &p, const T &s)
復(fù)制代碼
就會(huì)報(bào)錯(cuò)
  1. 1>d:\program files\microsoft visual studio 11.0\vc\include\xfunctional(329): error C3848: 具有類型“const IsSame<T>”的表達(dá)式會(huì)丟失一些 const-volatile 限定符以調(diào)用“bool IsSame<T>::operator ()(const std::pair<_Ty1,_Ty2> &,const T &)”
  2. 1>          with
  3. 1>          [
  4. 1>              T=std::string
  5. 1>          ]
  6. 1>          d:\program files\microsoft visual studio 11.0\vc\include\xfunctional(328): 編譯類 模板 成員函數(shù)“bool std::binder2nd<_Fn2>::operator ()(std::pair<_Ty1,_Ty2> &) const”時(shí)
  7. 1>          with
  8. 1>          [
  9. 1>              _Fn2=IsSame<std::string>,
  10. 1>              _Ty1=std::string,
  11. 1>              _Ty2=std::string
  12. 1>          ]
  13. 1>          d:\program files\microsoft visual studio 11.0\vc\include\list(1615): 參見(jiàn)對(duì)正在編譯的函數(shù) 模板 實(shí)例化“bool std::binder2nd<_Fn2>::operator ()(std::pair<_Ty1,_Ty2> &) const”的引用
  14. 1>          with
  15. 1>          [
  16. 1>              _Fn2=IsSame<std::string>,
  17. 1>              _Ty1=std::string,
  18. 1>              _Ty2=std::string
  19. 1>          ]
  20. 1>          c:\users\chenxie\documents\visual studio 2012\projects\simpletest\simpletest\simpletest.cpp(30): 參見(jiàn)對(duì)正在編譯的類 模板 實(shí)例化“std::binder2nd<_Fn2>”的引用
  21. 1>          with
  22. 1>          [
  23. 1>              _Fn2=IsSame<std::string>
  24. 1>          ]
復(fù)制代碼

論壇徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16賽季CBA聯(lián)賽之青島
日期:2016-07-05 12:36:0515-16賽季CBA聯(lián)賽之廣東
日期:2016-06-29 11:45:542015亞冠之全北現(xiàn)代
日期:2015-07-22 08:09:472015年辭舊歲徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39獅子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技術(shù)圖書(shū)徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
2 [報(bào)告]
發(fā)表于 2013-04-27 12:52 |只看該作者
因?yàn)?binder2nd 的 operator() 后面有個(gè) const

另外 binary_function、binder2nd 都是要廢棄的東西,沒(méi)必要研究了
system("pause") 嘛,不想評(píng)價(jià)

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2013-04-27 14:10 |只看該作者
回復(fù) 2# bruceteen


    binder2nd如果廢棄了有沒(méi)有什么替代的接口可以實(shí)現(xiàn)同樣的功能呢?

論壇徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16賽季CBA聯(lián)賽之青島
日期:2016-07-05 12:36:0515-16賽季CBA聯(lián)賽之廣東
日期:2016-06-29 11:45:542015亞冠之全北現(xiàn)代
日期:2015-07-22 08:09:472015年辭舊歲徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39獅子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技術(shù)圖書(shū)徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
4 [報(bào)告]
發(fā)表于 2013-04-27 14:24 |只看該作者
回復(fù) 3# kingoftime3
lambda表達(dá)式 和 std::function
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP