I'd like to find the sort order of a vector, for example, without reordering the vector.
I can think of several ways to do this, I'm wondering if I'm missing some built-in STL or BOOST way to do this.
I imagine if the functionality were available the code would end up looking something like this:
std::vector<float> unsortedSeq;
unsortedSeq.push_back( 1.1 );
unsortedSeq.push_back( 1.0 );
unsortedSeq.push_back( 0.5 );
unsortedSeq.push_back( 1.2 );
unsortedSeq.push_back( 1.15 );
std::list<std::size_t> sortOrder;
std::sort_indices( unsortedSeq.begin(), unsortedSeq.end(), sortOrder.begin() );
BOOST_FOREACH( std::size_t index, sortOrder )
{
std::cout << index << "\n"
}
2
1
0
4
3
Anyone know any STL or BOOST-sims that would do what I'm asking about as simply as shown?
std::vector<float> v;
// filled somewhere else
std::vector<std::size_t> indices(v.size());
// iota is from <numeric>, C++0x
std::iota(indices.begin(), indices.end(), 0);
std::sort(indices.begin(), indices.end(), [&v](std::size_t left, std::size_t right)
{
return v[left] < v[right];
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With