如何将 std::sort() 与 std::unique_ptr<[]> 一起使用?

sun*_*kue 1 c++ std

std::unique_ptr<type[]> D{ std::make_unique<type[]>(10'000) };

std::sort(D2[0], D2[10'000], [](auto& a, auto& b)->bool {return a < b; }); //error

Run Code Online (Sandbox Code Playgroud)

std::unique_ptr<[]>用来读取二进制文件。如果我想对这些数据进行排序,我是否必须制作另一个矢量或其他东西?我不知道如何将sortfunc 与unique_ptr<[]>. 你能告诉我路吗?

Ayx*_*xan 5

您可以使用指针作为排序例程的迭代器:

std::sort(D.get(), D.get() + 10'000);
Run Code Online (Sandbox Code Playgroud)