gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
Напиши функцию, которая бы вывела в файл (result.txt) std::map<CoordinatesInfo, size_t>.…
- User
Напиши функцию, которая бы вывела в файл (result.txt) std::map<CoordinatesInfo, size_t>. CoordinatesInfo - структура из int x и int y
- Assistant
Вот пример функции, которая выводит содержимое
std::map<CoordinatesInfo, size_t>в файл "result.txt":#include <iostream> #include <fstream> #include <map> struct CoordinatesInfo { int x; int y; }; void writeToFile(const std::map<CoordinatesInfo, size_t>& coordinatesMap) { std::ofstream outputFile("result.txt"); if (outputFile.is_open()) { for (const auto& entry : coordinatesMap) { outputFile << "Coordinates: (" << entry.first.x << ", " << entry.first.y << ") Size: " << entry.second << std::endl; } outputFile.close(); std::cout << "Data has been successfully written to result.txt" << std::endl; } else { std::cerr << "Unable to open the output file" << std::endl; } } int main() { std::map<CoordinatesInfo, size_t> coordinatesMap; // Заполнение map данными для примера coordinatesMap[{1, 2}] = 10; coordinatesMap[{3, 5}] = 15; coordinatesMap[{7, 9}] = 20; writeToFile(coordinatesMap); return 0; }