Does the standard guarantee that order of equal elements will not change (eh, forgot the term for that) by using std::sort or do I need to consider an alternative solution to achieve this goal?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
No, if you want the guarantee use std::stable_sort
No it explicitly does not guarantee this. If you need to maintain relative ordering use stable_sort instead.
Documentation of sort which includes reference to equivalent elements
std::sort
is not guaranteed to be stable (the term you were trying to think of). As you'd guess,std::stable_sort
is guaranteed to be stable.std::stable_sort
also provides a guarantee on worst-case complexity, whichstd::sort
does not.std::sort
is typically faster on average though.From C++ reference: here
You might want stable_sort, but note that it's not as fast (in average)
The term for what you're describing is stability.
From SGI's STL docs:
Use
stable_sort
if you need this.