0

On the weekend I will attend a programing competition and I would like to know whether should I use std::vector or std::map? I would use them simply as arrays but I am confused which is the better(mostly speed for basic operations)?

I saw this pic on stackoverflow and I have no idea which is the main difference between these...

The picture suggest that vector is faster... I really don't know what to do pls help me! I need to use them only like arrays but with dynamical size... Thanks in advance,

EDIT I will probably get a 2D array of integers (for example a map of a city or a labirinth and I would be given some kind of problem that could be solved with graph-algorithms or dynamic programming), so the things I will need are: write, read specific cells of the "table", search for specific values, and I guess thats all. I heard that std::map won't locate the full N*M sized table in the memory, but I will do it value by value... So is it true that it may use less memory?

Sorry for being so silly but I have never had any real teachers I learned all that I know on my on. I just started to learn about data structures(2-3 trees, red-black trees, binomial heaps, and so on...)

s4y
  • 50,525
  • 12
  • 70
  • 98
gen
  • 9,528
  • 14
  • 35
  • 64
  • Is it a competition where knowing the difference between those two would matter? – K-ballo Jan 09 '13 at 20:47
  • 2
    You are comparing an associative container with a sequential container, what sense does this made? It's like asking if a boat is faster than a car - you *can* have a valid answer, but they are tools for completely different needs. – Matteo Italia Jan 09 '13 at 20:56
  • "The picture suggest that vector is faster..." *at that specific task on that specific machine at that specific time*. – GManNickG Jan 09 '13 at 21:05
  • @K-Ballo, No, definitely it wouldn't. Pls dont make me a laughing stock :|. Simply I have heard about these data structures and I used arrays before, so I would like to know the advantages of them :) – gen Jan 09 '13 at 21:06
  • @MatteoItalia I would like to know about the _completely different needs and uses..._ – gen Jan 09 '13 at 21:08
  • I say stay away from arrays. Check the [vector reference](http://www.cplusplus.com/reference/vector/vector/) and the [map reference](http://www.cplusplus.com/reference/map/map/) to understand the difference and choose the best that suits your needs. – ChiefTwoPencils Jan 09 '13 at 21:17
  • 1
    @nábob: a `vector` is an ordered sequence of elements; you store elements sequentially and have an integer to refer to each one; a `map` is an associative container - you store key-value pairs (which both can be of any type as long as certain constraints are satisfied) and you can retrieve each value by specifying the corresponding key. You can think at a vector to something like a bookshelf, while a map is more like a dictionary. – Matteo Italia Jan 09 '13 at 21:23
  • @MatteoItalia So, there is no sense to use maps with integer keys just as at vectors? – gen Jan 09 '13 at 21:31
  • 2
    @nábob: Integer keys are fine, if that's what you need. If you need indexing, that's for vector. You can even implement your own map on top of a vector by using the key as an index, though of your keys are sparse you waste space, etc. All this really depends. You need to take a step back and understand that: stop trying to compare them directly. Understand what each does *alone*. – GManNickG Jan 09 '13 at 22:01
  • @nábob: It may make sense if you have sparse indexes, since in such a case you would need an overly big array with lots of empty slots. But, again, follow *GManNickG*'s suggestion: *Understand what each does alone*. – Matteo Italia Jan 10 '13 at 00:06
  • One place where std::vector fails compared to simply allocating an array and populating it manually is that if you have a problem whose run-time is dominated by the vector.push_back() calls, the std::vector implementation would very likely be slower. Generally, however, I find the coding time benefits of using the STL classes outweighs the (perhaps minor) performance cost, but again it depends very much on the problem at hand and exactly how the solutions are judged/ranked in the competition (i.e., is run-time a factor). – Bogatyr Nov 28 '14 at 10:56

3 Answers3

9

I would like to know whether should I use std::vector or std::map?

That completely depends on what you want/need to do. If you need to input 2 numbers from the user, add them and the display the result, there's no point in using a vector or a map.

However, if you need to store an array of objects (or primitives), std::vector is generally the way to go. If you need storage of keys and values, then that's what std::map has been invented for. The question you asked is way too broad, so it's practically very hard to answer it, but you may still get the idea.

Also, you can get some inspiration about code that you should never try to replicate here.

  • I try to refine my question: I would need to use some kind of 2D arrays of integers such as maps of cities or anything like that :). I heard std::map uses less memory if I don't fill all the cells of the "table"/array, is that right? – gen Jan 09 '13 at 20:56
  • @nábob Good question, sounds like you'll need to fire up a benchmarking tool :) –  Jan 09 '13 at 21:00
  • nabob if not all the elements of an array are going to be filled, you can create a sparse matrix. There are a few different representations used to do this. However, you should be aware, that you will suffer speed-wise. You really can't get faster than a contiguous block of memory. – Abe Schneider Jan 09 '13 at 21:04
  • @nábob And you can also create a vector of vectors as well. Or if you need a simple array of scalars (numbers, for example) which may not change its size, you can survive by creating a plain old 2D array: `int matrix[w][h];` - but again, it would be far easier for us to give a reasonable answer if you narrowed your question down to an actual problem. –  Jan 09 '13 at 21:06
1

std::map is not an array, but rather a red-black binary tree. Therefore, it isn't a good choice as a basic array storage.

std::vector may be used as an array, and will likely give similar access speeds as arrays (while it may depend on the implementation, most if not all will be implemented as arrays). The advantage of std::vector is not speed, but rather that it manages memory for you.

Also, you might want to read up on different data structures. It will help expand your programming skills.

Abe Schneider
  • 977
  • 1
  • 11
  • 23
  • How come `std::map` is a red-black tree? o.O It's rather a key-value storage. It may be implemented using a red-black tree (that doesn't make much sense, though), but that's an implementation detail. –  Jan 09 '13 at 20:53
  • I should have said it's a binary tree, though the implementations I've seen are red-black trees. What do you mean that doesn't make sense? If you want a hash map, you can use std::hash_map instead. However, it's more than an implementation detail. You should know the underlying algorithm you're using. – Abe Schneider Jan 09 '13 at 20:58
  • More discussion on implementation of std::map: http://stackoverflow.com/questions/5288320/why-is-stdmap-implemented-as-red-black-tree – Abe Schneider Jan 09 '13 at 20:59
0

Do you mean an array in the C sense of a character array or the mathematical sense? Will your array be multidimensional?

If you mean the mathematical kind, you may find that storing the array in space malloced in the heap and resized by doing a new, larger malloc followed by a memcopy and a free of the old malloced area may be faster than using std:containers.

Don't be fooled by the std: constructors sounding like they allocate what you need spacewise. They malloc too much space at constructor time so they won't have to grow as often as they might if they didn't no this. When they do have to grow, they again claim extra space for exactly the same reason again. When you do have to expand your space, remember to move the data using the largest data item you can. 1/64th as many longs move faster than N bytes assuming you have a 64 bit bus and chars move one at a time and that your compiler isn't smart enough to optimize this for you.

Wes Miller
  • 2,191
  • 2
  • 38
  • 64