views:

358

answers:

11

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.

+15  A: 

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.

Jay
Amen, brother! It's all a matter of trade-offs (as is usually in life generally ;)
sbk
+6  A: 

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

Xorty
+8  A: 

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.

rlbond
+3  A: 

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.

Maciej Hehl
A: 

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.

gbrandt
Three out of five statements in this answer are wrong.
anon
woops. I meant deque, not list. thanks for the catch.
gbrandt
How is deque relevant here? Without data compression, and usually even with, there is no such thing as a structure that doesn't require linear memory for its data.
Potatoswatter
+1  A: 

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.

Paul Nathan
+5  A: 

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.

Jason
+2  A: 

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.

Clifford
+1  A: 

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).

David Thornley
+2  A: 
oraz
A: 

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.

Emile Cormier