// find.cpp: 定义控制台应用程序的入口点。
//
#include <vector>
#include <iostream>
#include <map>
#include <string>
template<class InputIterator, class T>
InputIterator find(InputIterator &first, InputIterator &end, const T& value) {
while (first != end && *first != value) {
++first;
}
return first;
}
template<class InputIterator, class predicit>
InputIterator find_if(InputIterator &first, InputIterator &end, predicit pred) {
while (first != end && !pred(*first)) {
first++;
}
return first;
}
class map_finder {
public:
map_finder(const std::string& cmp_string) : m_s_cmp_string(cmp_string) {
}
inline bool operator()(std::map<int, std::string>::value_type &pair) const {
return pair.second == m_s_cmp_string;
}
private:
const std::string m_s_cmp_string;
};
int main()
{
std::map<int, std::string> mymap;
mymap.insert(std::make_pair(10, "China"));
mymap.insert(std::make_pair(20, "USA"));
mymap.insert(std::make_pair(30, "English"));
mymap.insert(std::make_pair(40, "Japan"));
std::map<int, std::string>::iterator first = mymap.begin();
std::map<int, std::string>::iterator end = mymap.end();
auto it = find_if(first, end, map_finder("English"));
if (it == end) {
std::cout << "find fail";
}
else {
std::cout << (*it).first;
}
return 0;
}