Thread: Indexing arrays

  1. #1
    Registered User
    Join Date
    Dec 2002

    Cool Char Indexing arrays

    I am trying to create an array of objects where the index value is a string instead of an int, for example instead of:

    array[0] = object1;
    array[1] = object2;
    I would like to have:

    array[A2] = object1;
    array[E4] = object2;
    Please can anyone tell me whether this is possible. I know the syntax for creating a usual array:

    ClassName arrayname[arraysize]

    But what would be the syntax to create an array with the string indexes.

    Thank you for any help you can provide.

    Last edited by Dutch; 12-09-2002 at 01:53 PM.

  2. #2
    Neoseeker's master master2000's Avatar
    Join Date
    Dec 2002
    sorry Dutch no clue
    missles on metriods

  3. #3
    Join Date
    Nov 2002
    Array indexes have to be numbers,

    if you want to make the numbers more meaningful in the program you can use #define or const , but you can't index arrays with strings ( but why would you want to anyway? ).

    #define THIRD_THING 2
    const int THIRD_THING = 2;

    int array[3];

    array[THIRD_THING] = 49;

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    Look into the map and multimap objects. For the sake of argument, let's assume that your object1 and object2 variables are integer's, you could create such an array as you are talking about by doing this:

    #include <map>
    #include <string>
    using std::string;
    using std::map;
    map<string,int> MyArray;
    int object1, object2;
    MyArray["A2"] = object1;
    MyArray["E4"] = object2;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User Pioneer's Avatar
    Join Date
    Dec 2002
    You can't, array indexes have to be int.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Arrays Indexing Intialization
    By jshamilton73 in forum C++ Programming
    Replies: 7
    Last Post: 06-06-2003, 02:26 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM