473,386 Members | 1,715 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,386 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 1641
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.