forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
205 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,69 @@ | ||
{ | ||
"files.associations": { | ||
"vector": "cpp" | ||
"vector": "cpp", | ||
"iosfwd": "cpp", | ||
"random": "cpp", | ||
"xstring": "cpp", | ||
"bitset": "cpp", | ||
"set": "cpp", | ||
"tuple": "cpp", | ||
"algorithm": "cpp", | ||
"cctype": "cpp", | ||
"cmath": "cpp", | ||
"cstddef": "cpp", | ||
"cstdint": "cpp", | ||
"cstdio": "cpp", | ||
"cstdlib": "cpp", | ||
"cstring": "cpp", | ||
"ctime": "cpp", | ||
"cwchar": "cpp", | ||
"deque": "cpp", | ||
"exception": "cpp", | ||
"forward_list": "cpp", | ||
"fstream": "cpp", | ||
"functional": "cpp", | ||
"initializer_list": "cpp", | ||
"iomanip": "cpp", | ||
"ios": "cpp", | ||
"iostream": "cpp", | ||
"istream": "cpp", | ||
"iterator": "cpp", | ||
"limits": "cpp", | ||
"list": "cpp", | ||
"locale": "cpp", | ||
"map": "cpp", | ||
"memory": "cpp", | ||
"new": "cpp", | ||
"numeric": "cpp", | ||
"ostream": "cpp", | ||
"regex": "cpp", | ||
"sstream": "cpp", | ||
"stack": "cpp", | ||
"stdexcept": "cpp", | ||
"streambuf": "cpp", | ||
"string": "cpp", | ||
"system_error": "cpp", | ||
"type_traits": "cpp", | ||
"typeinfo": "cpp", | ||
"unordered_map": "cpp", | ||
"unordered_set": "cpp", | ||
"utility": "cpp", | ||
"xfacet": "cpp", | ||
"xfunctional": "cpp", | ||
"xhash": "cpp", | ||
"xiosbase": "cpp", | ||
"xlocale": "cpp", | ||
"xlocbuf": "cpp", | ||
"xlocinfo": "cpp", | ||
"xlocmes": "cpp", | ||
"xlocmon": "cpp", | ||
"xlocnum": "cpp", | ||
"xloctime": "cpp", | ||
"xmemory": "cpp", | ||
"xmemory0": "cpp", | ||
"xstddef": "cpp", | ||
"xtr1common": "cpp", | ||
"xtree": "cpp", | ||
"xutility": "cpp" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Chapter 17. Specialized Library Facilities | ||
|
||
## Exercise 17.1 | ||
|
||
> Define a `tuple` that holds three `int` values and initialize the members to 10, 20, and 30 | ||
```cpp | ||
tuple<int, int, int> threeInt(10, 20, 30); | ||
``` | ||
## Exercise 17.2 | ||
> Define a `tuple` that holds a `string`, a `vector<string>`, and a `pair<string, int>`. | ||
```cpp | ||
tuple<std::string, std::vector<std::string>, std::pair<std::string, int> t; | ||
``` | ||
|
||
## Exercise 17.3 | ||
|
||
> Rewrite the `TextQuery` programs from 12.3 (p. 484) to use a `tuple` instead of the `QueryResult` class. | ||
Explain which design you think is better and why. | ||
|
||
[TextQuery Definition](ex17_03_text_query.h) | [TextQuery Implementation](ex17_03_text_query.cpp) | [TextQuery Test](ex17_03_text_query_test.cpp) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include "ex17_03_text_query.h" | ||
|
||
#include <algorithm> | ||
#include <cctype> | ||
#include <sstream> | ||
|
||
namespace EX03 { | ||
|
||
using std::get; | ||
|
||
TextQuery::TextQuery(ifstream& is) : file(new vector<string>) | ||
{ | ||
string txt; | ||
while (getline(is, txt)) { // for each line in the file | ||
file->push_back(txt); // remember this line of text | ||
int n = file->size() - 1; // the current line number | ||
// remove punctuation from the statement | ||
txt.erase(std::remove_if(txt.begin(), txt.end(), ::ispunct), txt.end()); | ||
std::istringstream line(txt); // separate the line into words | ||
string word; | ||
while (line >> word) { // for each word in that line. | ||
// if word isn't already in wm, subscripting adds a new entry | ||
auto& lines = wm[word]; // lines is a shared_ptr | ||
if (!lines) // that pointer is null the first time we see word | ||
lines.reset(new set<line_no>); // allocate the new | ||
lines->insert(n); // insert this line number | ||
} | ||
} | ||
} | ||
|
||
query_result TextQuery::query(const string& sought) const | ||
{ | ||
// we'll return a pointer to this set if we don't find sought | ||
static shared_ptr<set<line_no>> nodata(new set<line_no>); | ||
// use find and not a subscript to avoid adding words to wm! | ||
auto loc = wm.find(sought); | ||
if (loc == wm.end()) return std::make_tuple(sought, nodata, file); | ||
return std::make_tuple(sought, loc->second, file); | ||
} | ||
|
||
string make_plural(size_t ctr, string const& word, string const& ending) | ||
{ | ||
return (ctr > 1) ? word + ending : word; | ||
} | ||
|
||
ostream& print(ostream& os, const query_result& qr) | ||
{ | ||
// if the word was found, print the count and all occurrences. | ||
os << get<0>(qr) << " occurs " << get<1>(qr)->size() << " " | ||
<< make_plural(get<1>(qr)->size(), "time", "s") << std::endl; | ||
// print each line in which the word appeared | ||
for (auto num : *get<1>(qr)) // for every element in the set | ||
// don't confound the user with text lines starting at 0 | ||
os << "\t(line " << num + 1 << ") " << *(get<2>(qr)->begin() + num) | ||
<< std::endl; | ||
return os; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <fstream> | ||
#include <iostream> | ||
#include <map> | ||
#include <memory> | ||
#include <set> | ||
#include <string> | ||
#include <tuple> | ||
#include <vector> | ||
|
||
using std::string; | ||
using std::vector; | ||
using std::tuple; | ||
using std::shared_ptr; | ||
using std::set; | ||
using std::map; | ||
using std::ifstream; | ||
using std::ostream; | ||
|
||
namespace EX03 { | ||
using line_no = vector<string>::size_type; | ||
using query_result = | ||
tuple<string, shared_ptr<set<line_no>>, shared_ptr<vector<string>>>; | ||
|
||
class TextQuery { | ||
public: | ||
// read the input file and build the map of lines to line numbers. | ||
TextQuery(ifstream&); | ||
query_result query(const string&) const; | ||
|
||
private: | ||
shared_ptr<vector<string>> file; // input file | ||
// map of each word to the set of the lines in which that word appears. | ||
map<string, shared_ptr<set<line_no>>> wm; | ||
}; | ||
|
||
ostream& print(ostream&, const query_result&); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include "ex17_03_text_query.h" | ||
|
||
void runQueries(ifstream& infile) | ||
{ | ||
EX03::TextQuery tq(infile); | ||
while (true) { | ||
std::cout << "Enter word to look for, or q to quit: "; | ||
string s; | ||
if (!(std::cin >> s) || s == "q") break; | ||
EX03::print(std::cout, tq.query(s)) << std::endl; | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
ifstream file("../data/storyDataFile.txt"); | ||
runQueries(file); | ||
} |