472,958 Members | 2,091 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Can associative arrays be implemented by operator overloading?


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
5 1634
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
4
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or...
8
by: Derek Basch | last post by:
Is there any way to associate name/value pairs during an array initialization? Like so: sType = "funFilter" filterTypeInfo = ; filterTypeInfo = new Array("type" : sType); I can do it using...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.