🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Microsoft stl set implementation

Started by
7 comments, last by ChaosEngine 5 years ago

I notice that Microsoft's implementation of the set container has some weird quirks. If I run the exact same code several times the order of the elements will be inserted in a different order each time. Has anyone else noticed this?

Advertisement

This may be a dumb question, but are you using pointers to store objects and the container sorts the pointer locations instead of a key ?

3 hours ago, Ed Welch said:

I notice that Microsoft's implementation of the set container has some weird quirks. If I run the exact same code several times the order of the elements will be inserted in a different order each time.

Sets have no order in math. You can add elements (adding it more than once is silently ignored), remove elements (removing a non-existing elements is silently ignored), and test whether elements are in a set, (gives true or false), and that's it.

In programming more operations exist on sets, in particular iteration over its elements. Some set implementations explicitly state how they are ordered (by element value or by insertion order are common), while other implementations don't define order, basically saying "don't rely on it". In other words, depending on the set implementation that you pick you may (and do, in case of unorded set implementations) get undefined order of retrieved elements.

Unordered set implementations are often faster or take less memory. On the other hand, your code has to be ready for any order of retrieving the elements.

 

5 hours ago, Green_Baron said:

This may be a dumb question, but are you using pointers to store objects and the container sorts the pointer locations instead of a key ?

No the set elements are just strings

2 hours ago, Alberth said:

Sets have no order in math.

no, but they should have the same order every time the same code is executed

I am confused. According to https://en.cppreference.com/w/cpp/container/set a set is a sorted set of unique objects. Sorting is based on operator<().

An std::string should be inserted lexicographically, char * by value of the pointer. The latter could lead to different output in different runs ... or have i misunderstood ?

With a piece of code we could try out more ?

5 minutes ago, Green_Baron said:

I am confused. According to https://en.cppreference.com/w/cpp/container/set a set is a sorted set of unique objects. Sorting is based on operator<().

An std::string should be inserted lexicographically, char * by value of the pointer. The latter could lead to different output in different runs ... or have i misunderstood ?

With a piece of code we could try out more ?

No, that's it exactly. I just wanted to know if anyone came across the same problem.

I've used std::set for sorting and that always worked fine. When you talk about string, you do mean std::string, not char*?

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

@Ed Welch you really need to post a small reproducible example of what you're talking about. 

This works fine for example. I'd be amazed if Microsoft's std::set implementation was broken.

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

This topic is closed to new replies.

Advertisement