newbie question: map<string, int[2]>

Discussion in 'C++' started by Hyrum Mortensen, Jul 25, 2003.

  1. Hi,

    If I create a structure like this:
    map<string, int[2]> myMap;

    How do I access the first element of the array? I get a compiler's
    error if I try something like this: myMap["foo"][0]

    If the above structure is NOT possible, what is something that's
    equivalent?
     
    Hyrum Mortensen, Jul 25, 2003
    #1
    1. Advertising

  2. Hyrum Mortensen

    Rolf Magnus Guest

    Hyrum Mortensen wrote:

    > Hi,
    >
    > If I create a structure like this:
    > map<string, int[2]> myMap;


    You can't. Arrays are not assignable, and so they cannot be the element
    type of any standard container.

    > How do I access the first element of the array? I get a compiler's
    > error if I try something like this: myMap["foo"][0]
    >
    > If the above structure is NOT possible, what is something that's
    > equivalent?


    struct twoInts
    {
    int data[2];
    }

    std::map<std::string, twoInts> myMap;

    access it like:

    myMap["foo"].data[0];
     
    Rolf Magnus, Jul 25, 2003
    #2
    1. Advertising

  3. "Hyrum Mortensen" <> wrote...
    > If I create a structure like this:
    > map<string, int[2]> myMap;


    You cannot. Arrays cannot be used for values in standard containers.
    They do not satisfy the Assignable requirement.

    > How do I access the first element of the array? I get a compiler's
    > error if I try something like this: myMap["foo"][0]


    Of course.

    > If the above structure is NOT possible, what is something that's
    > equivalent?


    You need to wrap the array into a class:

    struct TwoInts {
    int a, b;
    int& operator[](int i) {
    if (i == 0) return a;
    else if (i == 1) return b;
    else throw "Out of range";
    }
    };

    then you can use it with a map:

    map<string, TwoInts> myMap;

    map["foo"][0] = 42;

    Victor
     
    Victor Bazarov, Jul 25, 2003
    #3
  4. Hyrum Mortensen

    Kapil Khosla Guest

    (Hyrum Mortensen) wrote in message news:<>...
    > Hi,
    >
    > If I create a structure like this:
    > map<string, int[2]> myMap;
    >
    > How do I access the first element of the array? I get a compiler's
    > error if I try something like this: myMap["foo"][0]
    >
    > If the above structure is NOT possible, what is something that's
    > equivalent?


    This code might help.
    // This is the main project file for VC++ application project
    // generated using an Application Wizard.

    #include "stdafx.h"

    #using <mscorlib.dll>
    #include <map>
    #include <string>

    using namespace System;

    struct twoInts
    {
    int data[2];
    }myStruct;

    int main()
    {
    // Create your data set to be entered into the MAP

    myStruct.data[0] = 0;
    myStruct.data[1] = 1;

    std::map<std::string, twoInts> myMap;
    // Insert it into the MAP

    myMap["one"] = myStruct;


    return 0;
    }
     
    Kapil Khosla, Jul 25, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,254
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,716
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    485
    Dave Thompson
    Jul 31, 2006
  4. Angus
    Replies:
    3
    Views:
    359
  5. albert kao
    Replies:
    12
    Views:
    624
    Roedy Green
    Oct 7, 2011
Loading...

Share This Page