Skip to content

Commit

Permalink
Update ex11_14.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Mooophy committed Oct 23, 2015
1 parent 0ef39ff commit 8377ecd
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions ch11/ex11_14.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//
// @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
Expand All @@ -15,8 +17,15 @@
#include <string>
#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;
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;

class Families
{
Expand All @@ -25,24 +34,21 @@ class Families
using Children = vector<Child>;
using Data = map<string, Children>;

auto add(string const& last_name, string const& first_name, string birthday) -> void
auto add(string const& last_name, string const& first_name, string birthday)
{
_data[last_name].push_back(make_pair(first_name, birthday));
auto child = make_pair(first_name, birthday);
_data[last_name].push_back(child);
}

auto print(std::ostream& os) const -> ostream&
auto print() const
{
if (_data.empty())
return os << "No data right now." << endl;

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

private:
Expand All @@ -51,10 +57,10 @@ class Families

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);
Families families;
auto msg = "Please enter last name, first name and birthday:\n";
for (string l, f, b; cout << msg, cin >> l >> f >> b; families.add(l, f, b));
families.print();

return 0;
}

0 comments on commit 8377ecd

Please sign in to comment.