const map templated 'find'

S

shaun

I'm doing some work with const maps of various types and I find that I'm
writing the same few lines of code over and over:

(having declared and initialized a map<myTypeA, myTypeB> myMap)


myTypeB errvalue(anErrorValue);

map<myTypeA, myTypeB>::const_iterator ppair(myMap.find(theKey));
myTypeB foundValue = (ppair not_eq myMap.end())?ppair->second:errValue;


I'd like to template this as :

template <class S, class T>
T constFind(const map< S, T > & theMap, const S & theKey, const T &
errVal){
map< S, T>::const_iterator ppair(theMap.find(theKey));
return (ppair not_eq theMap.end())?(ppair->second):errVal;
}

and then in the body of the code just have

theValue = constFind(myMap, aKey, anErrVal);


on one of my compilers (gcc 3.2.3) this appears to compile (havent tried
running it), on the other (gcc 4.0.0) it doesn't. (I can't guarantee
that all compiler options are the same)

If I change the type of ppair to be pair< S, T> *, the situation is
reversed; either way, I don't trust that the code will work as I think
it should and for the moment I am overloading a 'constFind' function for
the specific maps I am using.

Is it possible to write a more generic templated version?

cheers

shaun
 
V

Victor Bazarov

shaun said:
I'm doing some work with const maps of various types and I find that I'm
writing the same few lines of code over and over:

(having declared and initialized a map<myTypeA, myTypeB> myMap)


myTypeB errvalue(anErrorValue);

map<myTypeA, myTypeB>::const_iterator ppair(myMap.find(theKey));
myTypeB foundValue = (ppair not_eq myMap.end())?ppair->second:errValue;


I'd like to template this as :

template <class S, class T>
T constFind(const map< S, T > & theMap, const S & theKey, const T &
errVal){
map< S, T>::const_iterator ppair(theMap.find(theKey));
return (ppair not_eq theMap.end())?(ppair->second):errVal;
}

and then in the body of the code just have

theValue = constFind(myMap, aKey, anErrVal);


on one of my compilers (gcc 3.2.3) this appears to compile (havent tried
running it), on the other (gcc 4.0.0) it doesn't.

And what does it say? Remember, FAQ 5.8?...
> (I can't guarantee
that all compiler options are the same)

I don't think I could care less about those.
If I change the type of ppair to be pair< S, T> *, the situation is
reversed;

I.e. g++ 3.2.3 doesn't compile, right? What does it say?
> either way, I don't trust that the code will work as I think
it should and for the moment I am overloading a 'constFind' function for
the specific maps I am using.

Is it possible to write a more generic templated version?

Keep 'ppair' an iterator. Let's make it work with that.

V
 
C

Cy Edmunds

shaun said:
I'm doing some work with const maps of various types and I find that I'm
writing the same few lines of code over and over:

(having declared and initialized a map<myTypeA, myTypeB> myMap)


myTypeB errvalue(anErrorValue);

map<myTypeA, myTypeB>::const_iterator ppair(myMap.find(theKey));
myTypeB foundValue = (ppair not_eq myMap.end())?ppair->second:errValue;


I'd like to template this as :

template <class S, class T>
T constFind(const map< S, T > & theMap, const S & theKey, const T &
errVal){
map< S, T>::const_iterator ppair(theMap.find(theKey));
return (ppair not_eq theMap.end())?(ppair->second):errVal;
}

and then in the body of the code just have

theValue = constFind(myMap, aKey, anErrVal);


on one of my compilers (gcc 3.2.3) this appears to compile (havent tried
running it), on the other (gcc 4.0.0) it doesn't. (I can't guarantee
that all compiler options are the same)

If I change the type of ppair to be pair< S, T> *, the situation is
reversed; either way, I don't trust that the code will work as I think
it should and for the moment I am overloading a 'constFind' function for
the specific maps I am using.

Is it possible to write a more generic templated version?

cheers

shaun

Try this:

template <typename MAP>
const typename MAP::mapped_type &
const_find(const MAP &map, const typename MAP::key_type &key)
{
typename MAP::const_iterator i = map.find(key);
if (i == map.end())
{
// deal with key not found
}
return i->second;
}

Cy
 
S

shaun

Keep 'ppair' an iterator. Let's make it work with that.

V

Indeed the compilers gave me a helpful clue, that really what I meant
was:

typename map< S, T>::const_iterator ppair(theMap.find(theKey));

and suddenly all is well.

cheers

shaun
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top