Unordered map iterating by values












1















I have a weird issue that I don't know if I am miss reading the documentation or my computer is doing something strange.



I have an unordered_map. I want to iterate over the buckets of the unordered_map in bucket order. This part is important as I need the access to be relatively random. I searched over cplusplus.com and I found this. The code is as follows:



// unordered_map::bucket
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,std::string> mymap = {
{"us","United States"},
{"uk","United Kingdom"},
{"fr","France"},
{"de","Germany"}
};

for (auto& x: mymap) {
std::cout << "Element [" << x.first << ":" << x.second << "]";
std::cout << " is in bucket #" << mymap.bucket (x.first) << std::endl;
}

return 0;
}


The output I was expecting and wanted on my computer was



Element [us:United States] is in bucket #1
Element [de:Germany] is in bucket #2
Element [fr:France] is in bucket #2
Element [uk:United Kingdom] is in bucket #4


However the output I am getting is instead sorted by value type which is strange



Element [de:Germany] is in bucket #2
Element [fr:France] is in bucket #3
Element [uk:United Kingdom] is in bucket #1
Element [us:United States] is in bucket #1


I even tried replacing the value with a class that had no comparison operators and it still was able to sort them. Is this something with the way my computer is storing the map or is cplusplus.com outdated? I was able to iterate over the buckets through a loop like this:



