最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++在成員函數(shù)中使用STL的find_if函數(shù)實(shí)例

 更新時(shí)間:2014年10月08日 11:16:51   投稿:shichen2014  
這篇文章主要介紹了C++在成員函數(shù)中使用STL的find_if函數(shù)實(shí)例,包括了STL中find_if函數(shù)的具體用法及相關(guān)的完整實(shí)例,非常具有參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C++在成員函數(shù)中使用STL的find_if函數(shù)的方法。分享給大家供大家參考。具體方法分析如下:

一般來說,STL的find_if函數(shù)功能很強(qiáng)大,可以使用輸入的函數(shù)替代等于操作符執(zhí)行查找功能(這個(gè)網(wǎng)上有很多資料,我這里就不多說了)。

比如查找一個(gè)數(shù)組中的奇數(shù),可以用如下代碼完成(具體參考這里:http://www.cplusplus.com/reference/algorithm/find_if/):

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool IsOdd (int i) {
 return ((i%2)==1);
}

int main () {
 vector<int> myvector;
 vector<int>::iterator it;

 myvector.push_back(10);
 myvector.push_back(25);
 myvector.push_back(40);
 myvector.push_back(55);

 it = find_if (myvector.begin(), myvector.end(), IsOdd);
 cout << "The first odd value is " << *it << endl;

 return 0;
}

運(yùn)行結(jié)果:

The first odd value is 25

如果把上述代碼加入到類里面,寫成類的成員函數(shù),又是什么效果呢?

比如如下類代碼:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

class CTest
{
public:
 bool IsOdd (int i) {
  return ((i%2)==1);
 }

 int test () {
  vector<int> myvector;
  vector<int>::iterator it;
  myvector.push_back(10);
  myvector.push_back(25);
  myvector.push_back(40);
  myvector.push_back(55);
  it = find_if (myvector.begin(), myvector.end(), IsOdd);
  cout << "The first odd value is " << *it << endl;
  return 0;
 }
};
int main()
{
 CTest t1;
 t1.test();
 return 0;
}

會(huì)出現(xiàn)類似下面的錯(cuò)誤:

error C3867: 'CTest::IsOdd': function call missing argument list; use '&CTest::IsOdd' to create a pointer to member

今天我就遇到了這個(gè)問題,這里把解決方案貼出來,僅供參考:

it = find_if (myvector.begin(), myvector.end(), IsOdd);

改為:

it = find_if(myvector.begin(), myvector.end(),std::bind1st(std::mem_fun(&CTest::IsOdd),this));

用bind1st函數(shù)和mem_fun函數(shù)加上this指針搞定的。

完整實(shí)例代碼點(diǎn)擊此處本站下載

希望本文所述對(duì)大家的C++程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

宝兴县| 杭锦后旗| 阿城市| 望都县| 金昌市| 满洲里市| 石门县| 南通市| 盐津县| 云龙县| 舞钢市| 成都市| 陇西县| 石河子市| 金湖县| 中宁县| 泰州市| 唐山市| 繁峙县| 兴城市| 汉沽区| 泌阳县| 江西省| 南木林县| 绿春县| 香格里拉县| 河曲县| 永新县| 晋中市| 西华县| 长泰县| 孟津县| 凯里市| 新郑市| 邓州市| 陇川县| 渭南市| 蒙自县| 大方县| 会理县| 漳平市|