getting values in a map - map wrapper class

A

Angus

I am writing a class as a wrapper around a std::map.

The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>

At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]

Eg if I create a:
CGeneralMap<int, std::string> TestMap;

and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?


In my GetValue function I tried:

value = [3];

but that doesn't work.
 
C

Chris ( Val )

I am writing a class as a wrapper around a std::map.

The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>

At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]

Eg if I create a:
CGeneralMap<int, std::string> TestMap;

and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?

In my GetValue function I tried:

value = [3];

but that doesn't work.


std::string getKeyValue( int index ) const
{
// Code...

return TestMap[ index ];
}
 
A

Angus

I am writing a class as a wrapper around a std::map.
The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>
At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]
Eg if I create a:
CGeneralMap<int, std::string> TestMap;
and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?
In my GetValue function I tried:
value = [3];
but that doesn't work.

std::string getKeyValue( int index ) const
{
// Code...

return TestMap[ index ];
}

Yes of course I understand that. But I am talking about from within
my class. From within my GetValue function.
 
C

Chris ( Val )

I am writing a class as a wrapper around a std::map.
The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>
At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]
Eg if I create a:
CGeneralMap<int, std::string> TestMap;
and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?
In my GetValue function I tried:
value = [3];
but that doesn't work.
std::string getKeyValue( int index ) const
{
// Code...
return TestMap[ index ];
}
- Show quoted text -

Yes of course I understand that. But I am talking about from within
my class. From within my GetValue function.- Hide quoted text -

What am I missing?
What I showed you can be a member function if you put it in a class.
 
A

Angus

Angus said:
I am writing a class as a wrapper around a std::map.
The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>
At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]
Eg if I create a:
CGeneralMap<int, std::string> TestMap;
and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?
In my GetValue function I tried:
value = [3];
but that doesn't work.

You could of course use
value = (*this)[3];

But I think the "optimal" solution will be
value = operator[](3);

Both of those call the "operator[]" of the std::map, which is what you want.

Cheers,
Daniel

--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress--so
please use good, old E-MAIL!- Hide quoted text -

- Show quoted text -

Ah brilliant, that is what I needed. But now I have a conversion
problem.

If on line 366 I have:
Value = operator[](Key);

and on line 367 I have:
Value = (*this)[Key];

Then I get these compilation errors in Microsoft Visual C++ version 6:
TestBed.cpp


generalmap.h(366) : error C2662: '[]' : cannot convert 'this' pointer
from


'const class CGeneralMap<int,class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > >'

to

'class std::map<int,class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >,struct
std::less<int>,class std::allocator<class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> > > > &'

Conversion loses qualifiers
c:\program files\microsoft visual studio\vc98\include
\xmemory(59) : while compiling class-template member function 'void
__thiscall CGeneralMap<int,class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > >::G
etValue(const int,class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > &) const'



generalmap.h(367) : error C2678: binary '[' : no operator defined
which takes a left-hand operand of type 'const class
CGeneralMap<int,class std::basic_string<char,struct std::char_traits<
char>,class std::allocator<char> > >' (or there is no acceptable
conversion)
c:\program files\microsoft visual studio\vc98\include
\xmemory(59) : while compiling class-template member function 'void
__thiscall CGeneralMap<int,class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > >::G
etValue(const int,class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > &) const'

ie I am trying both options.
 
A

Angus

Angus said:
I am writing a class as a wrapper around a std::map.
The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>
At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]
Eg if I create a:
CGeneralMap<int, std::string> TestMap;
and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?
In my GetValue function I tried:
value = [3];
but that doesn't work.

You could of course use
value = (*this)[3];

But I think the "optimal" solution will be
value = operator[](3);

Both of those call the "operator[]" of the std::map, which is what you want.

Cheers,
Daniel

--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress--so
please use good, old E-MAIL!- Hide quoted text -

- Show quoted text -

Its ok I had to fiddle with const qualifiers. It now works fine.
 
C

Chris ( Val )

I am writing a class as a wrapper around a std::map.
The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>
At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]
Eg if I create a:
CGeneralMap<int, std::string> TestMap;
and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?
In my GetValue function I tried:
value = [3];
but that doesn't work.
std::string getKeyValue( int index ) const
{
// Code...
return TestMap[ index ];
}

Yes of course I understand that. But I am talking about from within
my class. From within my GetValue function.

In future, please try to describe what you're having difficulty
with a bit more clearly. You stated that you were having difficulty
with your member function "GetValue", not operator[] which is quite
different.

While you're at it, ensure that you check for the key's existence.
 
G

Guest

I am writing a class as a wrapper around a std::map.

The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>

At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]

Eg if I create a:
CGeneralMap<int, std::string> TestMap;

and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?


In my GetValue function I tried:

value = [3];

but that doesn't work.


You should use the find() method for a GetValue method since there is no
guarantee that the key specified exists. Using the [] operator will
create an entry if it does not exist which is not what you want from a
GetValue method.
 
C

Chris ( Val )

On 2007-10-01 16:28, Angus wrote:
[snip]
In my GetValue function I tried:
value = [3];
but that doesn't work.

You should use the find() method for a GetValue method since there is no
guarantee that the key specified exists. Using the [] operator will
create an entry if it does not exist which is not what you want from a
GetValue method.

That was exactly the thought I had, which led me to believe
the OP didn't want operator[].
 
D

Daniel Kraft

Angus said:
I am writing a class as a wrapper around a std::map.

The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>

At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]

Eg if I create a:
CGeneralMap<int, std::string> TestMap;

and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?


In my GetValue function I tried:

value = [3];

but that doesn't work.

You could of course use
value = (*this)[3];

But I think the "optimal" solution will be
value = operator[](3);

Both of those call the "operator[]" of the std::map, which is what you want.

Cheers,
Daniel
 
J

James Kanze

std::string getKeyValue( int index ) const
{
// Code...
return TestMap[ index ];
Can std::map<> operator [](size_t) be used in a const member function?

Of course not.

The first problem is that he is deriving from std::map, when he
probably should be using containment; derivation is a very poor
way to "wrap" something. But having an std::map as a member
doesn't change the problem: you still can't use operator[] in a
const function.

The real question, of course, is what should happen if the value
isn't present in the map. Depending on what the map is used
for, several possibilities can be considered:

-- It's an error. The user should check beforehand (e.g. with
a contains() function which you provide). If the value
isn't present, it's an assertion failure, or possibly only
an exception.

-- It's an expected condition. The simplest solution here is
to return a pointer, returning NULL if the element isn't
present.

-- The element is by definition present, with a default value.
Basically, you implement something like the precedent, but
return "ptr == NULL ? defaultValue : *ptr".

-- The element is automatically inserted, with a default value.
This is the behavior of std::map, but it means that you
cannot read a const map.

The function behind whatever you do (except for the last) is
std::map<>::find. For the second solution (which is the most
general, and easiest to map into the other solutions), you do
something like:

ValueType const*
Map::get( KeyType const& key ) const
{
std::map<...>::const_iterator
elem = myMap.find( key ) ;
return elem == myMap.end()
? NULL
: &elem->second ;
}
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top