首页 > 代码库 > 使用find_if算法搜寻map的value

使用find_if算法搜寻map的value

////  main.cpp//  map_find////  Created by PKU on 14-9-8.//  Copyright (c) 2014年 PKU. All rights reserved.//#include <iostream>#include <algorithm>#include <map>using namespace std;template <class K, class V>class value_equals{private:    V value;public:    value_equals(const V & vt):value(vt)    {            }    bool operator()(pair<const K, V> & elem)    {        return elem.second==value;    }};int main(int argc, const char * argv[]){    typedef map<float, float> FloatFloatMap;    FloatFloatMap coll;    FloatFloatMap::iterator pos;    coll[1]=7;    coll[2]=4;    coll[3]=2;    coll[4]=3;    coll[5]=6;    coll[6]=1;    coll[7]=3;    pos=coll.find(3);    if (pos!=coll.end()) {        cout << pos->first << ": " << pos->second << endl;    }    pos=find_if(coll.begin(), coll.end(), value_equals<float, float>(3.0));    while (pos!=coll.end()) {        cout << pos->first << ": " << pos->second << endl;        pos=find_if(++pos, coll.end(), value_equals<float, float>(3.0));    }    return 0;}

使用find_if算法搜寻map的value