0

I have created 2D vector:

{{9,8,7},
 {6,1,4},
 {3,2,1}}

I want to sort using std::sort and I want to have:

{{1,1,2},
 {3,4,6},
 {7,8,9}}

I have no idea how to do it.

user0042
  • 7,917
  • 3
  • 24
  • 39
Michal123
  • 31
  • 4
  • 9
    Don't use a 2d vector. Store the 2d structure in a flat vector and then you can just pass that vector to `std::sort`. – NathanOliver Oct 24 '17 at 13:50
  • 1
    `vector>` is overused, you don't usually really want some list of lists. In particular, it's not the right type for matrices. – Baum mit Augen Oct 24 '17 at 13:55
  • So how to do it in the different way? – Michal123 Oct 24 '17 at 13:57
  • 1
    @Michal123 See [this](https://stackoverflow.com/questions/2151084/map-a-2d-array-onto-a-1d-array-c) on how you can map a 2d array to a 1d array – NathanOliver Oct 24 '17 at 13:58
  • Depends on what you want to do. If you do want a matrix: There are a lot of matrix classes readily available in libraries like Eigen, if you only need some features, you can also role your own. There are questions that demonstrate how here on SO. – Baum mit Augen Oct 24 '17 at 13:59

0 Answers0