I have created a C++ class called a SelectionArray that holds a vector alongside a dynamically sized bitset. Each item in the vector has a bit alongside it that represents whether said object is selected or not. I understand that I can sort the vector using std::sort. Can anyone think of a way to sort the bitset alongside the vector to maintain item selection? If not I will have to implement my own sorting function.
Asked
Active
Viewed 52 times
0
-
2You could write a wrapper class that allows accessing the vector and the bitset at once. With the right self written iterator you could iterate over your containers, both at once (using your wrapper object). That might be a solution. – Werner Henze Jun 19 '20 at 12:33
-
2*If not I will have to implement my own sorting function* -- Sort an array of indices. See [this answer and read the footnote](https://stackoverflow.com/questions/46382252/sort-array-by-first-item-in-subarray-c/46382976#46382976). Also [this answer](https://stackoverflow.com/questions/58050978/how-to-sort-multiple-arrays-based-on-one-and-print-them-out/58051327#58051327) – PaulMcKenzie Jun 19 '20 at 12:35
-
1as @WernerHenze said, a custom iterator is the way to go for that. then you could pass this iterator to the std::sort function and it will do the sort for you. You can find reference about what your iterator needs to implement in order to comply with standard c++ function just here: https://en.cppreference.com/w/cpp/named_req in the `Iterator` section – Nico J. Jun 19 '20 at 12:41