user188276
user188276

Reputation:

which is better in general, map or vector in c++?

As I know that accessing an element in vector takes constant time while in map takes logarithmic time. However, storing a map takes less memory than storing a vector.

Therefore, I want to ask which one is better in general? I'm considering using one of those two in my program, which has about 1000 elements. I plan to use 3 dimensional vector, which would take 1000x1000x1000 elements.

Upvotes: 0

Views: 3188

Answers (11)

Clifford
Clifford

Reputation: 93456

It is frankly rediculous to suggest that you creat 1,000,000,000, empty objects to store just 1000 useful ones. If you think a vector will be faster, you are probably very wrong when you consider all the swapfile thrashing, dynamic memory allocation, and unnecessary object construction that will ensue!

The description of your application is probably too vague; but if a value can be indexed by x,y,z; then use an object containing x,y,z as the map key. It will not take long to locate just 1000 objects - that is a very small map.

Upvotes: 2

Emile Cormier
Emile Cormier

Reputation: 29209

If your 3D matrix will be sparsely populated (i.e. mostly zeros), then you'll be interested in boost::ublas::sparse_matrix. If I recall correctly, by default, it uses std::map as the underlying container. It provides operators for easy row/column indexing (as well as row/column/element iterators).

EDIT: Nevermind, I thought boost::ublas had 3D matrices. It seems that it doesn't. It also seems that sparse_matrix has been replaced by new matrix types having sparse storage. It's been a long time since I used that library.

You can still look at Boost.uBlas for inspiration in rolling your own sparse 3D matrix.

Upvotes: 0

Gregor Brandt
Gregor Brandt

Reputation: 7799

Deque is a more appropriate replacement for a vector than a map. But it depends on your needs. A vector requires linear memory for all its data, but a deque does not. And a deque is can be used as a drop in replacement for a vector in most cases.

Upvotes: 1

Oleg Razgulyaev
Oleg Razgulyaev

Reputation: 5935

If you have sparse 3-dimension array, you can save a lot of memory using map with the key as combination of indexes. For your case with indexes < 1000, key can be 32 bit unsigned integer:

#include <stdint.h>
...
int index1 = 12, index2 = 34, index3 = 56;

// construct key:
uint32_t key = 
  ((index1&0x3FF) << 20) | 
  ((index2&0x3FF) << 10) | 
  (index3&0x3FF)
  );

// restore indexes:
index1 = (key >> 20) & 0x3FF;
index2 = (key >> 10) & 0x3FF;
index3 = (key) & 0x3FF;


Upvotes: 2

David Thornley
David Thornley

Reputation: 57036

I consider vector<> to be the best default choice of container (except for vector<bool>). If I have a reason to prefer another container, I use the other one; if not, I use vector<> (or deque<bool>). That's as close as I can get to "better in general".

There are reasons for all the STL containers, things they do better than any other one. Without knowing what you're going to be using the data structure for, I can't be any more help.

However, the penalties for using the wrong structure go up with the size. If you have a few dozen elements, any class will do. If you're talking about a billion elements, you really do want to get it right (and make sure your computer will handle it - you probably need a 64-bit app with a lot of RAM).

Upvotes: 1

Jason B
Jason B

Reputation: 12975

First of all, storing a map will almost definitely take more memory than a vector since a vector is simply a contiguous block and a map contains a tree structure.

The answer to your question of which is better is that it depends on the problem you are trying to solve. It really comes down to how you want to be able to index your data. If your data can be indexed linearly by an integer, then vector will perform the best.

However, there are many cases where you'll want to access your data in some other way. For example, if you want to index your data using a string (like a dictionary lookup), then the map will perform better. To index data with a string using a vector, you would have to perform a linear search (O(n)) to find an element if you don't keep the vector sorted. A map would only have to perform a binary search (O(log n)) which would be a LOT faster as n grows.

Upvotes: 5

Maciej Hehl
Maciej Hehl

Reputation: 7985

There is no such thing as better in general. It all depends on the operations You are going to perform and on the frequency of those operations.

Vectors don't use more space than map, for the same number of elements. However such a thing, like a sparse matrix for example, sometimes may be implemented more efficiently using map.

Upvotes: 3

Paul Nathan
Paul Nathan

Reputation: 40299

If you want a key-value component, use map. If you want a random-access-by-number solution, or an iteratively ordered solution, use vector.

Check your data structures/algorithm analysis text for more information.

Upvotes: 1

rlbond
rlbond

Reputation: 67739

A map is an associative container -- it serves a completely different function from a vector. Use a map if you want to associate keys with values. If your keys are just nonnegative consecutive integers, then a vector is superior in every way.

Upvotes: 10

Xorty
Xorty

Reputation: 18861

Vector and Map are two different containers used for different purposes. IF there was one better, the other would NOT exist ...

Upvotes: 7

Jay
Jay

Reputation: 14441

There is no correct answer to this question. The correct question should be "which is better for the specific application --insert your project here--". To choose the correct container you need to explain how it will be used.

Upvotes: 15

Related Questions