1

I'm running the Pigeonhole Sort code which using C++, and use the VS2017 and VS code to compiler. That creates an array of vectors. Size of array range. It had not worked in VS2017 but it had work in VS code. What's wrong between two editors?

I do the following

int arr[] = { 8, 3, 2, 7, 4, 6, 8 };
int n = sizeof(arr) / sizeof(arr[0]);

printf("Sorted order is : ");
for (int i = 0; i < n; i++)
    printf("%d ", arr[i]);

    int min = arr[0], max = arr[0];
    for (int i = 1; i < n; i++) {
        if (arr[i] < min)
            min = arr[i];
        if (arr[i] > max)
            max = arr[i];
    }
    int range = max - min + 1; 


    vector<int> holes[range];


    for (int i = 0; i < n; i++)
        holes[arr[i] - min].push_back(arr[i]);


    int index = 0; 
    for (int i = 0; i < range; i++) {
        vector<int>::iterator it;
        for (it = holes[i].begin(); it != holes[i].end(); ++it)
            arr[index++] = *it;

the code vector holes[range] can run in VS code but it can not run in VS2017. It was an error when it complier in VS2017 like this: error: expression must have a constant value. What's the problem in here and how to debug? please help me!

Castro
  • 11
  • 1
  • 2
  • https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard – Retired Ninja Sep 02 '19 at 23:43
  • Thank you, I know the reason. Complier difference can cause the error. I used the clang compiler in VS code on mac and used the VS community on windows. Maybe this why I can get the error from these compilers. – Castro Sep 09 '19 at 02:35

0 Answers0