以下是冒泡排序的函数模板的代码: ```cpp template <typename T> void bubbleSort(T arr[], int size) { for (int i = 0; i < size - 1; i++) { for (int j = 0; j < size - i - 1; j++) { if (arr[j] < arr[j + 1]) { std::swap(arr[j], arr[j + 1]); } } } } ``` 以下是 txtdata 类的代码: ```cpp #include <iostream> #include <fstream> #include <string> #include <algorithm> class txtdata { private: std::string stringsData[10]; public: txtdata(const std::string& filename) { std::ifstream file(filename); if (!file) { std::cout << "Failed to open the file." << std::endl; return; } std::string line; int count = 0; while (count < 10 && std::getline(file, line)) { stringsData[count++] = line; } file.close(); } ~txtdata() { std::ofstream file("reversed.txt"); if (!file) { std::cout << "Failed to open the file." << std::endl; return; } std::reverse(stringsData, stringsData + 10); for (int i = 0; i < 10; i++) { file << stringsData[i] << std::endl; } file.close(); } }; ``` 以下是 bindata 类的代码: ```cpp #include <iostream> #include <fstream> #include <string> #include <algorithm> class bindata { private: int intData[10]; public: bindata(const std::string& filename = "") { if (filename != "") { std::ifstream file(filename, std::ios::binary); if (!file) { std::cout << "Failed to open the file." << std::endl; return; } file.read(reinterpret_cast<char*>(intData), 10 * sizeof(int)); if (!file) { std::cout << "Failed to read the file." << std::endl; return; } file.close(); } } ~bindata() {} void savedata(const std::string& filename) { std::ofstream file(filename, std::ios::binary); if (!file) { std::cout << "Failed to open the file." << std::endl; return; } file.write(reinterpret_cast<const char*>(intData), 10 * sizeof(int)); if (!file) { std::cout << "Failed to write the file." << std::endl; return; } file.close(); } friend std::ostream& operator<<(std::ostream& os, const bindata& obj) { for (int i = 0; i < 10; i++) { os << obj.intData[i] << std::endl; } return os; } }; ``` 以下是使用示例代码: ```cpp int main() { txtdata txt("c:\\isy1311123.txt"); for (int i = 0; i < 10; i++) { std::cout << txt.stringsData[i] << std::endl; } bindata bina("c:\\lisy13.bin"); std::cout << bina; return 0; } ``` [2024-06-01 16:23:06 | AI写代码神器 | 1941点数解答]