我正在整理
std::vector<std::pair<float, std::string>>
按升序排列。
std::sort
工作,我发现字符串影响顺序,如果浮点数有相同的值。
我的代码:
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector<std::pair<float, std::string>> vec;
vec = {{1, "e"}, {1, "d"}, {1, "c"}, {1, "b"}, {1, "a"}};
std::sort(vec.begin(), vec.end());
for (auto i : vec)
std::cout << i.first << ", " << i.second << '\n';
}
我得到的结果是:
(值绑定时按字母顺序排列)
1, a
1, b
1, c
1, d
1, e
Program ended with exit code: 0
我想要的结果是:
(以前的元素在值绑定时优先)
1, e
1, d
1, c
1, b
1, a
Program ended with exit code: 0