for ( unsigned int i = 0; i < b.bucket_count(); ++i) {

for ( hash_table::const_local_iterator image_iterator =
b.begin(i);image_iterator!= b.end(i); ++image_iterator ){


The only issue is that I need to be able to skip a certain number of values i.e. I only want 1 item for every 100 which requires complicated loops and is slow.



Any help would be appreciated. I can't seem to figure this out!



[EDIT]
With my code my unordered_map is actually unordered_map where point is a simple class that just has two member variables and no helper functions.



When I run the loop above on my map here is my output. I have linked a text file as it is a pretty long file here
What confuses me even more is that my Point class has no comparison operators. Could it be my insertion order that is causing this?










share|improve this question

























  • Note that your output is also sorted by key type and also sequential by bucket, but starting at #2 with modulo arithmetic.

    – François Andrieux
    Nov 22 '18 at 16:02













  • As far as I know, the order of elements when iterating over an std::unordered_map is unspecified. I understand why you might expect it to order them by bucket but I don't know of anything that guaranties it. Evidently that's not what's happening here.

    – François Andrieux
    Nov 22 '18 at 16:04






  • 3





    It's not clear what documentation you are referring to. The order of iterating over unordered_map is not specified so should not be given by any documentation.

    – François Andrieux
    Nov 22 '18 at 16:10








  • 3





    It's just a coincidence. I can reproduce with the specific keys you show, but change them to "a", "b", "c", "d" and the output is no longer sorted (either by key or by bucket). Unordered map is unordered.

    – Igor Tandetnik
    Nov 22 '18 at 16:13






  • 1





    @MichaelHonaker The bottom line is you cannot rely on unordered_map's ordering of elements. The order is unspecified, but it doesn't mean random. So even a strangely well ordered sequence can be a legitimate ordering.

    – François Andrieux
    Nov 22 '18 at 16:30
















1















I have a weird issue that I don't know if I am miss reading the documentation or my computer is doing something strange.



I have an unordered_map. I want to iterate over the buckets of the unordered_map in bucket order. This part is important as I need the access to be relatively random. I searched over cplusplus.com and I found this. The code is as follows:



// unordered_map::bucket
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,std::string> mymap = {
{"us","United States"},
{"uk","United Kingdom"},
{"fr","France"},
{"de","Germany"}
};

for (auto& x: mymap) {
std::cout << "Element [" << x.first << ":" << x.second << "]";
std::cout << " is in bucket #" << mymap.bucket (x.first) << std::endl;
}

return 0;
}


The output I was expecting and wanted on my computer was



Element [us:United States] is in bucket #1
Element [de:Germany] is in bucket #2
Element [fr:France] is in bucket #2
Element [uk:United Kingdom] is in bucket #4


However the output I am getting is instead sorted by value type which is strange



Element [de:Germany] is in bucket #2
Element [fr:France] is in bucket #3
Element [uk:United Kingdom] is in bucket #1
Element [us:United States] is in bucket #1


I even tried replacing the value with a class that had no comparison operators and it still was able to sort them. Is this something with the way my computer is storing the map or is cplusplus.com outdated? I was able to iterate over the buckets through a loop like this:



for ( unsigned int i = 0; i < b.bucket_count(); ++i) {

for ( hash_table::const_local_iterator image_iterator =
b.begin(i);image_iterator!= b.end(i); ++image_iterator ){


The only issue is that I need to be able to skip a certain number of values i.e. I only want 1 item for every 100 which requires complicated loops and is slow.



Any help would be appreciated. I can't seem to figure this out!



[EDIT]
With my code my unordered_map is actually unordered_map where point is a simple class that just has two member variables and no helper functions.



When I run the loop above on my map here is my output. I have linked a text file as it is a pretty long file here
What confuses me even more is that my Point class has no comparison operators. Could it be my insertion order that is causing this?










share|improve this question

























  • Note that your output is also sorted by key type and also sequential by bucket, but starting at #2 with modulo arithmetic.

    – François Andrieux
    Nov 22 '18 at 16:02













  • As far as I know, the order of elements when iterating over an std::unordered_map is unspecified. I understand why you might expect it to order them by bucket but I don't know of anything that guaranties it. Evidently that's not what's happening here.

    – François Andrieux
    Nov 22 '18 at 16:04






  • 3





    It's not clear what documentation you are referring to. The order of iterating over unordered_map is not specified so should not be given by any documentation.

    – François Andrieux
    Nov 22 '18 at 16:10








  • 3





    It's just a coincidence. I can reproduce with the specific keys you show, but change them to "a", "b", "c", "d" and the output is no longer sorted (either by key or by bucket). Unordered map is unordered.

    – Igor Tandetnik
    Nov 22 '18 at 16:13






  • 1





    @MichaelHonaker The bottom line is you cannot rely on unordered_map's ordering of elements. The order is unspecified, but it doesn't mean random. So even a strangely well ordered sequence can be a legitimate ordering.

    – François Andrieux
    Nov 22 '18 at 16:30














1












1








1








I have a weird issue that I don't know if I am miss reading the documentation or my computer is doing something strange.



I have an unordered_map. I want to iterate over the buckets of the unordered_map in bucket order. This part is important as I need the access to be relatively random. I searched over cplusplus.com and I found this. The code is as follows:



// unordered_map::bucket
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,std::string> mymap = {
{"us","United States"},
{"uk","United Kingdom"},
{"fr","France"},
{"de","Germany"}
};

for (auto& x: mymap) {
std::cout << "Element [" << x.first << ":" << x.second << "]";
std::cout << " is in bucket #" << mymap.bucket (x.first) << std::endl;
}

return 0;
}


The output I was expecting and wanted on my computer was



Element [us:United States] is in bucket #1
Element [de:Germany] is in bucket #2
Element [fr:France] is in bucket #2
Element [uk:United Kingdom] is in bucket #4


However the output I am getting is instead sorted by value type which is strange



Element [de:Germany] is in bucket #2
Element [fr:France] is in bucket #3
Element [uk:United Kingdom] is in bucket #1
Element [us:United States] is in bucket #1


I even tried replacing the value with a class that had no comparison operators and it still was able to sort them. Is this something with the way my computer is storing the map or is cplusplus.com outdated? I was able to iterate over the buckets through a loop like this:



for ( unsigned int i = 0; i < b.bucket_count(); ++i) {

for ( hash_table::const_local_iterator image_iterator =
b.begin(i);image_iterator!= b.end(i); ++image_iterator ){


The only issue is that I need to be able to skip a certain number of values i.e. I only want 1 item for every 100 which requires complicated loops and is slow.



Any help would be appreciated. I can't seem to figure this out!



[EDIT]
With my code my unordered_map is actually unordered_map where point is a simple class that just has two member variables and no helper functions.



When I run the loop above on my map here is my output. I have linked a text file as it is a pretty long file here
What confuses me even more is that my Point class has no comparison operators. Could it be my insertion order that is causing this?










share|improve this question
















I have a weird issue that I don't know if I am miss reading the documentation or my computer is doing something strange.



I have an unordered_map. I want to iterate over the buckets of the unordered_map in bucket order. This part is important as I need the access to be relatively random. I searched over cplusplus.com and I found this. The code is as follows:



// unordered_map::bucket
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,std::string> mymap = {
{"us","United States"},
{"uk","United Kingdom"},
{"fr","France"},
{"de","Germany"}
};

for (auto& x: mymap) {
std::cout << "Element [" << x.first << ":" << x.second << "]";
std::cout << " is in bucket #" << mymap.bucket (x.first) << std::endl;
}

return 0;
}


The output I was expecting and wanted on my computer was



Element [us:United States] is in bucket #1
Element [de:Germany] is in bucket #2
Element [fr:France] is in bucket #2
Element [uk:United Kingdom] is in bucket #4


However the output I am getting is instead sorted by value type which is strange



Element [de:Germany] is in bucket #2
Element [fr:France] is in bucket #3
Element [uk:United Kingdom] is in bucket #1
Element [us:United States] is in bucket #1


I even tried replacing the value with a class that had no comparison operators and it still was able to sort them. Is this something with the way my computer is storing the map or is cplusplus.com outdated? I was able to iterate over the buckets through a loop like this:



for ( unsigned int i = 0; i < b.bucket_count(); ++i) {

for ( hash_table::const_local_iterator image_iterator =
b.begin(i);image_iterator!= b.end(i); ++image_iterator ){


The only issue is that I need to be able to skip a certain number of values i.e. I only want 1 item for every 100 which requires complicated loops and is slow.



Any help would be appreciated. I can't seem to figure this out!



[EDIT]
With my code my unordered_map is actually unordered_map where point is a simple class that just has two member variables and no helper functions.



When I run the loop above on my map here is my output. I have linked a text file as it is a pretty long file here
What confuses me even more is that my Point class has no comparison operators. Could it be my insertion order that is causing this?







c++ c++11 unordered-map






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 14:57









Cubbi

38.5k667145




38.5k667145










asked Nov 22 '18 at 15:55









Michael HonakerMichael Honaker

83




83













  • Note that your output is also sorted by key type and also sequential by bucket, but starting at #2 with modulo arithmetic.

    – François Andrieux
    Nov 22 '18 at 16:02













  • As far as I know, the order of elements when iterating over an std::unordered_map is unspecified. I understand why you might expect it to order them by bucket but I don't know of anything that guaranties it. Evidently that's not what's happening here.

    – François Andrieux
    Nov 22 '18 at 16:04






  • 3





    It's not clear what documentation you are referring to. The order of iterating over unordered_map is not specified so should not be given by any documentation.

    – François Andrieux
    Nov 22 '18 at 16:10








  • 3





    It's just a coincidence. I can reproduce with the specific keys you show, but change them to "a", "b", "c", "d" and the output is no longer sorted (either by key or by bucket). Unordered map is unordered.

    – Igor Tandetnik
    Nov 22 '18 at 16:13






  • 1





    @MichaelHonaker The bottom line is you cannot rely on unordered_map's ordering of elements. The order is unspecified, but it doesn't mean random. So even a strangely well ordered sequence can be a legitimate ordering.

    – François Andrieux
    Nov 22 '18 at 16:30



















  • Note that your output is also sorted by key type and also sequential by bucket, but starting at #2 with modulo arithmetic.

    – François Andrieux
    Nov 22 '18 at 16:02













  • As far as I know, the order of elements when iterating over an std::unordered_map is unspecified. I understand why you might expect it to order them by bucket but I don't know of anything that guaranties it. Evidently that's not what's happening here.

    – François Andrieux
    Nov 22 '18 at 16:04






  • 3





    It's not clear what documentation you are referring to. The order of iterating over unordered_map is not specified so should not be given by any documentation.

    – François Andrieux
    Nov 22 '18 at 16:10








  • 3





    It's just a coincidence. I can reproduce with the specific keys you show, but change them to "a", "b", "c", "d" and the output is no longer sorted (either by key or by bucket). Unordered map is unordered.

    – Igor Tandetnik
    Nov 22 '18 at 16:13






  • 1





    @MichaelHonaker The bottom line is you cannot rely on unordered_map's ordering of elements. The order is unspecified, but it doesn't mean random. So even a strangely well ordered sequence can be a legitimate ordering.

    – François Andrieux
    Nov 22 '18 at 16:30

















Note that your output is also sorted by key type and also sequential by bucket, but starting at #2 with modulo arithmetic.

– François Andrieux
Nov 22 '18 at 16:02







Note that your output is also sorted by key type and also sequential by bucket, but starting at #2 with modulo arithmetic.

– François Andrieux
Nov 22 '18 at 16:02















As far as I know, the order of elements when iterating over an std::unordered_map is unspecified. I understand why you might expect it to order them by bucket but I don't know of anything that guaranties it. Evidently that's not what's happening here.

– François Andrieux
Nov 22 '18 at 16:04





As far as I know, the order of elements when iterating over an std::unordered_map is unspecified. I understand why you might expect it to order them by bucket but I don't know of anything that guaranties it. Evidently that's not what's happening here.

– François Andrieux
Nov 22 '18 at 16:04




3




3





It's not clear what documentation you are referring to. The order of iterating over unordered_map is not specified so should not be given by any documentation.

– François Andrieux
Nov 22 '18 at 16:10







It's not clear what documentation you are referring to. The order of iterating over unordered_map is not specified so should not be given by any documentation.

– François Andrieux
Nov 22 '18 at 16:10






3




3





It's just a coincidence. I can reproduce with the specific keys you show, but change them to "a", "b", "c", "d" and the output is no longer sorted (either by key or by bucket). Unordered map is unordered.

– Igor Tandetnik
Nov 22 '18 at 16:13





It's just a coincidence. I can reproduce with the specific keys you show, but change them to "a", "b", "c", "d" and the output is no longer sorted (either by key or by bucket). Unordered map is unordered.

– Igor Tandetnik
Nov 22 '18 at 16:13




1




1





@MichaelHonaker The bottom line is you cannot rely on unordered_map's ordering of elements. The order is unspecified, but it doesn't mean random. So even a strangely well ordered sequence can be a legitimate ordering.

– François Andrieux
Nov 22 '18 at 16:30





@MichaelHonaker The bottom line is you cannot rely on unordered_map's ordering of elements. The order is unspecified, but it doesn't mean random. So even a strangely well ordered sequence can be a legitimate ordering.

– François Andrieux
Nov 22 '18 at 16:30












1 Answer
1






active

oldest

votes


















3














In std::unordered_map the bucket for a particular element is completely determined by the hash value computed on the key, std::unordered_map use as the corresponding specialization of std::hash as default hash function, which is implementation defined and is not even guaranteed to be the same throughout different program execution. see std::hash.



As @François Andrieux said in the comment section, order of iteration of std::unordered_map is not specified, hence, you cannot expect the same iteration behavior in all machines, e.g the output in my computer is:



Element [de:Germany] is in bucket #0
Element [fr:France] is in bucket #3
Element [uk:United Kingdom] is in bucket #4
Element [us:United States] is in bucket #4





share|improve this answer


























  • Look at my edit. My confusion is on why it is ordered in that way even though there is no comparison operator

    – Michael Honaker
    Nov 22 '18 at 16:24











  • That's right, ..but you can't expect to guess what it is until you try it ... that was what i meant, Fixed ... Thanks

    – Jans
    Nov 22 '18 at 16:29













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434552%2funordered-map-iterating-by-values%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














In std::unordered_map the bucket for a particular element is completely determined by the hash value computed on the key, std::unordered_map use as the corresponding specialization of std::hash as default hash function, which is implementation defined and is not even guaranteed to be the same throughout different program execution. see std::hash.



As @François Andrieux said in the comment section, order of iteration of std::unordered_map is not specified, hence, you cannot expect the same iteration behavior in all machines, e.g the output in my computer is:



Element [de:Germany] is in bucket #0
Element [fr:France] is in bucket #3
Element [uk:United Kingdom] is in bucket #4
Element [us:United States] is in bucket #4





share|improve this answer


























  • Look at my edit. My confusion is on why it is ordered in that way even though there is no comparison operator

    – Michael Honaker
    Nov 22 '18 at 16:24











  • That's right, ..but you can't expect to guess what it is until you try it ... that was what i meant, Fixed ... Thanks

    – Jans
    Nov 22 '18 at 16:29


















3














In std::unordered_map the bucket for a particular element is completely determined by the hash value computed on the key, std::unordered_map use as the corresponding specialization of std::hash as default hash function, which is implementation defined and is not even guaranteed to be the same throughout different program execution. see std::hash.



As @François Andrieux said in the comment section, order of iteration of std::unordered_map is not specified, hence, you cannot expect the same iteration behavior in all machines, e.g the output in my computer is:



Element [de:Germany] is in bucket #0
Element [fr:France] is in bucket #3
Element [uk:United Kingdom] is in bucket #4
Element [us:United States] is in bucket #4





share|improve this answer


























  • Look at my edit. My confusion is on why it is ordered in that way even though there is no comparison operator

    – Michael Honaker
    Nov 22 '18 at 16:24











  • That's right, ..but you can't expect to guess what it is until you try it ... that was what i meant, Fixed ... Thanks

    – Jans
    Nov 22 '18 at 16:29
















3












3








3







In std::unordered_map the bucket for a particular element is completely determined by the hash value computed on the key, std::unordered_map use as the corresponding specialization of std::hash as default hash function, which is implementation defined and is not even guaranteed to be the same throughout different program execution. see std::hash.



As @François Andrieux said in the comment section, order of iteration of std::unordered_map is not specified, hence, you cannot expect the same iteration behavior in all machines, e.g the output in my computer is:



Element [de:Germany] is in bucket #0
Element [fr:France] is in bucket #3
Element [uk:United Kingdom] is in bucket #4
Element [us:United States] is in bucket #4





share|improve this answer















In std::unordered_map the bucket for a particular element is completely determined by the hash value computed on the key, std::unordered_map use as the corresponding specialization of std::hash as default hash function, which is implementation defined and is not even guaranteed to be the same throughout different program execution. see std::hash.



As @François Andrieux said in the comment section, order of iteration of std::unordered_map is not specified, hence, you cannot expect the same iteration behavior in all machines, e.g the output in my computer is:



Element [de:Germany] is in bucket #0
Element [fr:France] is in bucket #3
Element [uk:United Kingdom] is in bucket #4
Element [us:United States] is in bucket #4






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 16:29

























answered Nov 22 '18 at 16:19









JansJans

9,05422635




9,05422635













  • Look at my edit. My confusion is on why it is ordered in that way even though there is no comparison operator

    – Michael Honaker
    Nov 22 '18 at 16:24











  • That's right, ..but you can't expect to guess what it is until you try it ... that was what i meant, Fixed ... Thanks

    – Jans
    Nov 22 '18 at 16:29





















  • Look at my edit. My confusion is on why it is ordered in that way even though there is no comparison operator

    – Michael Honaker
    Nov 22 '18 at 16:24











  • That's right, ..but you can't expect to guess what it is until you try it ... that was what i meant, Fixed ... Thanks

    – Jans
    Nov 22 '18 at 16:29



















Look at my edit. My confusion is on why it is ordered in that way even though there is no comparison operator

– Michael Honaker
Nov 22 '18 at 16:24





Look at my edit. My confusion is on why it is ordered in that way even though there is no comparison operator

– Michael Honaker
Nov 22 '18 at 16:24













That's right, ..but you can't expect to guess what it is until you try it ... that was what i meant, Fixed ... Thanks

– Jans
Nov 22 '18 at 16:29







That's right, ..but you can't expect to guess what it is until you try it ... that was what i meant, Fixed ... Thanks

– Jans
Nov 22 '18 at 16:29






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434552%2funordered-map-iterating-by-values%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?