473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

const map templated 'find'

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
Jan 26 '06 #1
3 1662
shaun wrote:
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
Jan 26 '06 #2

"shaun" <sh*******@cern.ch> wrote in message
news:sh*****************************@sunnews.cern. ch...
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
Jan 26 '06 #3
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.

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
Jan 27 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael H Lees | last post by:
Hi there, I'm trying to use an stl map where I have my own defined (templated) class as a key. I have the class "variableId" below which I want to use as the key for an stl map. It has two...
5
by: Bolin | last post by:
Hi all, A question about smart pointers of constant objects. The problem is to convert from Ptr<T> to Ptr<const T>. I have look up and seen some answers to this question, but I guess I am too...
2
by: John Stiles | last post by:
I have written some pretty simple code which works great in Dev Studio and CodeWarrior, but gcc is giving me link errors. Here is what I've been able to distill it down to: namespace Private {...
6
by: Alex | last post by:
I have been loving the templated datacolumns of the datagrid. I have stumbled onto a problem though that is beyond by knowledge and I was hoping someone here could jumpstart me. My templated...
4
by: hweekuan | last post by:
Hi, I got an error with gnu C++ but not with intel C++ (icc). The distilled code is given below, a variable "T" is stored as a const T& in the base class and when the derived class try to access...
13
by: dragoncoder | last post by:
Hi everyone, please consider the following function:- const int& foo ( const double& d ) { return d; } g++ compiles it with warnings and solaris CC gives error. I want to know if the code...
3
by: =?Utf-8?B?U3VybWVldCBKYW5kdQ==?= | last post by:
Hi, Can any body tell me that instead of "const" what can we use in MC++. Thanks in advance!!
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
8
by: Adem24 | last post by:
The compiler which I have to use in a project doesn't like the following construct. It says: 'bitset' : invalid template argument. It means that it wants only a static-like const N as a template...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.