associative arrays (hash)

L

Larry

Hi,

how can I go about setting an associative array the way as Perl does?

%hash = {};
$hash{"key1"} = "Value1";
$hash{"key2"} = "Value2";
...etc..

so %hash contains key, value pairs .I can acces a certain value given its
key...can C++ do something like this or it is too much high level approach?

thanks
 
F

feverzsj

Larry wrote,
Hi,

    how can I go about setting an associative array the way as Perl does?

%hash = {};
$hash{"key1"} = "Value1";
$hash{"key2"} = "Value2";
..etc..

so %hash contains key, value pairs .I can acces a certain value given its
key...can C++ do something like this or it is too much high level approach?

thanks

the upcoming c++0x standard will support the so called "Initializer
lists", which could apply primitive type like initialization list.
you may write something like that:

map<std::string,vector<int>> years = {
{ "Maurice",{1913, 1945, 1951, 1967, 2000} },
{ "Martin", {1982, 2003, 2007} },
{ "David", {1927, 1947, 1951, 2004} }
};

the associative container in c++ like std::map, boost::unordered_map
use the reloaded operator[] to access the data, just like the perl
way.

years["Maurice"] = ....
years["Martin"] = ....
 
R

Robert Hairgrove

Larry said:
Hi,

how can I go about setting an associative array the way as Perl does?

%hash = {};
$hash{"key1"} = "Value1";
$hash{"key2"} = "Value2";
..etc..

so %hash contains key, value pairs .I can acces a certain value given
its key...can C++ do something like this or it is too much high level
approach?

thanks

It's called std::map.

STL has to be enabled in the compiler options; it usually is by default
on most implementations. To use it, you must have:
#include <map>
somewhere before it's first used or referenced.
 
J

Jorgen Grahn

It's worth noting that std::map isn't a hashed container, though. For
that you'll have to wait for C++0x or use something like
boost::unordered_map:

But Perl calls "associative arrays" a "hash" -- I don't think the OP
used that word in the strict datalogical sense. (Perhaps you didn't
either.)

I'm a bit worried for the OP, by the way. If he has to ask on Usenet
to find out about std::map, then he desperately needs at least one good
book about C++ before he starts producing code.

....
P.S. Which compilers don't have STL enabled by default, out of interest?
I've never come across one.

Neither have I. If there is no standard library, it's not C++ in any
normal sense.
(Although I once did a programming
competition where almost all the standard headers seemed to have been
conveniently deleted beforehand - just to make it that little bit more
of a pain :))

That's a very strange idea ... I can't see any purpose for that.

/Jorgen
 
L

Larry

map<std::string,vector<int>> years = {
{ "Maurice",{1913, 1945, 1951, 1967, 2000} },
{ "Martin", {1982, 2003, 2007} },
{ "David", {1927, 1947, 1951, 2004} }
};

So, all in all it is just something like this?

std::map<std::string, std::string> hash;

hash["key"] = "value";
hash.insert(std::make_pair("key2", "value2"));
...etc..

correct?
 
J

Jorgen Grahn

map<std::string,vector<int>> years = {
{ "Maurice",{1913, 1945, 1951, 1967, 2000} },
{ "Martin", {1982, 2003, 2007} },
{ "David", {1927, 1947, 1951, 2004} }
};

So, all in all it is just something like this?

std::map<std::string, std::string> hash;

hash["key"] = "value";
hash.insert(std::make_pair("key2", "value2"));
..etc..

correct?

It's hard to see what your question is but yes, if that compiles it's
correct code. That's not all you have to know about std::map to use
it, though.

I think I wrote earlier that you need a book on C++, but at the very
least check out the STL Programmer's guide at http://www.sgi.com/tech/stl/

Pro: it's very readable and complete
Con: it's pre-standard and differs here and there from
reality (e.g. it includes hash_map as a hashed version of
std::map).

/Jorgen
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top