Using find_if on the map to find by value

I have a class that has a map. I need to find an iterator on a map, looking for a specific value, instad key. Using the member function predicate IsValueFound, I am trying to do this.

class A
{
public:
  void findVal();
private:
 int state;
 map<int, int> exmap;
 bool IsValueFound(pair<int key, int val> itr)
 {
   return state == itr.second;
 }
};

void A::findVal
{
  itr = find_if(exmap.begin, exmap.end, mem_fun1_ref(&A::IsValueFound));
}

I get compilation errors. I'm not sure what the syntax is for these functional adapters. Please, help.

EDIT: Sorry. Please ignore compilation errors other than finf_if stmt. I need to find find_if stmt first. Also, the code does not have support :(

+3
source share
7 answers

Edit : there is clearly an error in my answer that mem_fun1_ref(&A::IsValueFound)does not work as a predicate for std::find_if. I am fixing it.

exmap.begin exmap.end. , , .

:

typedef map<int, int>::const_iterator MyIterator
void A::findVal()
{
    const MyIterator itrBegin = exmap.begin();
    const MyIterator itrEnd = exmap.end();
    MyIterator itrFound = find_if( itrBegin ,
                                   itrEnd ,
                                   mem_fun1_ref(&A::IsValueFound));    
}

mem_fun1_ref(&A::IsValueFound) . mem_fun1_ref, operator().

+2

A :
(. ):

#include <map>
#include <memory>
#include <functional>
#include <algorithm>

using namespace std;

class A {
    public:
      void findVal();
    private:
      int state;
      map<int, int> exmap;

      // Changed the function IsValueFound() to operator()
      // This makes the whole object behave like a function.
      // Its a lot easier then getting member functions and binding
      // the this reference.
      bool operator()(map<int,int>::value_type const& itr) const
                    //         ^^^^^^^^^^^^^^^^^
                    //  std::pair<int,int> is not the type held in the map
                    //  so you are not going to bind against it using a pair.
      {
          return state == itr.second;
      }
};

void A::findVal()
{
    // You did not specify a type
    // for the iterator in your code.
    std::map<int,int>::iterator itr1 = find_if(exmap.begin(), exmap.end(), *this);
                                                   //   ^^^^^^       ^^^^^^
                                                   // begin() and end() are methods.

    // Just pass the `this` object as the the third parameter it will act as a function now.
}
+4

Boost Bimap, , , (). :

class A {
public:
bool findVal(int s);

private:
map<int, int> exmap;

struct IsValueFound {
    int state;
    IsValueFound(int _state) : state(_state) {};
    bool operator()(const pair<int, int>& itr) {
        return state == itr.second;
    }
};  

};

bool A::findVal(int x) {
    return (find_if(exmap.begin(), exmap.end(), A::IsValueFound(x)) != exmap.end());
}
+2

, IsValueFound . :

map<int, int>::iterator itr

A:: findVal itr .

, , . .

+1

mem_fun_ref

bool IsValueFound(pair<int, int> itr)

:

bool functor(A& this, pair<int, int> itr).

, , this - IsValueFound.

, , , - mem_fun_ref - :

std::find_if(exmap.begin, exmap.end, std::bind2nd(std::equal_to<int>(), state));

, ... , , .

, , @martona , :)

+1
source

you are actually using your map like bi-directional mapso you can find boost boost multi-index containers . They have a concrete example of a bidirectional map implementation.

0
source

Just use the Boost Bimap .

0
source

Source: https://habr.com/ru/post/1781859/


All Articles