Cannot Use std::sort()

I’m trying to use std::sort() (provided by algorithm.h) in VEXcode EXP 2.4.0-430 on EXP Brain but it produces errors.

#include "vex.h"
#include <algorithm>

// Allows for easier use of the VEX Library
using namespace vex;
int main() {
  int arr[] = {5, 8, 6, 1, 7, 2, 3};
  // print array
  for(int a : arr){
    printf("%d ", a);
  }
  printf("\n");

  // sort array
  std::sort(arr, arr + (sizeof(arr) / sizeof(int)));
  // print array
  for(int a : arr){
    printf("%d ", a);
  }
  printf("\n");
  
}

The error doesn’t have a message, just a location of the algorithm include (line 43).
image

This thread implies algorithm.h works in VEXcode Pro.
Is algorithm.h not available for VEXcode EXP?

Thank you.

As far as I remember, the build system would need an update to allow full C++ access in VEXcode, and you really have no access to that. Perhaps switch over to using the VEX Robotics Visual Studio Code extension which provides far more flexibility. Bear in mind that using C++ standard library code on EXP will likely produce pretty big executables so I would avoid using any wireless downloading.

7 Likes

Thank you for your help, I appreciate it!

I’m leaving a followup for anyone in the future.
While C++ std::sort() isn’t available as of now, C qsort() does work! Using VEXcode EXP 2.4.2-0

See the following example:

#include "vex.h"
// Allows for easier use of the VEX Library
using namespace vex;

// sort in ascending, flip the signs for descending
int compare(const void *a,const void *b){
  if(*(int *)a > *(int *)b){
    return 1;
  }else if(*(int *)a < *(int *)b){
    return -1;
  }else{
    return 0;
  }
}

int main() {
  int arr[] = {5, 8, 6, 1, 7, 2, 3};
  // print array
  for(int a : arr){
    printf("%d ", a);
  }
  printf("\n");

  // sort array
  qsort(arr, sizeof(arr) / sizeof(int), sizeof(int), compare);

  // print array
  for(int a : arr){
    printf("%d ", a);
  }
  printf("\n");
  
}

Output on Console:

5 8 6 1 7 2 3
1 2 3 5 6 7 8

For more information on C qsort() see this guide

1 Like