没有合适的资源?快使用搜索试试~ 我知道了~
20121018_在成员函数中使用STL的find_if函数1
需积分: 0 1 下载量 44 浏览量
2022-08-04
14:33:47
上传
评论
收藏 65KB PDF 举报
温馨提示
试读
3页
20121018_在成员函数中使用STL的find_if函数1
资源详情
资源评论
资源推荐
在成员函数中使用 STL 的 find_if 函数
STL 的 find_if 函数功能很强大,可以使用输入的函数替代等于操作符执行查找功能。
比如查找一个数组中的奇数,可以用如下代码完成(具体参考这里:
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;
}
运行结果:
The first odd value is 25
如果把上述代码加入到类里面,写成类的成员函数,又是什么效果呢?
比如如下类代码:
#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);
E-Mail : Mike_Zhang@live.com 1
xhmoon
- 粉丝: 15
- 资源: 329
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0