Is vector the specialized form of unordered_map with integer key? It seems so because a vector has integer keys, too.
If not, what are the differences?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The main difference is in how the data is stored.
A
vectorstores the data in an internal array that it resizes and you add more elements.An
unordered_mapuses a hash table internally.Practically, a
vectorgives you amortized constant time insertions at the back (it needs to resize and copy/move everything once in a while), constant time access by index, and up to linear time insertion and deletion (all the subsequent elements have to be shifted). Also, since avectoris contiguous, you can pass it into functions expecting a c-style array.unordered_mapgives you amortized constant time lookup by key (because hashing is not perfect, and collisions force the lookup to traverse through internal linked lists), amortized constant time inserts and deletes.See: http://en.cppreference.com/w/cpp/container/unordered_map
and: http://en.cppreference.com/w/cpp/container/vector