By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,864 Members | 1,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,864 IT Pros & Developers. It's quick & easy.

Can associative arrays be implemented by operator overloading?

P: n/a

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

Nov 20 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Ramon F Herrera wrote:
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,intmyMap;

myMap["ramon"] = 42.

--
Ian Collins.
Nov 20 '07 #2

P: n/a
On Nov 19, 11:40 pm, Ian Collins <ian-n...@hotmail.comwrote:
Ramon F Herrera wrote:
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.
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,intmyMap;

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.
Nov 20 '07 #3

P: n/a
On Nov 19, 11:40 pm, Ian Collins <ian-n...@hotmail.comwrote:
Ramon F Herrera wrote:
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,intmyMap;

myMap["ramon"] = 42.

--
Ian Collins.

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

Nov 20 '07 #4

P: n/a
On Nov 21, 10:53 am, Ramon F Herrera <ra...@conexus.netwrote:
On Nov 19, 11:40 pm, Ian Collins <ian-n...@hotmail.comwrote:
std::map<std::string,intmyMap;
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, intitem )
{
std::cout << "Key '" << item.first << "', value '" << item.second <<
"'\n";
}

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

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

return 0;
}

Nov 20 '07 #5

P: n/a
On Nov 21, 7:25 am, Old Wolf <oldw...@inspire.net.nzwrote:
On Nov 21, 10:53 am, Ramon F Herrera <ra...@conexus.netwrote:
On Nov 19, 11:40 pm, Ian Collins <ian-n...@hotmail.comwrote:
std::map<std::string,intmyMap;
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.hor
#include<cstdio>. There are differences between the two; IIRC the
most important difference is that functions included via <c...are in
the std namespace.
>
void show_item( std::pair<std::string, intitem )
{
std::cout << "Key '" << item.first << "', value '" << item.second <<
"'\n";
std::cout is equivalent to stdout.
>
}

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

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

return 0;

}
Nov 21 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.