# c++ - c ++ - 我可以在堆分配的原始数组上使用std :: sort吗？

``````
int arra[100];

for (int i = 0; i < 100; i++) {

arra[i] = rand() % 32000;

}

for (int i = 0; i < len; i++)std::cout << arra[i]<<"";

std::sort(arra,arra+100);

``````

``````
int len;

len = 100;

int* arr = new int[len];

for (int i = 0; i < len; i++) {

arr[i] = rand() % 32000;

}

``````

``````
std::sort(arr, arr + len);

``````

``````
#include<algorithm>

#include<iostream>

int main() {

int len;

len = 100;

int* arr = new int[len];

for (int i = 0; i < len; i++) {

arr[i] = rand() % 32000;

}

//Valid, Defined Behavior that works as expected

std::sort(arr, arr + len);

//alternative, to make the code easier to read:

//auto begin = arr;

//auto end = arr + len;

//std::sort(begin, end);

for(int i = 0; i < len; i++)

std::cout << arr[i] << std::endl;

}

``````

``````
#include<algorithm>

#include<iostream>

int main() {

int len;

len = 100;

int* arr = new int[len];

for (int i = 0; i < len; i++) {

arr[i] = rand() % 32000;

}

//MSVC Specific Code!

auto begin = stdext::make_checked_array_iterator(arr, len);

auto end = arr + len;

std::sort(begin, end);

for(int i = 0; i < len; i++)

std::cout << arr[i] << std::endl;

}

``````