0

Teh carried by the vector is a pair indexed by an id:

<std::pair<int, std::pair<int, int>>>

         496              1,   256

        (message id)   

As you can see, the "id" is repetitive. For further processing of this data, it will be logically very useful for me to sort that triplet on the basis of "message id". How can I do this using STL functions ?

Here is some data sample:

 15:38:08.307 - (I) ET02 -  For message Id: 496 Tag - value pair: 1 - 256
 15:38:08.307 - (I) ET02 -  For message Id: 496 Tag - value pair: 2 - 27060
 15:38:08.307 - (I) ET02 -  For message Id: 496 Tag - value pair: 3 - 2014-06-16T17:07:00.519
 15:38:08.307 - (I) ET02 -  For message Id: 487 Tag - value pair: 1 - 1044
 15:38:08.307 - (I) ET02 -  For message Id: 487 Tag - value pair: 2 - 9098150000
 15:38:08.307 - (I) ET02 -  For message Id: 498 Tag - value pair: 1 - 9098150000
 15:38:08.307 - (I) ET02 -  For message Id: 498 Tag - value pair: 2 - 9098150000
 15:38:08.307 - (I) ET02 -  For message Id: 498 Tag - value pair: 3 - 5902400000000
 15:38:08.307 - (I) ET02 -  For message Id: 498 Tag - value pair: 501 - 256000000000
 15:38:08.307 - (I) ET02 -  For message Id: 498 Tag - value pair: 502 - 0
 15:38:08.307 - (I) ET02 -  For message Id: 498 Tag - value pair: 503 - 0
 15:38:08.307 - (I) ET02 -  For message Id: 498 Tag - value pair: 504 - 9098150000
 15:38:08.307 - (I) ET02 -  For message Id: 498 Tag - value pair: 505 - 9098150000
13
  • 1
    Depending on that "first pair" means, it could be as simpla as std::sort(v.begin(), v.end());. Commented Oct 24, 2013 at 10:26
  • Please put code in the question, not the title. Commented Oct 24, 2013 at 10:26
  • @juanchopanza I mean the message id. Which is the 'first' of the first pair. Commented Oct 24, 2013 at 10:28
  • 1
    Just try the sort I suggested then. But "first pair" isn't descriptive enough. I still have to guess what you mean. Commented Oct 24, 2013 at 10:29
  • 1
    @RitwikG Just implement the comparator and pass it to the sort function as the third parameter. Have a look at this stackoverflow.com/questions/1380463/… Commented Oct 24, 2013 at 10:50

1 Answer 1

8

if you use std::sort(v.begin(), v.end());, it will sort by the first element, then by the second. So if you just use the default sort, you'll get the desired ordering. This is because std::pair overloads the < operator to firstly sort by the first element and then by the second.

Sign up to request clarification or add additional context in comments.

2 Comments

+1, and special note, "then by the second", if the "second" is also a pair, will do the exact same thing, which can be damn-handy.
@WhozCraig Thanks for the extra note :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.