Skip to content

Commit

Permalink
better name for 31,32
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Dec 18, 2014
1 parent 14a9b1e commit fcfc3d9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
28 changes: 19 additions & 9 deletions ch11/ex11_31.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,29 @@ using std::string;

int main()
{
std::multimap<string, string> map = {{"Alan","111"},{"Alan","112"},{"Alan","113"},{"Wang","222"}};
std::multimap<string, string> authors{
{"alan", "DMA"},
{"pezy", "LeetCode"},
{"alan", "CLRS"},
{"wang", "FTP"},
{"pezy", "CP5"},
{"wang", "CPP-Concurrency"}
};
// want to delete an element that author is [Alan], work is [112].
string author = "Alan";
string work = "112";
string author = "pezy";
string work = "CP5";

auto found = map.find(author);
auto count = map.count(author);
auto found = authors.find(author);
auto count = authors.count(author);
while (count) {
if (found->second == work) map.erase(found);
if (found->second == work) {
authors.erase(found);
break;
}
++found;
--count;
}

for (const auto &e : map)
std::cout << e.first << " " << e.second << std::endl;
}
for (const auto &author : authors)
std::cout << author.first << " " << author.second << std::endl;
}
16 changes: 8 additions & 8 deletions ch11/ex11_32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ using std::string;

int main()
{
std::multimap<string, string> map{
std::multimap<string, string> authors{
{"alan", "DMA"},
{"pezy", "LeetCode"},
{"alan", "CLRS"},
{"wang", "FTP"},
{"pezy", "CP5"},
{"wang", "CPP-Concurrency"}
};
std::map<string, std::multiset<string>> ordermap;
for (const auto &e : map)
ordermap[e.first].insert(e.second);
for (const auto &e : ordermap) {
std::cout << e.first << ": ";
for (const auto &work : e.second)
std::map<string, std::multiset<string>> order_authors;
for (const auto &author : authors)
order_authors[author.first].insert(author.second);
for (const auto &author : order_authors) {
std::cout << author.first << ": ";
for (const auto &work : author.second)
std::cout << work << " ";
std::cout << std::endl;
}
}
}

0 comments on commit fcfc3d9

Please sign in to comment.