This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
STL like container with O(1) performance.
I always thought that std::map is a hashed list. In that case shouldn't lookup be O(1). The documentation says it's O(logn). What's an appropriate data structure in STL that mimics a hashed map the best with O(1) insertion and lookup.
std::map is implemented as a binary search tree. So lookups are not O(1). TR1 and C++0x are adding a hash map to the STL called an unordered_map. See http://en.wikipedia.org/wiki/Unordered_map_(C%2B%2B)
Depending on your compiler, you might have unordered_map or possible hash_map in the STL.
There is no official STL container with constant lookup. However, several library implementations provide a non-standard hash_map container which does O(1) lookups (http://www.sgi.com/tech/stl/hash_map.html)
You are looking for std::hash_map.
Related
This question already has answers here:
Find mapped value of map
(6 answers)
Closed 1 year ago.
std::unordered_map::find search for the certain key in the unordered_map, and is there a function to search a certain value?
Definitely I can write some simple loop to do it but maybe something already exist for that that?
No, not really. The map entries (key-value pairs) are not arranged according to their values; nor are the values stored separately from the keys etc. Or rather - the standard doesn't guarantee any of that, and all popular implementations don't offer this.
You're just going to have to use a linear search... or a different/additional data structure which supports the kind of searches you need.
Also remember that std::unordered_map is quite slow in practical, non-asymptotic terms, so if performance is a consideration - definitely look for alternatives.
This question already has answers here:
What's the difference between set<pair> and map in C++?
(7 answers)
Closed 9 years ago.
I'm relatively new to c++ programming and was wondering if someone could help clarify a few questions for me.
http://www.cplusplus.com/reference/set/set/
http://www.cplusplus.com/reference/map/map/
I've been reading on how to implement STL binary search trees and I keep noticing that std::set and std::map are constantly mentioned as the methods for accomplishing such a task. What exactly is the difference between the two however? To me both seem almost identical and I'm not sure if there's something I'm not noticing that makes one better than the other for specific tasks. Is there any advantage of using std::set over std::map for implementing a STL binary search tree that takes values from an array or vector (such as speed for example)?
If someone could help me understand this concept I'd greatly appreciate it!
Both std::set and std::map are associative containers. The difference is that std::sets contain only the key,
while in std::map there is an associated value , that is if A -> B , then map[A]=B , this works like hashing but not O(1) , instead O(log N).
You can further look unordered_map which provides the operation in O(1) time.
std::set keeps data in sorted format .
Implementation of both is done by balanced trees (like AVL or Red-Black trees ) giving O(logN) time complexity.
But important point to note is that both can store unique values . To overcome that you must see also multimap and multiset .
Hope this helps !
update: In the case of Red-Black tree re-balancing rotation is an O(1) operation while with AVL this is a O(log n) operation, making the Red-Black tree more efficient in this aspect of the re-balancing stage and one of the possible reasons that it is more commonly used.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How is a C++ multimap implemented?
C++ reference mentions
Multimaps are typically implemented as binary search trees.
But what is their typical internal representation?
Is it like std::map<Key, std::list<Value> > or similar?
My concern is complexity of insertion and iteration over a set of elements with the same key.
If you want to know the complexity of specific operations, you need look no further than to the standard. The standard has guarantees on the complexity, but implementations are free to satisfy those guarantees any way they wish.
For insertion the complexity is O(lg n), unless you specify an optimal hint every time, in which case the complexity is O(1) amortized. (See details here: http://en.cppreference.com/w/cpp/container/multimap/insert)
For iteration over a set of elements with the same key, the complexity is the same as iteration from any iterator to another. Given that you have already found the iterators, the iteration is linear in the count of items you are iterating over. (Sorry, unable to find a reference for this right now)
I am a big java fan but i have to work now on C++ for a project. I intended to you a java hashmap kind of feature in c++. After googling i found there exists no hashmap/hashtable in C++ STL library. But i found these data types: map, unordered_map, unorderd_set and hash_map. hash_map is microsoft's specific dll/library and remaining are used under STL. i have to work on IBM XL C/C++ compiler. So i can't use microsoft/boost as my company don't recommend them. All i have to use STL specific. Please, provide some info on these collections. What would be best among these STL specifics if i have to choose hashmap functionality? Thanks in advance.
unordered_map is equivalent to java's HashMap, and is a hash map - so it is probably what you are after.
map is equivalent to a TreeMap in java. It is implemented as a red-black tree.
unordered_set is equivalent to java's HashSet. It contains only keys, and not pairs of (key,value)
Did you read Wikipedia's page on associative containers in C++?
If you want real hash table (with a key providing an hash code, but no order between keys) you could use C++ 2011 std::unordered_map template. You'll need a compiler recent enough to be C++11 compatible in that regard.
If you can provide an order on keys, consider also using std::map which is available even in the older C++03 standard.
C++ Boost has Bimap container that is a bidirectional map:
http://www.boost.org/doc/libs/1_43_0/libs/bimap/doc/html/index.html
Does anyone know the performance of Boost::bimap? I mean what's the time complexity of accessing an element in the map? Is it as quick as unordered_map access (which is O(1))?
Thanks!
AFAIK each different container of this library have different operation complexity relative to the implementation (like for the stl containers). For details necessary to make your choice, read : http://www.boost.org/doc/libs/1_43_0/libs/bimap/doc/html/boost_bimap/the_tutorial/controlling_collection_types.html