I want to use a map
to store key-value pairs.
The key of the map should contain information about the coordinates(int
) of one point.
One possibility is to convert the int
s to string
. For example, coordinate(x,y) can be represented as "x#y"
and store this string "x#y"
as a key.
Another possibility is to use a pair to store the coordinates as pair<int, int>
and using this pair
as key.
Which way is a better approach and why ?
This depends on your definition of efficient, and we very quickly devolve into what might be considered premature optimization. There are a lot of things at play, and by the way you phrased your question I think we should take a very simplistic look:
Your primary considerations are probably:
And let's assume that on your system:
int
is 4 bytesstd::string
(which is implementation-dependent)Storage
std::pair<int,int>
requires 8 bytesSpeed
std::pair<int,int>
requires at most two integer comparisons, which is fast on most processorsInitialization
std::pair<int,int>
initialization is simple and fastAlready you can see that at face value, using strings might be crazy... That is, unless you have some other important reason for it.
Now, should you even use std::pair<int,int>
? It might be overkill. As an example, let's say you only store values that fit in the range [0,65535]. In that case, std::pair<uint16_t,uint16_t>
would be sufficient, or you could pack the two values into a single uint32_t
.
And then others have mentioned hashing, which is fine provided you require fast lookup but don't care about iteration order.
I said I'd keep this simplistic, so this is where I'll stop. Hopefully this has given you something to think about.
One final caution is this: Don't overthink your problem -- write it in the simplest way, and then TEST if it suits your needs.
1st, coordinate can be double number so I think pair< double , double > would be better choice.
2nd , if you really want to use int pair or string key, pair< int , int> would be better choice as string will always create more capacity than it's actual length. Basically, You will loose some unused memory for each string key. string.length() value can be equal to or less than string.capacity() value..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With