Can associative arrays be implemented by operator overloading?

R

Ramon F Herrera

I am looking for a good excuse to pick up C++. I have been a satisfied
C programmer for years, a language that provides me all the facilities
that I need, with ONE exception.

I keep on finding programming problems that can be nicely solved by
the use of relational arrays. I wish I could count on the simplicity
of expression afforded by Java:

array.put(key, data);

or -even better- by Perl:

pictureOf{"ramon"} = myimage.gif; // or something to that effect


My question is two-fold:

(1) Which relational array implementation should I use? A while ago I
posted a similar question in this NG and I was surprised to read that
one of the recommended packages was Berkeley DB from Sleepycat (Oracle
property these days). Since I was referring to RAM-based arrays, I was
told that all I had to do is keep it in RAM instead of the normal disk-
based database file. Berkeley DB has a heavier footprint for what I
have in mind. Isn't there an implementation of relational arrays in
the standard, or even in the de facto commonly used C++ libraries?

(2) Let's say I found a perfect implementation that suits my needs. Is
there a way to overload the bracket operators to make them behave like
in Perl? Please don't tell me that the only interesting/elegant case
of operator overloading is in complex arithmetic!

TIA,

-Ramon
 
I

Ian Collins

Ramon said:
I am looking for a good excuse to pick up C++. I have been a satisfied
C programmer for years, a language that provides me all the facilities
that I need, with ONE exception.

I keep on finding programming problems that can be nicely solved by
the use of relational arrays. I wish I could count on the simplicity
of expression afforded by Java:

array.put(key, data);

or -even better- by Perl:

pictureOf{"ramon"} = myimage.gif; // or something to that effect


My question is two-fold:

(1) Which relational array implementation should I use?

Heterogeneous or homogeneous? For the latter, std::map probably gives
you what you want, for the former, look at boost.
(2) Let's say I found a perfect implementation that suits my needs. Is
there a way to overload the bracket operators to make them behave like
in Perl? Please don't tell me that the only interesting/elegant case
of operator overloading is in complex arithmetic!
Given:

std::map<std::string,int> myMap;

myMap["ramon"] = 42.
 
R

Ramon F Herrera

Heterogeneous or homogeneous? For the latter, std::map probably gives
you what you want, for the former, look at boost.

Hadn't thought about it, but my stuff is pretty homogeneous.
(2) Let's say I found a perfect implementation that suits my needs. Is
there a way to overload the bracket operators to make them behave like
in Perl? Please don't tell me that the only interesting/elegant case
of operator overloading is in complex arithmetic!
Given:

std::map<std::string,int> myMap;

myMap["ramon"] = 42.

I am sold! Let the C++ cramming begin...

Thanks++! (*)

-Ramon

(*) don't want to imply that the question is closed, as I am very
interesting on the whole topic.
 
R

Ramon F Herrera

Ramon said:
I am looking for a good excuse to pick up C++. I have been a satisfied
C programmer for years, a language that provides me all the facilities
that I need, with ONE exception.
I keep on finding programming problems that can be nicely solved by
the use of relational arrays. I wish I could count on the simplicity
of expression afforded by Java:
array.put(key, data);
or -even better- by Perl:
pictureOf{"ramon"} = myimage.gif; // or something to that effect
My question is two-fold:
(1) Which relational array implementation should I use?

Heterogeneous or homogeneous? For the latter, std::map probably gives
you what you want, for the former, look at boost.


(2) Let's say I found a perfect implementation that suits my needs. Is
there a way to overload the bracket operators to make them behave like
in Perl? Please don't tell me that the only interesting/elegant case
of operator overloading is in complex arithmetic!

Given:

std::map<std::string,int> myMap;

myMap["ramon"] = 42.


I am really a newbie when it comes to the "::" notation. Can somebody
please convert the above into an actual snippet? (i.e, code that
compiles)?

Thanks!

-Ramon
 
O

Old Wolf

std::map<std::string,int> myMap;
myMap["ramon"] = 42.

I am really a newbie when it comes to the "::" notation. Can somebody
please convert the above into an actual snippet? (i.e, code that
compiles)?

#include <string>
#include <map>
#include <algorithm>
#include <iostream>

void show_item( std::pair<std::string, int> item )
{
std::cout << "Key '" << item.first << "', value '" << item.second <<
"'\n";
}

int main()
{
std::map<std::string, int> myMap;
myMap["ramon"] = 42;
myMap["joe"] = 22;

for_each( myMap.begin(), myMap.end(), show_item );

return 0;
}
 
A

alan

std::map<std::string,int> myMap;
myMap["ramon"] = 42.
I am really a newbie when it comes to the "::" notation. Can somebody
please convert the above into an actual snippet? (i.e, code that
compiles)?
C++ supports a concept called "namespace". Basically, you can do
something like this:
#include<assert>
namespace mynamespace{
int variable;
}
namespace hisnamespace{
int variable;
}

int main(){
mynamespace::variable = 42;
hisnamespace::variable = 54;
assert(mynamespace::variable != hisnamespace::variable);
assert(&mynamespace::variable != &hisnamespace::variable);
return 0;
}

Note, however, that :: notation is used for more than just namespaces
(it's just that in the code shown it's for namespaces, and in my
experience you will more commonly use it for namespaces).

It is also used with class names and members.
class foo{
public:
int member();
}

int foo::member(){
std::cout << "this is foo's member()!" << std::endl;
}
#include <string>
#include <map>
#include <algorithm>
#include <iostream>
Note how the include filenames look like. The standard C++ headers do
not end in ".h". Secondly, you can include some C standard headers
using two different variants: something like #include<stdio.h> or
#include<cstdio>. There are differences between the two; IIRC the
most important difference is that functions included via said:
void show_item( std::pair<std::string, int> item )
{
std::cout << "Key '" << item.first << "', value '" << item.second <<
"'\n";
std::cout is equivalent to stdout.
}

int main()
{
std::map<std::string, int> myMap;
myMap["ramon"] = 42;
myMap["joe"] = 22;

for_each( myMap.begin(), myMap.end(), show_item );

return 0;

}
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top