Sorting a std::vector<std::pair<std::string,bool>> by the string?

12,647

Solution 1

std::vector<std::pair<std::string, bool> > v;
std::sort(v.begin(), v.end());

std::pair overloads operator< to sort first by the first element then by the second element. Thus, if you just sort the vector using the default sort ordering (operator<), you'll get your desired ordering.

Solution 2

I really like James' answer, but there's one other option you might want to consider - just funnel everything into a std::map:

std::map<std::string, bool> myMap(v.begin(), v.end());

Or, if you have duplicate strings, a std::multimap:

std::multimap<std::string, bool> myMultiMap(v.begin(), v.end());

This does have the added advantage that if you then need to add or remove new key/value pairs, you can do so in O(lg n), as opposed to O(n) for the sorted vector.

If you really must use a vector, then go with James' answer. However, if you have a vector of pairs, there's a good chance that you really want a std::map.

Solution 3

Answer to "duplicate question" of this: link: Sort a vector of pairs by first element then by second element of the pair in C++?

bool cmp(const pair<int,int>&x,const pair<int,int>y){
if(x.first==y.first){
   return(x.second<y.second);
}
return(x.first<y.first);
}

array of pairs before:
5 2
4 2
8 2
8 3
8 1
array of pairs after:
4 2
5 2
8 1
8 2
8 3
Share:
12,647
jmasterx
Author by

jmasterx

Hi

Updated on June 27, 2022

Comments

  • jmasterx
    jmasterx almost 2 years

    How can I sort this vector by comparing the pair.first which is an std::string? (without providing a static compare function, nor use boost).

  • CB Bailey
    CB Bailey over 13 years
    This is a C++0x only answer. ;) Edit: Now fixed (>> token closing two nested template <> pairs is C++0x only).
  • James McNellis
    James McNellis over 13 years
    @Charles: Ha! Yeah, I probably do that in a lot of answers. I'm too used to using a compiler that supports >>.
  • James McNellis
    James McNellis over 13 years
    Note that select1st is not part of the C++ Standard Library.
  • Oliver Charlesworth
    Oliver Charlesworth over 13 years
    +1: I didn't know that std::pair::operator<() was overloaded. I do now!
  • ephemient
    ephemient over 13 years
    Mmm. Luckily, it's trivial to write: template<class T> struct select1st : public unary_function<T, typename T::first_type> { const typename T::first_type& operator()(const T& x) const {return x.first;} };
  • jmasterx
    jmasterx over 13 years
    I need to consider the case where the user does not want them sorted and in the order they gave them.
  • edA-qa mort-ora-y
    edA-qa mort-ora-y over 13 years
    Just to be picky, this requires that the second item in the pair has < defined. If the second type is not sortable then this will fail.
  • James McNellis
    James McNellis over 13 years
    @edA: Yes. The OP's pair has a bool as its second element type, which is comparable.
  • Reunanen
    Reunanen about 10 years
    Also vector + sort may in practice be much faster than inserting lots of stuff into a (multi)map, regardless of what the big-O notation says.