This question already has answers here:
Keep the order of unordered_map as we insert a new key
(3 answers)
C++11: does unordered_map/set guarantees traversing order as insert order?
(1 answer)
std::unordered_map not behaving as expected
(3 answers)
Closed 6 months ago.
My understanding of what unordered_map means is that is stores unit value per key without ordering them. But is it expected that insertion order is not preserved?
When I compile and run:
std::unordered_map<std::string,int> temp;
temp["Start"] = 0;
temp["Read"] = 0;
for ( auto iter : temp )
{
std::cout << iter.first.c_str();
}
With VS2015, it outputs
Start
Read
With GCC 4.9 for Android, it outputs:
Read
Start
Is it a bug, or expected?
From here:
Internally, the elements in the unordered_map are not sorted in any particular order with respect to either their key or mapped values, but organized into buckets depending on their hash values to allow for fast access to individual elements directly by their key values (with a constant average time complexity on average).
I think that pretty much sums it up.
This is expected. In the standard there're no guarantees regarding the order of elements in std::unordered_map.
Related
This question already has an answer here:
c++ ordered(stable) priority queue
(1 answer)
Closed 5 years ago.
wise guys
My question was like this:
I need to use priority_queue from std, everything works fine, until if there exists ties between my records, the order is no long consistent if I compile using clang compared to compiling on gcc.
my comparator function is simple:
bool comparator(const max_pair_t &lhs, const max_pair_t &rhs) {
return lhs.pval < rhs.pval;
}
that's it.
Is there a way to resolve this problem?
PS: I printed out all the records using two binary excutables, and compared the order side by side, the order is different, but the tied records are in the neighboring area
std::priority_queue gives no guarantees about sort stability. If you need sort stability, you'll have to provide it yourself, e.g. by storing a progressively increasing or decreasing value (doesn't really matter which, it just changes the direction of the fallback comparison) that is used when the primary comparison key is equal, and stripping it off when you pop off the queue.
This question already has answers here:
How can I efficiently select a Standard Library container in C++11?
(4 answers)
Closed 4 years ago.
I have several items saved in a list. I would like to add items that have already been processed to a datastructure (this makes sense in my case even though you might wonder why). When processing the next item from the list I first want to make sure if it has been processed before so lets say something like this:
if(element_is_in_datastructure(current_element)) {
do this
}
else
{
do that
add_element_to_datastructure(current_element)
}
My question is, what is the ideal datastructure where checking if the element is in it won't take too long. At the moment I don't have too many elements (max 30) which will be added to the datastructure, but this number might increase and I don't want to lose performance.
You can use a map e.g std::unordered_map to store your elements as keys.
Then just check their presence e.g
if(!yourMap.count(element))
{
// your element is not in the structure
}
This finding takes logarithmic time in the map's size to finish.
This question already has an answer here:
c++ ordered(stable) priority queue
(1 answer)
Closed 5 years ago.
wise guys
My question was like this:
I need to use priority_queue from std, everything works fine, until if there exists ties between my records, the order is no long consistent if I compile using clang compared to compiling on gcc.
my comparator function is simple:
bool comparator(const max_pair_t &lhs, const max_pair_t &rhs) {
return lhs.pval < rhs.pval;
}
that's it.
Is there a way to resolve this problem?
PS: I printed out all the records using two binary excutables, and compared the order side by side, the order is different, but the tied records are in the neighboring area
std::priority_queue gives no guarantees about sort stability. If you need sort stability, you'll have to provide it yourself, e.g. by storing a progressively increasing or decreasing value (doesn't really matter which, it just changes the direction of the fallback comparison) that is used when the primary comparison key is equal, and stripping it off when you pop off the queue.
This question already has answers here:
Sorting a std::map by value before output & destroy
(5 answers)
Closed 8 years ago.
I have a need to sort a Map(Key=Word,Value=CountofWord) in the descending order of the count of occurrences and print top 10 words in the Map. What can be the best possible method to do that?
My idea is to build a Multimap(Key=CountofWord,Values=Words) and then print the top 10 elements of the Multimap. But this would take O(n) extra space.
Can there be any other optimized solution?
Just to point out that your proposed solution does not take into account that two counts of word can have the same value, therefore two words would be mapped to the same key and override the last one.
What you could do instead is make a class of a word with member variables the word as string and the count as int, and implement the < operator to allow sorting of the objects by the std::sort method. Refer to http://www.cplusplus.com/reference/algorithm/sort/.
This question already has answers here:
Efficient way of iterating over true bits in std::bitset?
(7 answers)
Closed 8 years ago.
I'm using bitset in my code:
std::bitset<MAX_INSTRUMENTS_NUMBER_IN_SYSTEM> updatedInstruments;
Often I need to iterate only values that "set" (or "not set"), this is how I do that:
for (int instrumentId = 0; instrumentId < MAX_INSTRUMENTS_NUMBER_IN_SYSTEM; instrumentId++) {
if (!updatedInstruments[instrumentId]) {
continue;
}
// work
}
Can this iteration be improved to be more readable and possibly faster?
I don't think you can exploit the contiguity of set bits in your code using std::bitset: the interface doesn't provide anything to help in this task, and there's no legal way to access the underlying storage and work directly with it.1
If instead you can afford to change container, you can find several better alternatives. Here for example is a bitset-like structure that provides "enumerators" for active bits (it seems mostly like working with spans on images), and in the duplicate I linked above there are several other suggestions for data structures more specialized for this use case.
Previously I supposed that iterators might have yielded some performance advantage, but turns out std::bitset doesn't have iterators :-o Also, a similar test performed on std::vector<bool> (which should pack bits more or less the same way) gave a ~2x slowdown using iterators both with g++ and clang++.