Skip to content

Commit

Permalink
merge upstream about 11.14
Browse files Browse the repository at this point in the history
  • Loading branch information
Mooophy authored and pezy committed Jun 24, 2015
1 parent a0ba3f2 commit e51df91
Showing 1 changed file with 31 additions and 37 deletions.
68 changes: 31 additions & 37 deletions ch11/ex11_14.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
//! @Alan
//! @Yue Wang
//! Exercise 11.14:
//! Extend the map of children to their family name that you wrote for the
//! exercises in § 11.2.1 (p. 424) by having the vector store a pair that
//! holds a child’s name and birthday.
//!
//! @Alan
//!
//! Exercise 11.7:
//! Define a map for which the key is the family’s last name and
//! the value is a vector of the children’s names. Write code to
Expand All @@ -15,52 +13,48 @@
#include <iostream>
#include <map>
#include <string>
#include <algorithm>

#include <vector>

using std::ostream; using std::cout; using std::cin; using std::endl; using std::string;
using std::make_pair; using std::pair; using std::vector; using std::map;

int main()
class Families
{
//! define a map as required.
std::map<std::string, std::vector<std::pair<std::string,std::string>>>
famlies_map;
//! declare three strings to store the input
std::string lastName, childName, birthday;
public:
using Child = pair<string, string>;
using Children = vector<Child>;
using Data = map<string, Children>;

while([&](){//! a lambda to read lastName and check if should quit

std::cout << "last name:\n";
std::cin >> lastName;

return lastName != "@q";
}())
void add(string const& last_name, string const& first_name, string birthday)
{
while([&](){//! a lambda to read child name and birthday and check if should quit
std::cout << "child's name:\n";
std::cin >> childName;
std::cout << "his birthday:\n";
std::cin >> birthday;

return childName != "@q" && birthday != "@q";
}())
{
famlies_map[lastName].push_back({childName, birthday});
//! ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^
//! use lastName as the key create a pair using {}.
}
_data[last_name].push_back(make_pair(first_name, birthday));
}

//! print the content.
for(const auto &e : famlies_map)
ostream& print(std::ostream& os) const
{
std::cout << e.first <<":\n";
if (_data.empty())
return os << "No data right now." << endl;

for (const auto &l : e.second)
for (const auto& pair : _data)
{
std::cout << l.first << " "
<< l.second << " ";
os << pair.first << ":\n" ;
for (const auto& child : pair.second)
os << child.first << " " << child.second << endl;
os << endl;
}
return os;
}

private:
Data _data;
};

int main()
{
Families families;
string message = "Please enter last name, first name and birthday";
for (string l, f, b; cout << message << endl, cin >> l >> f >> b; families.add(l, f, b));
families.print(cout << "Current data:" << endl);

return 0;
}

0 comments on commit e51df91

Please sign in to comment.