safe and sound

I remember tears streaming down your face   
我记得泪水顺着你的脸颊流下   
when I said, I’ll never let you go  
当我说我将永不放开你的手  
When all those shadows almost killed your light  
当所有的阴影几乎挡住了你的光亮   
I remember you said, don’t leave me here alone  
我记得你说过:“别把我一个人扔下。” 
But all that’s dead and gone and passed tonight  
但这一切都在今晚化作尘埃   
Just close your eyes   
只需闭上你的眼睛   
the sun is going down  
太阳已西沉   
You’ll be alright  
你会没事的   
no one can hurt you now 
如今没有人能伤害你了  
Come morning light  
当明日晨光初现   
You and I’ll be safe and sound  
我们都将安然无恙   
Don’t you dare look out your window darling  
亲爱的,你害怕看到窗外吧  
everything’s on fire   
一切都在燃烧  
The war outside our door keeps raging on   
门外的战争仍在激烈的进行   
hold onto this lullaby  
牢牢记得我为你唱的这只摇篮曲  
even when the music’s gone  
即使音乐已经停止   
Just close your eyes  
只需闭上你的眼睛  
the sun is going down  
太阳已西沉   
You’ll be alright   
你会没事的  
no one can hurt you now 
如今没有人能伤害你了 
Come morning light   
当明日晨光初现  
You and I’ll be safe and sound 
我们都将安然无恙  
Just close your eyes  
只需闭上你的眼睛  
You’ll be alright   
你会没事的  
Come morning light,  
当明日晨光初现   
You and I’ll be safe and sound  
我们都将安然无恙
题目描述 Description 本题中,需要实现一个包含空间回收的文件读写类 Where the north wind meets the sea There's a river full of memory Sleep, my darling, safe and sound For in this river all is found —— All is Found 文件存储操作是编程的重要部分,其分为 ASCII 文件存储和二进制文件存储。文件其实是一块白纸,交由你创作出块状、链状、树状的画作,以及本题中需要实现的 MemoryRiver 类。 MemoryRiver 类是为了方便对文件的操作而封装了一些操作。该类的功能有 与一个文件名(string)绑定 往文件中写入一个 int 或 T类型对象 往文件中读取一个 int 或 T类型对象(T 的大小大于 int) 删除并回收一块空间。 请实现一个类模板 MemoryRiver.hpp,来实现文件的读写。 该类模板的声明为 template<class T, int info_len = 2> class MemoryRiver; 其中,T 是需要存储的原子对象的类,info_len 是存储文件头部预留出来的 int 的个数(1_base)。 MemoryRiver 的存储策略为在文件首预留出 info_len 个 int 作为存储一些参数的空间,这些 int 在 initialise() 函数种被初始化为 0。 请完成 MemoryRiver.hpp 内的代码: MemoryRiver.hpp #ifndef BPT_MEMORYRIVER_HPP #define BPT_MEMORYRIVER_HPP #include <fstream> using std::string; using std::fstream; using std::ifstream; using std::ofstream; template<class T, int info_len = 2> class MemoryRiver { private: /* your code here */ fstream file; string file_name; int sizeofT = sizeof(T); public: MemoryRiver() = default; MemoryRiver(const string& file_name) : file_name(file_name) {} void initialise(string FN = "") { if (FN != "") file_name = FN; file.open(file_name, std::ios::out); int tmp = 0; for (int i = 0; i < info_len; ++i) file.write(reinterpret_cast<char *>(&tmp), sizeof(int)); file.close(); } //读出第n个int的值赋给tmp,1_base void get_info(int &tmp, int n) { if (n > info_len) return; /* your code here */ } //将tmp写入第n个int的位置,1_base void write_info(int tmp, int n) { if (n > info_len) return; /* your code here */ } //在文件合适位置写入类对象t,并返回写入的位置索引index //位置索引意味着当输入正确的位置索引index,在以下三个函数中都能顺利的找到目标对象进行操作 //位置索引index可以取为对象写入的起始位置 int write(T &t) { /* your code here */ } //用t的值更新位置索引index对应的对象,保证调用的index都是由write函数产生 void update(T &t, const int index) { /* your code here */ } //读出位置索引index对应的T对象的值并赋值给t,保证调用的index都是由write函数产生 void read(T &t, const int index) { /* your code here */ } //删除位置索引index对应的对象(不涉及空间回收时,可忽略此函数),保证调用的index都是由write函数产生 void Delete(int index) { /* your code here */ } }; #endif //BPT_MEMORYRIVER_HPP tips: 二进制文件的读写 (可参考 https://en.cppreference.com/w/cpp/io/basic_fstream 等资料) //T类型的对象t的二进制文件读写 file.write(reinterpret_cast<char *>(&t), sizeof(T)); file.read(reinterpret_cast<char *>(&t), sizeof(T)); //将指针移动到[文件头指针+offset]处 file.seekp(offset); 可以使用文件(多)链表的思想来实现空间回收。 也可以用类似内存池的思想,多开一个文件进行管理。 可以隐式的增加文件头部的 info_len 的长度供自己使用,但是需要在 get_info 和 write_info 时作一个转换的映射避免冲突 本题未对原子化操作做出要求,但在大作业中推荐在每个函数调用前打开文件,调用后关闭文件。 评测的文件数量限制为 20,但最好不要超过 2 个文件 2≤infolen2≤infolen 解决这道题目
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值