CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: find_if

  1. #1
    Join Date
    Jan 2016
    Posts
    7

    find_if

    Hello, I am new in this forum and hope to get some answers here.

    Some words about me: I am a programmer since 88, working with C++ Builder XE7, living in Germany/Berlin.


    My first question is about the algorithm "find_if" in combination with classes.

    The compilere says : "const TDummy&" can not initialised with "par<const TDummyKey, TDummy>"

    What does this mean?

    This is my class:

    Code:
    class TDummyKey {
    public:
      int iID;
    
    public:
      TDummyKey(int paID) {
        iID = paID;
        }
    
      TDummyKey const& operator = (TDummyKey const&);
      bool operator == (TDummyKey const&) const;
      bool operator != (TDummyKey const&) const;
      bool operator <  (TDummyKey const&) const;
      bool operator <= (TDummyKey const&) const;
      bool operator >  (TDummyKey const&) const;
      bool operator >= (TDummyKey const&) const;
      int   Compare(TDummyKey const&) const;
    };
    
    
    inline bool TDummyKey::operator == (TDummyKey const& RefVar) const {
        return Compare(RefVar) == 0;
      }
    
    inline bool TDummyKey::operator != (TDummyKey const& RefVar) const {
        return Compare(RefVar) != 0;
      }
    
    inline bool TDummyKey::operator <  (TDummyKey const& RefVar) const {
        return Compare(RefVar) < 0;
      }
    
    inline bool TDummyKey::operator <= (TDummyKey const& RefVar) const {
        return Compare(RefVar) <= 0;
      }
    
    inline bool TDummyKey::operator >  (TDummyKey const& RefVar) const {
        return Compare(RefVar) > 0;
      }
    
    inline bool TDummyKey::operator >= (TDummyKey const& RefVar) const {
        return Compare(RefVar) >= 0;
      }
    
    int TDummyKey::Compare(TDummyKey const& RefVar) const {
    
      if (iID < RefVar.iID)                         return -1;
      if (iID > RefVar.iID)                         return  1;
    
      return 0;
      }
    
    
    class TDummy : public TDummyKey{
    public:
      string strVarName;
    
    public:
      TDummy(int paID, string paVarName) : TDummyKey(paID) {
        strVarName = paVarName;
        }
    
      TDummy const& operator = (TDummy const&);
      bool operator == (TDummy const&) const;
      bool operator != (TDummy const&) const;
      bool operator <  (TDummy const&) const;
      bool operator <= (TDummy const&) const;
      bool operator >  (TDummy const&) const;
      bool operator >= (TDummy const&) const;
      int   Compare(TDummy const&) const;
    };
    
    
    inline bool TDummy::operator == (TDummy const& RefVar) const {
        return Compare(RefVar) == 0;
      }
    
    inline bool TDummy::operator != (TDummy const& RefVar) const {
        return Compare(RefVar) != 0;
      }
    
    inline bool TDummy::operator <  (TDummy const& RefVar) const {
        return Compare(RefVar) < 0;
      }
    
    inline bool TDummy::operator <= (TDummy const& RefVar) const {
        return Compare(RefVar) <= 0;
      }
    
    inline bool TDummy::operator >  (TDummy const& RefVar) const {
        return Compare(RefVar) > 0;
      }
    
    inline bool TDummy::operator >= (TDummy const& RefVar) const {
        return Compare(RefVar) >= 0;
      }
    
    int TDummy::Compare(TDummy const& RefVar) const {
    
      if (strVarName < RefVar.strVarName)                         return -1;
      if (strVarName > RefVar.strVarName)                         return  1;
    
      return 0;
      }
    
    
    typedef map<TDummyKey,TDummy>   MapDummy;
    typedef MapDummy::iterator      ItMapDummy;
    This is the needed function for find_if

    Code:
    namespace stuff {
      int iID=0;
      bool IsID (const TDummy& value) { return (value.iID == iID); }
    }

    This is my main-code:

    Code:
      MapDummy mapDummy;
      TDummy   Dummy1(1, string("A"));
      TDummy   Dummy2(2, string("B"));
      TDummy   Dummy3(3, string("C"));
    
      mapDummy.insert (pair<TDummyKey, TDummy> (Dummy1, Dummy1));
      mapDummy.insert (pair<TDummyKey, TDummy> (Dummy2, Dummy2));
      mapDummy.insert (pair<TDummyKey, TDummy> (Dummy3, Dummy3));
    
      for (ItMapDummy it  = mapDummy.begin();
                      it != mapDummy.end();
                      ++it) {
    
        wostringstream os;
        os << it->first.iID << " : " << it->second.strVarName.c_str();
        cout << os.str().c_str();
        }
    
    
    
      stuff::iID = 2;
      find_if(mapDummy.begin(), mapDummy.end(), stuff::IsID);
    
      for (ItMapDummy it  = mapDummy.begin();
                      it != mapDummy.end();
                      ++it) {
    
        wostringstream os;
        os << it->first.iID << " : " << it->second.strVarName.c_str();
        cout << os.str().c_str();
        }


    sincerly,
    Necip

  2. #2
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: find_if

    The find_if algorithm iterates over the elements of the container. In your case, the container is map<TDummyKey,TDummy>, and each element in the container is a pair<const TDummyKey, TDummy>.
    Your predicate stuff::IsID should not take a single const TDummy& value, but the pair.

    HTH,
    Richard

  3. #3
    Join Date
    Jan 2016
    Posts
    7

    Re: find_if

    Indeed!!! ^^ Thank you Richard!

    Code:
    namespace stuff {
      int iID=0;
      bool IsID (const pair<const TDummyKey, TDummy>& value) {
        return (value.first == iID);
        }
    }
    Code:
    stuff::iID = 2;
    ItMapDummy it = find_if(mapDummy.begin(), mapDummy.end(), stuff::IsID);
    if (it != mapDummy.end());
      mapDummy.erase(it);

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured