CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2008
    Posts
    2

    Cool generic lookup using STL map

    The STL map's find method returns an iterator which is tedious
    to program with so I decided to write a templated lookup
    function as follows:

    //
    // Generic function that returns val associated with key
    // in given map. It is assumed that the key *will* be found.
    //
    template <class KEY, class VAL>
    VAL lookup(map<KEY,VAL>& m, KEY key) {
    map<KEY,VAL>::iterator iter = m.find(key);
    assert (iter != m.end());
    return iter->second;
    }

    I have trouble getting instances of this to compile via g++.
    Here is a small program that demos the problem:

    #include <map>
    #include <iostream>

    using namespace std;

    template <class KEY, class VAL>
    VAL lookup(map<KEY,VAL>& m, KEY key) {
    map<KEY,VAL>::iterator iter = m.find(key);
    assert (iter != m.end());
    return iter->second;
    }

    int main() {
    map<int,int> m;
    m[1] = 100;
    m[2] = 200;
    int n = lookup(m, 1);
    cout << n << endl;
    return 0;
    }

    Here are the error messages.

    $ g++ lookup.cpp -o lookup
    lookup.cpp: In function ‘VAL lookup(std::map<KEY, VAL, std::less<_Key>, std::allocator<std:air<const _Key, _Tp> > >&, KEY)’:
    lookup.cpp:8: error: expected `;' before ‘iter’
    lookup.cpp:9: error: ‘iter’ was not declared in this scope
    lookup.cpp: In function ‘VAL lookup(std::map<KEY, VAL, std::less<_Key>, std::allocator<std:air<const _Key, _Tp> > >&, KEY) [with KEY = int, VAL = int]’:
    lookup.cpp:17: instantiated from here
    lookup.cpp:8: error: dependent-name ‘std::map<KEY,VAL,std::less<_Key>,std::allocator<std:air<const _Key, _Tp> > >::iterator’ is parsed as a non-type, but instantiation yields a type
    lookup.cpp:8: note: say ‘typename std::map<KEY,VAL,std::less<_Key>,std::allocator<std:air<const _Key, _Tp> > >::iterator’ if a type is meant

    If you want to see how I am using this in a larger context see
    http://ezekiel.vancouver.wsu.edu/~cs.../mesh/Mesh.cpp

    -much thanks.
    --wayne

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: generic lookup using STL map

    Quote Originally Posted by wayne66
    The STL map's find method returns an iterator which is tedious to program with so I decided to write a templated lookup
    function as follows:
    Please use code tags, and get rid of the smilies in your post.
    Code:
      template <class KEY, class VAL>
      VAL lookup(map<KEY,VAL>& m, KEY key) 
      {
            typename map<KEY,VAL>::iterator iter = m.find(key);
            assert (iter != m.end());
            return iter->second;
      }
    Regards,

    Paul McKenzie

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: generic lookup using STL map

    This code is going to give you problems if you try to use it with a const map.

    Also, consider returning by reference rather than by value.

  4. #4
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: generic lookup using STL map

    The assert is probably not a good way to handle not-found, unless you can be sure that you are never going to look up a key that is not there, i.e. it would be a programming error to do so.

    My own preferencial way to do it is:
    Code:
    template < typename KEY, typename VAL >
    bool map_lookup( const std::map< KEY, VAL > & m, const KEY & k, VAL & v )
    {
       typename std::map< KEY, VAL >::const_iterator it = m.find( k );
       if ( it != m.end() )
       {
           v = it->second;
           return true;
       }
       else
       {
           return false;
       }
    }

  5. #5
    Join Date
    Mar 2008
    Posts
    2

    Re: generic lookup using STL map

    Wow. Thanks for all of the quick and useful replies.
    Now I know *yet another* esoteric detail of C++ -- the
    use of typename for template dependent types.

  6. #6
    Join Date
    Feb 2003
    Posts
    377

    Re: generic lookup using STL map

    If you're going to assume the key is always there, why not just use operator[]?

    Otherwise I like NMTop40's example.

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