skip to Main Content

Here is my code:

#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
void test()
{
    vector<vector<int>> info = {{0,0,999}, {0,1,1000}, {0,2,1001},
                {0,3,1002}, {0,4,1003}, {0,5,1004},
                {0,6,1005}, {0,7,1006}, {1,0,1000},
                {1,1,999}, {1,2,1000}, {1,3,1001},
                {1,4,1002}, {1,5,1003}, {1,6,1004},
                {1,7,1005}, {2,0,1001}, {2,1,1000},
                {2,2,999}, {2,3,1000}, {2,4,1001},
                {2,5,1002}, {2,6,1003}, {2,7,1004},
                {3,0,1002}, {3,1,1001}, {3,2,1000},
                {3,3,999}, {3,4,1000}, {3,5,1001},
                {3,6,1002}, {3,7,1003}, {4,0,1003},
                {4,1,1002}, {4,2,1001}, {4,3,1000},
                {4,4,999}, {4,5,1000}, {4,6,1001},
                {4,7,1002}, {5,0,1004}, {5,1,1003},
                {5,2,1002}, {5,3,1001}, {5,4,1000},
                {5,5,999}, {5,6,1000}, {5,7,1001},
                {6,0,1005}, {6,1,1004}, {6,2,1003},
                {6,3,1002}, {6,4,1001}, {6,5,1000},
                {6,6,999}, {6,7,1000}, {7,0,1006},
                {7,1,1005}, {7,2,1004}, {7,3,1003},
                {7,4,1002},{7,5,1001}};  
    auto cmp=[](const auto& a,const auto& b){
      if(a[2]<b[2])return true;
      if(a[0]<b[0])return true;
      return a[1]<b[1];       
 };

 sort(begin(info),end(info),cmp);

}
int main()
{
  test();
}

I use g++ 13.2 compile under Ubuntu 24.04 LTS(Linux 6.8.0-39-generic) build and run the code crashes on sort, very strange:

zzhao@zzhao-System-Version:~$ g++  -std=c++20 z3.cpp

zzhao@zzhao-System-Version:~$ ./a.out

Segmentation fault (core dumped)

2

Answers


  1. Your comparator does not respect strict-weak ordering.

    Take for example:
    a = {2,0,1}, b = {1,0,2}

    It will return true for a<b (due to the first if in the comparator), but also true for b<a (due to the second if in it).

    Since this does not satisfy the requirement of a comparator for std::sort, it causes undefined behavior.

    Login or Signup to reply.
  2. The code is crashing because the comparison function for sorting is messed up. Basically, it isn’t following the rules that std::sort needs to work properly. The original function is all over the place, sometimes saying one thing is smaller, sometimes not – it is super inconsistent.

    To fix it, I had to rewrite the comparison part. Now it checks the third number first, then the first, then the second. It’s way more logical and consistent this way.

    #include <vector>
    #include <algorithm>
    #include <iostream>
    
    void test() {
        std::vector<std::vector<int>> info = {{0,0,999}, {0,1,1000}, {0,2,1001},
                    {0,3,1002}, {0,4,1003}, {0,5,1004},
                    {0,6,1005}, {0,7,1006}, {1,0,1000},
                    {1,1,999}, {1,2,1000}, {1,3,1001},
                    {1,4,1002}, {1,5,1003}, {1,6,1004},
                    {1,7,1005}, {2,0,1001}, {2,1,1000},
                    {2,2,999}, {2,3,1000}, {2,4,1001},
                    {2,5,1002}, {2,6,1003}, {2,7,1004},
                    {3,0,1002}, {3,1,1001}, {3,2,1000},
                    {3,3,999}, {3,4,1000}, {3,5,1001},
                    {3,6,1002}, {3,7,1003}, {4,0,1003},
                    {4,1,1002}, {4,2,1001}, {4,3,1000},
                    {4,4,999}, {4,5,1000}, {4,6,1001},
                    {4,7,1002}, {5,0,1004}, {5,1,1003},
                    {5,2,1002}, {5,3,1001}, {5,4,1000},
                    {5,5,999}, {5,6,1000}, {5,7,1001},
                    {6,0,1005}, {6,1,1004}, {6,2,1003},
                    {6,3,1002}, {6,4,1001}, {6,5,1000},
                    {6,6,999}, {6,7,1000}, {7,0,1006},
                    {7,1,1005}, {7,2,1004}, {7,3,1003},
                    {7,4,1002},{7,5,1001}};  
    
        auto cmp = [](const auto& a, const auto& b) {
            if (a[2] != b[2]) return a[2] < b[2];
            if (a[0] != b[0]) return a[0] < b[0];
            return a[1] < b[1];
        };
    
        std::sort(begin(info), end(info), cmp);
    
        for (const auto& v : info) {
            std::cout << v[0] << " " << v[1] << " " << v[2] << std::endl;
        }
    }
    
    int main() {
        test();
        return 0;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search