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"] = ....
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.
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:
http://www.boost.org/doc/libs/1_41_0/doc/html/boost/unordered_map.html
Cheers,
Stu
P.S. Which compilers don't have STL enabled by default, out of interest?
I've never come across one. (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 :))
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
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
I wasn't sure what he meant so I thought it was worth clarifying. I'm
not massively familiar with Perl unfortunately (I should probably
rectify that). He'll almost certainly want std::map rather than
boost::unordered_map in practice - unless he actually does want a hashed
container of course.
> 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.
Depends how seriously he's going to be using C++ - but I'm surprised
that he couldn't find it with Google to be honest. (Googling "C++
associative array" works for me...)
> ...
>> 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.
Me neither - not sure it was done deliberately. Quite frustrating having
to hack up your own vector/list/sort function, etc. when you're in a
timed event though.
Stu
> /Jorgen
> 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).