Better way to do this ?

so I am trying to make map with 3 or more multiple key

1
2
3
map< int, map< int, map<int, data_t > > > Map;

Map[30][40][50] = data_t(); // to call ? 


Is there better way to do this ????
1
2
3
4
5
6
7
8
struct Key
{
    int val[3];
};

// overload the < operator for this struct as needed

std::map< Key, data_t > Map;
Last edited on
Would you say that it's far more efficient than my example ?

I think that it depends on the data itself

But on average case which one usually perform better ?
std::map<std::array<int, 3>, data_t> Map;
Map
Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order.

In a map, the key values are generally used to sort and uniquely identify the elements, while the mapped values store the content associated to this key. The types of key and mapped value may differ, and are grouped together in member type value_type, which is a pair type combining both:


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <map>
using std::map;
using std::pair;

typedef pair<int, int> pii;
typedef map<int, pii> mpi;

int main() {
	int x(1), y(1), z(1);
	mpi coord;
	coord[x] = pii(y, z);
        return 0;
}


You can do it this way or alternatively, you can decide to put all three values in a struct and do it this way:

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 <iostream>
using std::ostream;
using std::cout;

struct coord {
	int x, y, z;
	coord(){}
	coord(int a, int b, int c) : x(a), y(b), z(c) {}
	coord &operator = ( const coord& other) {
		x = other.x;
		y = other.y;
		z = other.z;
		return *this;
	}
	friend ostream &operator << (ostream &out, const coord& other) {
		out << "( " << other.x << ", " << other.y << ", " << other.z << " )";
		return out;
	}
};

int main() {
	coord mmap(1, 2, 3), omap(7, 8, 9);
	cout << mmap << "\n" << omap << '\n';
	mmap = omap;
	cout << mmap << "\n";
	return 0;	
}
Last edited on

@Smac89


#include <map>
#define pii std::pair<int, int>


It is a bad practice to use define. Use instead typedef or using.

Last edited on
Ok, I fixed it
From what I can tell (in my very noobish knowledge) is that the OP wants to map an ordering of 3-ints to one value (of type data_t). Are most of the solutions here (except vlads) doing that? Am I missing something?
Like I said: Noob Commenting...
@Script Coder


Disch should add to the structure operator <.:)
I now understand Disch's solution :)
Thanks vlad :)
@Smac89
for class shouldn't something like this be added ???

1
2
3
4
5
6
7
bool operator< (const coord& lhs, const coord& rhs){
    if( lhs.x< rhs.x) return true;
    if( lhs.y< rhs.y) return true;
    if( lhs.z< rhs.z) return true;

    return false;
}


Vlad solution looks great

1
2
3
4
std::map<std::array<int, 3>, data_t> Map;

Map[ array<int,3>{ 30,40, 50 } ] = data_t();


From my point of view my solution looks best for me but it does feel it is going to waste more memory I guess I will go with class to maximize compability ?
since sometimes they are not all integer sometime they are combination of 2 strings and int or other

Thanks everyone
I think you can use simpler though I did not test

Map[ { 30,40, 50 } ] = data_t();
I've tried that before but
It produce error Like

c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_map.h|450|note: no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const key_type& {aka const std::array<int, 3u>&}'|


I guess that type of casting doesn't exist
Topic archived. No new replies allowed.