wayne66
March 8th, 2008, 08:34 PM
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::pair<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::pair<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::pair<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::pair<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/~cs548/projects/mesh/Mesh.cpp
-much thanks.
--wayne
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::pair<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::pair<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::pair<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::pair<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/~cs548/projects/mesh/Mesh.cpp
-much thanks.
--wayne