C ++ deque vs vector and C ++ map vs Set

Can someone tell me what is the difference between vector and deck. I know the implementation of a vector in C ++, but not deque. Also the map and dial interfaces seem similar to me. What is the difference between them and when to use them.

+7
c ++ stl
source share
4 answers

std :: vector : a dynamic array class. The allocation of internal memory ensures that it always creates an array. Useful when the size of the data is known and is not known to change too often. This is also good when you want to have random access to items.
std :: deque : a double-ended queue that can act as a stack or queue. It’s good when you are not sure about the number of elements and when an access to a data element is always performed sequentially. They are quick when items are added / removed from the front and end, but not when they are added / removed to / from the middle.
std :: list : A double list that can be used to create a "list" of data. The advantage of the list is that items can be inserted or deleted from any part of the list without affecting the iterator, which points to the memeber list (and is still a member of the list after deletion). Useful when you know that items will be deleted very often from any part of the list.
std :: map : a dictionary that maps the "key" to the "value". Useful for applications like arrays whose index is not an integer. In principle, it can be used to create a list-list of names for an element, for example, a map in which the relationship between names and widgets is stored.
std :: set : a list of unique data values. E.g. if you insert 1, 2, 2, 1, 3, the list will only have items 1, 2, 3. Note that the items in this list are always ordered. Internally, they are usually implemented as binary search trees (for example, a map).

+42
source share

See here for more details:

What are the guarantees for the complexity of standard containers?

vector vs deque

Deque matches the vector, but with the following addition:

  • This is the "front insertion sequence"

This means that deque matches the vector, but provides the following additional guarantees:

  • push_front () O (1)
  • pop_front () O (1)

install vs map

A map is a “associative container for a couple” and set is a “simple associative container”

This means that they are exactly the same. The difference is that the map contains pairs of elements (Key / Value), not just a value.

+3
source share

set: contains unique values. Put 'a' twice, the set has one 'a'. map: maps keys to values, e.g. 'name' => 'fred', 'age' => 40. You can look at “name” and you will get “fred”.

dequeue as a vector, but you can add / remove only at the ends. There are no inserts in the middle. http://en.wikipedia.org/wiki/Deque

edit: my dequeue description is missing, see comments below for corrections

+1
source share

A map is what is often called an associative array, usually implemented using a binary tree (for example). A deck is a double queue, a specific embodiment of a linked list.

This does not mean that actual container library implementations use these concepts. The custodian library will simply give you some guarantees on how you can access the container and at what (amortized) cost.

I suggest you take a look at the link that details what warranties are. Scott Meyers' book, Effective STL: 50 Concrete Ways to Improve Usage of the Standard Template Library, should talk a little about it, if I remember correctly. Also, the C ++ standard is obviously a good choice.

I really want to say: containers are really described by their properties, not the main implementation.

+1
source share

All Articles