1336: MemoryRiver Easy
题目
题目描述
本题中,仅需要实现一个不包含空间回收的文件读写类
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
,来实现文件的读写。
该类模板的声明为
c++
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
```c++
ifndef BPT_MEMORYRIVER_HPP
define BPT_MEMORYRIVER_HPP
include
using std::string; using std::fstream; using std::ifstream; using std::ofstream;
template
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:
设置本题的目的之一是为bookstore大作业作准备
二进制文件的读写(可参考https://en.cppreference.com/w/cpp/io/basic_fstream等资料)
c++
//T类型的对象t的二进制文件读写
file.write(reinterpret_cast<char *>(&t), sizeof(T));
file.read(reinterpret_cast<char *>(&t), sizeof(T));
//将指针移动到[文件头指针+offset]处
file.seekp(offset);
Oops! 本题目还没有解答!
助教老师们编题的速度,已经超过了解题的速度!
OJ翻了一新,但本解答集还大多用的是2017-2019级,甚至更早的同学们贡献的答案。
如果你已经AC了,可以的话,请您参考添加页面,与大家一起分享你的题解!