//
//  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

相关文章:

  • 2021-08-12
  • 2021-04-14
  • 2021-12-03
  • 2021-12-12
  • 2022-01-14
  • 2021-08-02
猜你喜欢
  • 2021-06-01
  • 2021-05-18
  • 2022-12-23
  • 2021-08-13
  • 2021-12-12
  • 2022-12-23
  • 2021-12-20
相关资源
相似解决方案