forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ex11.31 should find an element rather than key.
- Loading branch information
Showing
2 changed files
with
31 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |