C++:冒泡排序函数模板与文件读写类(txtdata、bindata)的实现及应用
以下是冒泡排序的函数模板的代码: ```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; } ``` ######[AI写代码神器 | 1941点数解答 | 2024-06-01 16:23:06]
- C++:bindata 类实现文件二进制整数读取、排序与归并合并输出(GPT | 1814点数解答 | 2024-06-01 16:28:14)311
- C++:bindata 类实现文件二进制读取、排序与归并合并输出(GPT | 811点数解答 | 2024-06-01 16:29:51)293
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)343
- 批处理文件:依据参数表.xlsx精准修改文件.txt中p、t、s值(GPT | 811点数解答 | 2024-12-05 12:58:37)377
- 用批处理和Python实现“参数表.xlsx”与“文件.txt”数据匹配及修改(讯飞星火 | 531点数解答 | 2024-12-05 13:20:42)365
- 批处理文件:依据参数表.xlsx精准修改文件.txt特定参数值(阿里通义 | 495点数解答 | 2024-12-05 13:22:26)346
- C++:冒泡排序函数模板与文件读写类(txtdata、bindata)的实现及应用(GPT | 1941点数解答 | 2024-06-01 16:23:06)310
- C 语言实现链表操作:创建、输出、删除及插入功能详解(字节豆包 | 534点数解答 | 2024-12-09 21:33:48)338
- C语言实现链表创建、输出、删除与插入操作的完整代码及注意要点(字节豆包 | 609点数解答 | 2024-12-18 12:18:21)358
- C++编程:冒泡排序函数模板、txtdata类与bindata类的实现及应用(GPT | 1551点数解答 | 2024-06-01 16:02:33)324
- 不使用冒泡排序,用归并排序降序合并 bina 与 binb 并输出结果!(GPT | 48点数解答 | 2024-06-01 16:26:38)317
- Express 应用创建与数据查询 API 实现:附代码示例与注意要点(字节豆包 | 387点数解答 | 2024-12-05 09:09:59)224