DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. clay Guest

## map objects

could you explain the operation of the following function:

typedef map<int, double> maptype;
typedef pair<const int, double> Pair;

int main()
{
Pair a[3] =
{ Pair(20, 1.5),
Pair(800, 0.3),
Pair(3, 0.2)
};
maptype MA;
maptype MB(a, a + 3);
maptype MC(MB);
cout << MC[800] << endl;
return 0;
}

2. Igor Soukhov Guest

## Re: map objects

Hi clay !
"clay" <magilla@cfl.rr.com> wrote:
>could you explain the operation of the following function:

Sure ! =)

>typedef map<int, double> maptype;
>typedef pair<const int, double> Pair;

>
>int main()
>{
> Pair a[3] =
> { Pair(20, 1.5),
> Pair(800, 0.3),
> Pair(3, 0.2)
> };
> maptype MA;
> maptype MB(a, a + 3);

initialize map with array's data
these code can be replaced with this code:

maptype MB;
MB[20] = 1.5;
MB[800] = 0.3;
MB[3] = 0.2;

> maptype MC(MB);

create a copy of MB

> cout << MC[800] << endl;

"0.3" should be printed

> return 0;
>}

3. Danny Kalev Guest

## Re: map objects

clay wrote:
>
> could you explain the operation of the following function:

a map is an associative array that binds two values: a key and its
associated value. These two values are called pairs. in your example,
the map contains pairs of int and double. In other words, the int is a
key and the double is the value associated with it.
>
> typedef map<int, double> maptype;
> typedef pair<const int, double> Pair;
>
> int main()
> {
> Pair a[3] =
> { Pair(20, 1.5),
> Pair(800, 0.3),
> Pair(3, 0.2)
> };
> maptype MA;
> maptype MB(a, a + 3);

in the above line the map is populated with three pairs -- the elements
of the array a.

> maptype MC(MB);

MB is copied into a new map called MC.

> cout << MC[800] << endl;

The pair a[1] has the key 800 and the associated value 0.3. This line
prints the value associated with the key 800, namely 0.3.

Danny

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise