std :: sort与pair <unsigned long =“” long,int =“”>的怪异行为

时间:2018-11-06 16:32:11

标签: c++ sorting stl

我尝试使用lambda作为比较器,按相关时间戳对一些值(此处为int)进行排序:

vector<pair<unsigned long long,int>> v = {
    {1541518270397ull,0},
    {1541518270396ull,0},
    {1541518270394ull,0},
    {1541518270395ull,0},
    {1541518270385ull,0},
    {1541518270391ull,0},
    {1541518270390ull,0},
    {1541518270392ull,0},
    {1541518270386ull,0},
    {1541518270393ull,0},
    {1541518270389ull,0},
    {1541518270388ull,0},
    {1541518270401ull,0},
    {1541518270398ull,0},
    {1541518270399ull,0},
    {1541518270400ull,0},
    {1541518270402ull,0},
    {1541518270403ull,0}
};

std::sort(v.begin(), v.end(), [](const auto& p1, const auto& p2){ return p2.first >= p1.first; });
for (const auto& e : v) cout << e.first << endl;

(我知道std::less将在这里完成工作。我的真实代码可以完成更复杂的事情,但是此示例会触发相同的问题)。当使用Apple LLVM版本9.1.0(clang-902.0.39.2)进行编译时,-std=c++14 -stdlib=libc++该代码将产生错误的(?)输出:

1541518270385
1541518270386
1541518270388
1541518270389
1541518270390
1541518270391
1541518270392
1541518270394
1541518270401 <------ here
1541518270395
1541518270393 <------ and here
1541518270396
1541518270397
1541518270398
1541518270399
1541518270400
1541518270402
1541518270403

当我在GCC上运行该命令时,该命令是预期的。我想念什么?

0 个答案:

没有答案