《Cracking the Coding Interview》——第8章:面向对象设计——题目5
2014-04-23 19:11
405 查看
2014-04-23 18:42
题目:设计一个在线阅读系统的数据结构。
解法:这题目太大了,我的个亲娘。显然你不可能一次加载一整本书,做到单页纸加载的粒度是很必要的。为了读书的连贯效果,预取个几页也很必要。
代码:
题目:设计一个在线阅读系统的数据结构。
解法:这题目太大了,我的个亲娘。显然你不可能一次加载一整本书,做到单页纸加载的粒度是很必要的。为了读书的连贯效果,预取个几页也很必要。
代码:
// 8.5 Deign a class for an online book reader system. #include <unordered_map> using namespace std; class BookReaderSystem { public: BookReaderSystem() {}; // find a book by its name and read it. void readBook(string book_name) { Page page; if (books.find(book_name) != books.end()) { i = 0; // load the book page by page while (page = loadPage(books[book_name], i, page)) { displayPage(page); ++i; } } }; ~BookReaderSystem() { books.clear(); }; private: // the K-V database unordered_map<string, Book> books; // load one page from a book bool loadPage(Book &book, int page_no, Page &page); // display this page void displayPage(Page &page); };
相关文章推荐
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目4
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目9
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目10
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目6
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目7
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目1
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目8
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目2
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目3
- 《Cracking the Coding Interview》——第13章:C和C++——题目1
- 《Cracking the Coding Interview》——第14章:Java——题目3
- 《Cracking the Coding Interview》——第18章:难题——题目6
- 《Cracking the Coding Interview》——第1章:数组和字符串——题目6
- 《Cracking the Coding Interview》——第3章:栈和队列——题目3
- 《Cracking the Coding Interview》——第6章:智力题——题目2
- 《Cracking the Coding Interview》——第6章:智力题——题目5
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目7
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目2
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目7
- 《Cracking the Coding Interview》——第13章:C和C++——题目10