I have already read about differences between std::partition and std::stable_partition. Also I have watched the cppreference example
int main()
{
std::vector<int> v = {0,1,2,3,4,5,6,7,8,9};
std::cout << "Original vector:\n ";
for(int elem : v) std::cout << elem << ' ';
auto it = std::partition(v.begin(), v.end(), [](int i){return i % 2 == 0;});
std::cout << "\nPartitioned vector:\n ";
}
Original vector: 0 1 2 3 4 5 6 7 8 9
Partitioned vector: 0 8 2 6 4 * 5 3 7 1 9
But I'm still don't understand why in this example the order always the same. All elements in the std::vector save in the heap, and iterator and address stability of elements is guaranteed with std::vector only if no reallocation takes place. After std::partition this guarantee is violated, so the order can be different, but why it always 0 8 2 6 4.
Or am I wrong and have a problem with memory visualisation of std::vector elements?
Aucun commentaire:
Enregistrer un commentaire