11.4
#include<iostream>
#include<fstream>
#include<map>
#include<set>
#include<string>
#include<iterator>
#include<algorithm>
using namespace std;
int main(int argc, char* argv[])
{
map<string, size_t> word_count;
set<string> exclude = { "the", "but", "and", "or", "an", "a" };
string word;
while (cin >> word)
{
*word.begin() = tolower(*word.begin());
if (ispunct(*word.rbegin()))
word.pop_back();
if (exclude.find(word) == exclude.end())
++word_count[word];
}
for (const auto& w : word_count)
cout << w.first << " occurs " << w.second << (w.second > 1 ? " times." : " time.") << endl;
return 0;
}
11.20
#include<iostream>
#include<fstream>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<string>
#include<cctype>
#include<utility>
#include<algorithm>
using namespace std;
int main(int argc, char* argv[])
{
map<string, size_t> word_count;
set<string> exclude = { "the", "but", "and", "or", "an", "a" };
string word;
while (cin >> word)
{
*word.begin() = tolower(*word.begin());
if (ispunct(*word.rbegin()))
word.pop_back();
if (exclude.find(word) == exclude.end()) {
//auto ret = word_count.insert({ word, 1 });
//if (ret.second)
// ++ret.first->second;
++word_count.insert({ word,0 }).first->second; //与上等价
}
}
for (const auto& w : word_count)
cout << w.first << " occurs " << w.second << (w.second > 1 ? " times." : " time.") << endl;
return 0;
}
11.31 & 11.32
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<utility>
#include<algorithm>
using namespace std;
inline void print(multimap<string, string> cata)
{
for (auto m : cata)
cout << m.first << " " << m.second << endl;
}
int main(int argc, char* argv[])
{
multimap<string, string> cata = {
{"dzx","game"},
{"zxg","love"},
{"fly","friend"},
{"dzx","code"},
{"dzx","student"},
};
print(cata);
cout << endl;
string find_member = "zx";
if (cata.find(find_member) != cata.end()) {
cata.erase(find_member);
cout << "Delete complete." << endl;
}
else
cout << "I can't find." << endl;
print(cata);
return 0;
}
11.33
#include<iostream>
#include<fstream>
#include<sstream>
#include<map>
#include<string>
using namespace std;
map<string, string> buildMap(ifstream& map_file)
{
map<string, string> trans_map;
string key;
string value;
while (map_file >> key && getline(map_file, value))
if (value.size() > 1)
trans_map[key] = value.substr(1);
//trans_map.insert({ key, value.substr(1) });
else
throw runtime_error("no rule for " + key);
return trans_map;
}
const string&
transform(const string& s, const map<string, string>& m)
{
auto map_it = m.find(s);
if (map_it != m.cend())
return map_it->second;
else return s;
}
void word_transform(ifstream& map_file, istream& input)
{
auto trans_map = buildMap(map_file);
string text;
while (getline(input, text)) {
istringstream stream(text);
string word;
bool firstword = true;
while (stream >> word) {
if (firstword)
firstword = false;
else
cout << " ";
cout << transform(word, trans_map) << endl;
}
}
}
int main()
{
ifstream fin("1.txt");
word_transform(fin, cin);
return 0;
}
1.txt:
brb be right back
k okay?
y why
r are
u you
pic picture
thk thanks!
18r later