Multidimensional vectors

Lets say you have the vector(same as your comment on my answer’s post),

3 5 5 6

2 2 5 8

3 1 4 5

8 7 6 4

I want to sort the rows only based on the 2nd column such that my output is:

3 1 4 5

2 2 5 8

3 5 5 6

8 7 6 4

Note,how this output came, we are just sorting the coloumn1(0-indexed) i.e. 5 2 1 7. The entries The smallest element in the vector is 1, i.e. row 2 then 2(row 1) then 0(row0), then 7(row 3). Insert entries in the map as the pair <element, row number> If you don’t know that, let me know I’ll put that in code. As we know, entries in the map are sorted by their first value and this is logarithmic. What we’ll have in map now is <element, row number>. This will now tell you which row needs to be the first one in your sorted vector and which needs to be last or at what place. Still not clear? Let me know, i’ll code or rather show your code for the same, that will help more.

@sandy999 See below.

Damn me @damn_me :stuck_out_tongue: Now, maps are super new to me, so I am not familiar with any of the operations on map,. But I am able to get the logic now. If it’s trouble demonstrating the map operations in code like how you did for the original question could you please direct me to a resource which explains these operations well? Then, I’ll try sorting using this logic myself and attach the code. My gratitude is inexpressible. Thanks a lottt!

See this, http://ideone.com/v27HS0. Read about maps, iterators and basically STL. it’s really a powerful tool for many operations with less code to type with efficieny as an add-on. See : http://www.cplusplus.com/reference/utility/pair/
http://www.cplusplus.com/reference/map/map/
http://www.cplusplus.com/reference/utility/make_pair/