Skip to content

Commit

Permalink
ex11.31 should find an element rather than key.
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Dec 17, 2014
1 parent c2dbf6a commit c37e0f0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 52 deletions.
3 changes: 3 additions & 0 deletions ch11/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,6 @@ std::pair<std::map<std::string, std::vector<int>>::iterator, bool> // return
```

## [Exercise 11.23](ex11_23.cpp)
## [Exercise 11.24 ~ 11.26](ex11_24_25_26.cpp)
## [Exercise 11.27 ~ 11.30](ex11_27_28_29_30.cpp)
## [Exercise 11.31](ex11_31.cpp)
80 changes: 28 additions & 52 deletions ch11/ex11_31.cpp
Original file line number Diff line number Diff line change
@@ -1,59 +1,35 @@
//! @Alan
//!
//! Exercise 11.31:
//! Write a program that defines a multimap of authors and their works.
//! Use find to find an element in the multimap and erase that element.
// ^^^^ ^^^^^^^^^^
//! Be sure your program works correctly if the element you look for is
//! not in the map.
//!
//
// ex11_31.cpp
// Exercise 11.31
//
// Created by pezy on 12/17/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// Write a program that defines a multimap of authors and their works.
// Use **find** to find **an element** in the multimap and erase that element.
// Be sure your program works correctly if the element you look for is not in the map.

#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>

//!
//! \brief remove_works for ex 11.31
//! \param s the key value to be removed
//! \param m the multi map to be operated on
//!
//! this funciton just erase the first matching element.
void
remove_works(const std::string &s, std::multimap<std::string, std::string> &m);
using std::string;

int main()
{
//! define the multimap
std::multimap<std::string, std::string> m = {{"Alan","111"},{"Alan","112"},{"Alan","113"},{"Wang","222"}};

//! print the content
for(const auto &e : m)
std::cout << e.first << " " << e.second <<"\n";

std::cout <<"====================\n";

//! call the function to do as required.
remove_works("Alan",m);

//! print the content
for(const auto &e : m)
std::cout << e.first << " " << e.second <<"\n";

return 0;
}


void
remove_works(const std::string &s, std::multimap<std::string, std::string> &m)
{
std::multimap<std::string, std::string>::iterator it = m.find(s);
//! ^^^^
//!If successful the function returns an iterator pointing to the sought after %pair.
//!If unsuccessful it returns the past-the-end ( @c end() ) iterator.

if(it != m.end()) m.erase(it);
//! ^^
//! @param __position An iterator pointing to the element to be erased.
}
std::multimap<string, string> map = {{"Alan","111"},{"Alan","112"},{"Alan","113"},{"Wang","222"}};
// want to delete an element that author is [Alan], work is [112].
string author = "Alan";
string work = "112";

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

for (const auto &e : map)
std::cout << e.first << " " << e.second << std::endl;
}

0 comments on commit c37e0f0

Please sign in to comment.