C++ find_if函数使用(STL_LIST)
2015-12-22 15:25
736 查看
//函数原型 // TEMPLATE FUNCTION find_if template<class _InIt,class _Pr> inline _InIt _Find_if(_InIt _First, _InIt _Last, _Pr _Pred) { // find first satisfying _Pred for (; _First != _Last; ++_First) if (_Pred(*_First)) break; return (_First); } template<class _InIt,class _Pr> inline _InIt find_if(_InIt _First, _InIt _Last, _Pr _Pred) { // find first satisfying _Pred _DEBUG_RANGE(_First, _Last); _DEBUG_POINTER(_Pred); return (_Rechecked(_First,_Find_if(_Unchecked(_First), _Unchecked(_Last), _Pred))); }
使用例(STL LIST容器):
#include <iostream> #include <stdio.h> #include <string> #include <list> #include <algorithm> using namespace std; class keyValue { //private: public: string m_key; string m_value; public: keyValue():m_key(""),m_value(""){} keyValue(string key, string value = ""):m_key(key), m_value(value){} bool operator () (const keyValue &temp)// ()重载 { return m_key == temp.m_key; } }; int main(int argc, char *argv[]) { list<keyValue> test; test.push_back(keyValue("a", "1")); test.push_back(keyValue("b", "2")); test.push_back(keyValue("c", "3")); list<keyValue>::iterator iter = find_if(test.begin(), test.end(), keyValue("b")); if(iter != test.end())//如果迭代器指向end代表没查到 { cout << "find successful" << endl; cout << iter->m_key << endl; cout << iter->m_value << endl; test.erase(iter);//删除 } else cout << "find failed" << endl; iter = find_if(test.begin(), test.end(), keyValue("b")); if(iter != test.end()) { cout << "find successful" << endl; cout << iter->m_key << endl; cout << iter->m_value << endl; test.erase(iter); } else cout << "find failed" << endl; system("pause"); return 0; }运行结果:
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Linux C函数参考手册(PDF版)
- Lua中调用C++函数示例
- Lua教程(十七):C API简介
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C#中Ilist与list的区别小结
- C#中IList<T>与List<T>的区别深入解析
- C++联合体转换成C#结构的实现方法
- C#实现打造气泡屏幕保护效果
- C#对list列表进行随机排序的方法
- 使用C#代码获取存储过程返回值
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析