Hi, I'm wondering if it's possible to use a 'custom' array index in C++ like you can do with PHP. For example in PHP: $array = array('foo' => 'bar'); echo $array['foo']; //Output: bar PHP: Can this be done in C++? I tried: char arary[1] = {'foo' => 'bar'}; Code (markup): but I get this compile error: Thanks!
Unfortunately you can't do this in C++. Perl is an interpreted language and this is the reason that such things (as well as a dynamic index) are allowed.
Thanks for the info, is this possible with Java? I thought Java is an interpreted language or am I wrong?
With Java too this is not possible. Java in itself is not interpreted, its bytecodes are interpreted by the java virtual machine. Maybe with the map said by the fellow above you can do this in C++.
It isn't possible to index a normal C++ array in the way you asked but it is possible to get quite close to the array index syntax using the C++ map class in the STL. Here's an example: #include <string> #include <map> using namespace std; int main() { map<string, string> MapValues; MapValues["foo"] = "bar"; MapValues["dog"] = "rover"; MapValues["cat"] = "felix"; return 0; } This does allow dynamic creation of array-like structured data that can be indexed by strings.
In Java you could use the hashmap. http://www.javadeveloper.co.in/java-example/java-hashmap-example.html http://java.sun.com/javase/6/docs/api/java/util/HashMap.html
It is possible, use map as directed by LogicFlux or use simplified precreated class available here: http://www.apitalk.com/c-And-C-Plus...enting-Associative-Arrays-In-C-Plus-Plus.html regards