1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
// C21Drill Map.cpp
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <iomanip>
// Write a function that reads value pairs from cin and places them in msi.
std::map< std::string, int > read_map(std::istream& stm = std::cin);
int main()
{
// Define a map<string,int> called msi and insert ten pairs into it.
std::map<std::string, int> msi = {
{ "lecture", 21 },
{ "matinee", 53 },
{ "musical", 24 },
{ "broadway", 66 },
{ "seminar", 12 },
{ "panel", 37 },
{ "adhoc", 79 },
{ "jury", 81 },
{ "forum", 97 },
{ "committee", 45 }
};
// Output the (name,value) pairs into it
for (const auto& p : msi)
std::cout << std::setw(10) << p.first << std::setw(5) << p.second << '\n';
// Erase the (name, value) pairs from msi
msi.clear();
// Read ten pairs from input and enter them into msi
for (auto p : read_map()) std::cout << p.first << ' ' << p.second << '\n';
// looks like read_map() needs a parameter
}
std::map< std::string, int > read_map(std::istream& stm = std::cin)
{
std::map< std::string, int > map;
std::string key;
int value;
// key does not contain white space
while (stm >> key >> value) map.emplace(key, value);
return map;
}
|