Don Lun
Don Lun

Reputation: 2777

I need help choosing the right c++ container

key is :(x,y) , that means 2 integers.

value is: 0 or 1.

And I have frequently operation of iteration of this hash map.

Which data structure is fit? I am using C++ stl or tr1. Don't consider boost.

Upvotes: 2

Views: 126

Answers (4)

Mark Ingram
Mark Ingram

Reputation: 73625

I suggest the use of a proper hashmap (unlike std::map which isn't a hashmap):

typedef std::tr1::unordered_map<pair<int, int>, bool> MyContainer;
MyContainer m_myContainer;

Upvotes: 2

genpfault
genpfault

Reputation: 52083

map< pair< int, int >, bool > cont;

Upvotes: 3

C.J.
C.J.

Reputation: 16081

I would use a std::hash_map or std::map. I'm not sure which would give you faster performance though.

Upvotes: 0

orlp
orlp

Reputation: 117681

Well, the data format stored obviously is a bool.

And for the key format, if you would know the bitsize of those integers and performance is a must, I would use a associative array (called a map in C++) with twice the bitwidth integers as key:

bool val = true;
uint32 x, y; // key
uint64 combo = x << 32 + y;

std::map<uint64, bool> container;

But this would work fine too and is semantically much better:

std::map<std::pair<int, int>, bool> container.

Upvotes: 4

Related Questions