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
#include <iostream> #include <string> #include <map> #include <regex> using namespace std; int main() { // 入力形式の伝達 cout << "入力形式 項目: 内容[,内容...]" << endl << endl; map<string, string> intro; string koumoku, naiyou; string s; regex rx_input("[^:]+:[^,:]+(,[^,:]+)*"), rx_koumoku("^\\s+|\\s*:.+$"), rx_naiyou("[,:]\\s*|\\s+$"), rx_iter("[,:][^,:]+"); // 入力の受け取り while (true) { cout << "入力 > "; getline(cin, s); if (s.empty()) break; if (!regex_match(s, rx_input)) { cout << "入力形式に誤りがあります。" << endl; continue; } koumoku = regex_replace(s, rx_koumoku, ""); sregex_iterator iter(s.begin(), s.end(), rx_iter), last; for (; iter != last; iter++) naiyou += "[" + regex_replace(iter->str(), rx_naiyou, "") + "]"; intro[koumoku] = naiyou; naiyou.clear(); } // 表示 cout << endl; for (map<string, string>::iterator i = intro.begin(); i != intro.end(); i++) cout << "[" << i->first << "]: " << i->second << endl; }