Sorting a std::vector> by the string?

后端 未结 4 1375
猫巷女王i
猫巷女王i 2020-11-28 15:25

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

4条回答
  •  星月不相逢
    2020-11-28 15:57

    std::vector > 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.

提交回复
热议问题