Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > newbie question: map<string, int[2]>

Reply
Thread Tools

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

 
 
Hyrum Mortensen
Guest
Posts: n/a
 
      07-25-2003
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?
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      07-25-2003
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];
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-25-2003
"Hyrum Mortensen" <(E-Mail Removed)> 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


 
Reply With Quote
 
Kapil Khosla
Guest
Posts: n/a
 
      07-25-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Hyrum Mortensen) wrote in message news:<(E-Mail Removed). com>...
> 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;
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie with newbie questions JohnE ASP .Net 3 08-17-2009 10:10 PM
VONAGE Newbie w/newbie question New_kid@nowhere.new VOIP 0 08-11-2007 01:40 PM
another newbie question from another newbie.... Lee UK VOIP 4 05-17-2005 04:10 PM
newbie: cisco vlan newbie question No Spam Cisco 3 06-07-2004 10:02 AM
Newbie! I'm a newbie! What's wrong with this program? Id0x Python 4 07-20-2003 11:40 PM



Advertisments