C++11: Dynamic Memory
来源:互联网 发布:新歌2016网络吐槽红歌 编辑:程序博客网 时间:2024/05/22 15:43
#include <vector>#include <memory>#include <string>#include <map>#include <set>#include <iostream>#include <sstream>#include <fstream>class QueryResult;class TextQuery {public: using line_no = std::vector<std::string>::size_type; TextQuery(std::ifstream&); QueryResult query(const std::string &);private: std::shared_ptr<std::vector<std::string>> file; std::map<std::string, std::shared_ptr<std::set<line_no>>> wm;};std::ostream& print(std::ostream &, const QueryResult &);class QueryResult { friend std::ostream& print(std::ostream &, const QueryResult &);public: using line_no = std::vector<std::string>::size_type; QueryResult(std::string s, std::shared_ptr<std::set<line_no>> p, std::shared_ptr<std::vector<std::string>> f) : sought(s), lines(p), file(f) { }private: std::string sought; std::shared_ptr<std::set<line_no>> lines; std::shared_ptr<std::vector<std::string>> file;};void runQueries(std::ifstream &is){ TextQuery tq(is); while (true) { std::cout << "enter word to look for, or q to quit: "; std::string s; if (!(std::cin >> s) || s == "q") break; print(std::cout, tq.query(s)) << std::endl; }}int main(){ std::ifstream is("data.dat"); runQueries(is); return 0;}TextQuery::TextQuery(std::ifstream &is) : file(new std::vector<std::string>){ std::string text; while (std::getline(is, text)) { file->push_back(text); int n = file->size() - 1; std::istringstream line(text); std::string word; while (line >> word) { auto &lines = wm[word]; if (!lines) { lines.reset(new std::set<line_no>); } lines->insert(n); } }}QueryResult TextQuery::query(const std::string &word){ static std::shared_ptr<std::set<line_no>> nodata(new std::set<line_no>); auto loc = wm.find(word); if (loc == wm.end()) { return QueryResult(word, nodata, file); } else { return QueryResult(word, loc->second, file); }}std::string make_plural(std::size_t ctr, const std::string &word, const std::string &ending){ return (ctr > 0) ? word + ending : word;}std::ostream& print(std::ostream &os, const QueryResult &qr){ os << qr.sought << " occurs " << qr.lines->size() << " " << make_plural(qr.lines->size(), "time", "s") << std::endl; for (auto num : *qr.lines) { os << "\t(line " << num + 1 << ") " << *(qr.file->begin() + num) << std::endl; } return os;}
/* clang++ xx.cpp -std=c++11** Apple LLVM version 7.0.2 (clang-700.1.81)** Target: x86_64-apple-darwin15.2.0** Thread model: posix*/// C++ primer 5th: Using the Library: A Text-Query Program(p484)
0 0
- C++11: Dynamic Memory
- Pointers on C——11 Dynamic Memory Allocation.1
- Pointers on C——11 Dynamic Memory Allocation.2
- Pointers on C——11 Dynamic Memory Allocation.3
- Pointers on C——11 Dynamic Memory Allocation.4
- Pointers on C——11 Dynamic Memory Allocation.5
- Pointers on C——11 Dynamic Memory Allocation.6
- Dynamic Memory
- [转载]Dynamic Memory Allocation and Fragmentation in C and C++
- ANSI C, Variable Length Array, Dynamic Memory Allocation, Memory Reservation and Late Binding
- Dynamic memory allocation example
- Geeksquiz | Dynamic Memory Allocation
- 12-Dynamic Memory
- dynamic memory allocation
- dynamic memory allocation
- Dynamic Memory Networks & DMN+
- Chapter 12. Dynamic Memory
- C++11学习笔记——Dynamic memory management
- [LeetCode 326] Power of Three
- linux 光驱弹出
- Contacts(简介)
- JQuery总结:选择器归纳、DOM遍历和事件处理、DOM完全操作和动画
- tomcat 启动一段时间后 出问题。 无错误日志,页面卡住
- C++11: Dynamic Memory
- 使用Java为图片添加水印
- Android 讲解:ListView
- linux驱动_最简单的例子
- NSOperation
- Android 好的源码依赖包 收集
- JAR export finished with warnings. See details for additional information
- 图像处理 UIGraphicsBeginImageContext
- 数据解析的时间给model赋字典的值