-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflyweight_coding_exercise_9.cpp
More file actions
56 lines (49 loc) · 1.13 KB
/
Copy pathflyweight_coding_exercise_9.cpp
File metadata and controls
56 lines (49 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
using namespace std;
struct Sentence
{
struct WordToken
{
bool capitalize;
};
vector<string> words;
map<int, WordToken> tokens;
Sentence(const string& text)
{
istringstream iss{text};
words = vector<string>(istream_iterator<string>{iss},
istream_iterator<string>{});
}
WordToken& operator[](size_t index)
{
tokens[index] = WordToken{};
return tokens[index];
}
string str() const
{
vector<string> my_words;
for (size_t i = 0; i < words.size(); ++i)
{
string w = words[i];
auto t = tokens.find(i);
if (t != tokens.end() && t->second.capitalize)
{
// note: the annotation on ::toupper() below is only required
// for GCC; other compilers work fine without it
transform(w.begin(), w.end(), w.begin(), toupper);
}
my_words.push_back(w);
}
ostringstream oss;
for (size_t i = 0; i < ws.size(); ++i)
{
oss << my_words[i];
if (i+1 != my_words.size()) oss << " ";
}
return oss.str();
}
};