Thread: STL find error with STL map

  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    STL find error with STL map

    I've used maps before with no troubles but for some reason I'm getting an error now with this code. Perhaps I've missed something obvious but any help would be appreciated.

    Code:
    ...
    ...
      //iterator for find
      std::map<DWORD,STLListNode *>::iterator it;
      
      //If it==end of map then value is not in map
      bool bLoop=true; 
      do
      {
        dwID=r.Random(65535);
        it=std::find(m_mapRes.begin(),
                     m_mapRes.end(),
                     dwID);
                     
        if (it==m_mapRes.end()) bLoop=false;
          
      } while (bLoop);
    ...
    ...
    Class code
    Code:
    struct STLListNode
    {
      CResource *pResource;
      STLListNode *pNext;
      STLListNode *pPrev;
    };
    
    class CResMgr
    {
      private:
        CResMgr(const CResMgr &) { }
        CResMgr &operator=(const CResMgr &) { }
       
      protected:
        IDirect3DDevice9 *m_spDevice;
        
        ListNode *m_pRoot;
        STLListNode *m_pTop;
           
        std::map<DWORD,STLListNode *> m_mapRes;
        ....

    When I comment out the bolded line of code I do not get the error. The error is coming from <algorithm> and is:

    e:\program files\microsoft visual studio 8\vc\include\algorithm(40) : error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::pair<_Ty1,_Ty2>' (or there is no acceptable conversion)
    with
    [
    _Ty1=const DWORD,
    _Ty2=STLListNode *
    ]
    e:\program files\microsoft platform sdk\include\guiddef.h(192): could be 'int operator ==(const GUID &,const GUID &)' [found using argument-dependent lookup]
    while trying to match the argument list '(std::pair<_Ty1,_Ty2>, const DWORD)'
    with
    [
    _Ty1=const DWORD,
    _Ty2=STLListNode *
    ]
    e:\program files\microsoft visual studio 8\vc\include\algorithm(74) : see reference to function template instantiation '_InIt std::_Find<std::_Tree<_Traits>::iterator,_Ty>(_InI t,_InIt,const _Ty &)' being compiled
    with
    [
    _InIt=std::_Tree<std::_Tmap_traits<DWORD,STLListNo de *,std::less<DWORD>,std::allocator<std::pair<const DWORD,STLListNode *>>,false>>::iterator,
    _Traits=std::_Tmap_traits<DWORD,STLListNode *,std::less<DWORD>,std::allocator<std::pair<const DWORD,STLListNode *>>,false>,
    _Ty=DWORD
    ]
    e:\msvc 2005 projects\starx\cresmgr.cpp(27) : see reference to function template instantiation '_InIt std::find<std::_Tree<_Traits>::iterator,DWORD>(_In It,_InIt,const _Ty &)' being compiled
    with
    [
    _InIt=std::_Tree<std::_Tmap_traits<DWORD,STLListNo de *,std::less<DWORD>,std::allocator<std::pair<const DWORD,STLListNode *>>,false>>::iterator,
    _Traits=std::_Tmap_traits<DWORD,STLListNode *,std::less<DWORD>,std::allocator<std::pair<const DWORD,STLListNode *>>,false>,
    _Ty=DWORD
    ]
    Last edited by VirtualAce; 08-20-2007 at 05:21 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Use the container's own find member function, not the generic version.

    Code:
    it = m_mapRes.find(dwID);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by hk_mp5kpdw View Post
    Use the container's own find member function, not the generic version.
    To reinforce this comment... Using the generic algorithm completely wipes away any benefit of using a map in the first place. The generic find is probably implemented as a linear search from begin() to end(). You might as well have just used a vector, in that case.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Hmm. I tried that because I thought the same thing. Perhaps something else was wrong at that time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with static STL i.e. map
    By vijay_choudhari in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2007, 05:56 AM
  2. How to find the coords of an image map?
    By Geo-Fry in forum Tech Board
    Replies: 3
    Last Post: 09-08-2003, 05:54 PM
  3. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  4. Searching STL Map Inside STL Map Object :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2002, 09:11 AM
  5. Won't Return pointer, i can't find why...
    By ss3x in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2002, 08:50 PM