array as key value

Hello , is it possible to have an array as a key value?

In a map, or a set of pairs. The 2 possibilities are good for me.
I made this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <string.h>
#include <iostream>
#include <map>
#include <utility>
 
using namespace std;
 
int main()
{	
	map< double*, string> mapa;
	
	double a[3] = {1,2,3};
	double b[3] = {1,0,0};
	
	mapa[a] = "a";
	mapa[b] = "b";
 
   cout << mapa[a] << endl; //This print ok.

   for( map<double*,string>::iterator ii=mapa.begin(); ii!=mapa.end(); ++ii)
   {
       cout << (*ii).first << ": " << (*ii).second << endl;
   }
   // (*ii).first gives me trash.
   
}


OUTPUT :

1
2
3
Map size: 2
0x7fff2cc69240: b
0x7fff2cc69260: a



I have a list of double[3], So i want to make a map<double*, int> to find it there and have the int as a result.

Thank you!
Sure, but you didn't use arrays there, you used pointers.

With arrays, the following works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <string>
#include <iostream>
#include <map>
#include <utility>
#include <array>
 
using namespace std;
 
int main()
{
        map< array<double, 3>, string> mapa;

        array<double, 3> a = {1,2,3};
        array<double, 3> b = {1,0,0};

        mapa[a] = "a";
        mapa[b] = "b";
 
   cout << mapa[a] << endl; //This print ok.

   for(auto ii=mapa.begin(); ii!=mapa.end(); ++ii)
   {
       cout << '{' << ii->first[0] << ','
                   << ii->first[1] << ','
                   << ii->first[2] << "} : " << ii->second << '\n';
   }
}

online demo: http://ideone.com/2Xsee

(if you lack array support, you can use vectors as keys just the same way)
Last edited on
Thank you for the response..
Two questions :
1) What is auto?

2) I tried to compile this on my computer and have this error :


g++ -Wall -c "map2.cpp" (in directory: /home/amadio/testc++)
In file included from /usr/include/c++/4.5/array:35:0,
from map2.cpp:5:
/usr/include/c++/4.5/bits/c++0x_warning.h:31:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
map2.cpp: In function ‘int main()’:
map2.cpp:11:14: error: ‘array’ was not declared in this scope
map2.cpp:11:29: error: wrong number of template arguments (1, should be 4)
/usr/include/c++/4.5/bits/stl_map.h:86:11: error: provided for ‘template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map’
map2.cpp:11:30: error: expected unqualified-id before ‘,’ token
map2.cpp:11:38: error: expected initializer before ‘>’ token
map2.cpp:13:15: error: expected primary-expression before ‘double’
map2.cpp:13:15: error: expected ‘;’ before ‘double’
map2.cpp:14:15: error: expected primary-expression before ‘double’
map2.cpp:14:15: error: expected ‘;’ before ‘double’
map2.cpp:16:9: error: ‘mapa’ was not declared in this scope
map2.cpp:16:14: error: ‘a’ was not declared in this scope
map2.cpp:17:14: error: ‘b’ was not declared in this scope
map2.cpp:21:13: error: ‘array’ cannot appear in a constant-expression
map2.cpp:21:28: error: wrong number of template arguments (1, should be 4)
/usr/include/c++/4.5/bits/stl_map.h:86:11: error: provided for ‘template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map’
map2.cpp:21:29: error: expected unqualified-id before ‘,’ token
map2.cpp:21:37: error: expected initializer before ‘>’ token
map2.cpp:21:56: error: ‘ii’ was not declared in this scope
Compilation failed.


I don't have c++0x ... Is there another way?

Thanks!
As the compiler said, it "must be enabled with the -std=c++0x or -std=gnu++0x compiler options.". Or you could use vectors.
I tried changing array with vector.. but the same error
Sorry for my ignorance..

Can you give me an example ? =)
You could wrap your vector of doubles in a class and provide that class with an operator<

Then it will work as a key in a map

However, I think in the case of an array of doubles this will not make a good key.
This is because operator< will have poorly defined behaviour when the the floating point values are near each other.

So I think you have a bad idea from the start
@mik2718 vector has an operator< already
When you declare the iterator as auto, what definition does it use for ii? Does it use:
 
map< array<double, 3>, string>::iterator
or
 
map< array<double, 3>, string>::const_iterator
Yes, you are right @mik2718 .. Is a bad idea from scratch.. The arrays are representing points..
So definicn operator < will not be good..

I wanted to use predefined "find" .. but i thing I will have to make a map with this definition:

map <string, array <double,3> >

Thank you likewise ! =)
If you compile with -std=c++0x it will work!
Topic archived. No new replies allowed.