Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
template <typename BinaryFunction>
void sort(BinaryFunction comparator) { ... };
// ...
sort( [](int a, int b) { return true; /* ... */ } );
using AutoFile = std::unique_ptr<FILE, decltype(&fclose)>;
const auto openFileInC = [](const std::string& name) { return AutoFile(fopen(name.c_str(), "w"), fclose); };
using AutoHandle = std::unique_ptr<void, decltype(&CloseHandle)>;
const auto openFileInApi = [](const std::string& name)
{
return AutoHandle(::CreateFileA(name.c_str(), FILE_ALL_ACCESS, 0, nullptr, CREATE_ALWAYS, 0, nullptr), CloseHandle);
};
int main()
{
// RAII wrapper will auto close descriptor
auto fileInC = openFileInC("1.txt");
fprintf(fileInC.get(), "Hello");
// RAII wrapper will auto close handle
auto fileInApi = openFileInApi("2.txt");
WriteFile(fileInApi.get(), "World", 5, nullptr, nullptr);
return 0;
}
К слову это не пузырек, а сортировка выбором.
Может я несколько выпал из времени, но, например, qsort так работает испокон веков...
Полиморфизм и указатели на функции