欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門(mén)教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

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

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎn)擊: 次

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

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

比如查找一個(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

如果把上述代碼加入到類里面,寫(xiě)成類的成員函數(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è)問(wèn)題,這里把解決方案貼出來(lái),僅供參考:

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ì)有所幫助。

上一篇:C++ COM編程之什么是組件?

欄    目:C語(yǔ)言

下一篇:C++設(shè)計(jì)模式之組合模式

本文標(biāo)題:C++在成員函數(shù)中使用STL的find_if函數(shù)實(shí)例

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3316.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有