Skip to content

Commit

Permalink
fixed pezy#121, thx @why2cs.
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed May 14, 2017
1 parent 5174500 commit 49291a5
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions ch11/ex11_31.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,28 @@
// Be sure your program works correctly if the element you look for is not in
// the map.

#include <iostream>
#include <map>
#include <string>
#include <iostream>

using std::string;

int main()
{
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].
{"wang", "FTP"}, {"pezy", "CP5"}, {"wang", "CPP-Concurrency"},
{"pezy", "CP5"}};
// want to delete an element that author is [pezy], work is [CP5].
string author = "pezy";
string work = "CP5";

auto found = authors.find(author);
auto count = authors.count(author);
while (count) {
if (found->second == work) {
authors.erase(found);
break;
}
++found;
--count;
for (auto found = authors.find(author);
found != authors.end() && found->first == author;) {
if (found->second == work)
found = authors.erase(found);
else
++found;
}

for (const auto& author : authors)
Expand Down

0 comments on commit 49291a5

Please sign in to comment.