SHARE
TWEET

Untitled

a guest Apr 25th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. #include <set>
  5. #include <algorithm>
  6.  
  7. std::map<std::string, std::string> parseString(const std::string& string) {
  8.     std::map<std::string, std::string> pairedWordsFromString;
  9.     size_t currentPosition = 0, prevPosition = 0;
  10.  
  11.     currentPosition = string.find(' ', prevPosition);
  12.     std::string englishWord(string, prevPosition, currentPosition - prevPosition);
  13.  
  14.     currentPosition += 3;
  15.     prevPosition = currentPosition;
  16.  
  17.     while (currentPosition < string.size()) {
  18.         currentPosition = string.find(',', prevPosition);
  19.         if (currentPosition == std::string::npos) {
  20.             currentPosition = string.size();
  21.         }
  22.         std::string latinWord(string, prevPosition, currentPosition - prevPosition);
  23.         pairedWordsFromString[latinWord] = englishWord;
  24.  
  25.         currentPosition += 2;
  26.         prevPosition = currentPosition;
  27.     }
  28.  
  29.     return pairedWordsFromString;
  30. }
  31.  
  32.  
  33. class LatinEnglishDict {
  34.     std::map<std::string, std::set<std::string>> latinEnglishDict_;
  35.  
  36.     void AddWord(const std::pair<std::string, std::string>& wordsPair) {
  37.         latinEnglishDict_[wordsPair.first].insert(wordsPair.second);
  38.     }
  39.  
  40.     void AddMapOfWords(const std::map<std::string, std::string>& wordsMap) {
  41.         for (const auto& wordsPair : wordsMap) {
  42.             this->AddWord(wordsPair);
  43.         }
  44.     }
  45.  
  46. public:
  47.  
  48.     void FillDict() {
  49.         std::string string;
  50.         while (getline(std::cin, string)) {
  51.             this->AddMapOfWords(parseString(string));
  52.         }
  53.     }
  54.  
  55.     void PrintDict() {
  56.         std::cout << latinEnglishDict_.size() << '\n';
  57.         for (const auto& latinWord : latinEnglishDict_) {
  58.             std::cout << latinWord.first << " - ";
  59.             for (auto englishWord = latinWord.second.begin(); englishWord != latinWord.second.end(); ++englishWord) {
  60.                 if (englishWord != --latinWord.second.end()) {
  61.                     std::cout << *englishWord << ", ";
  62.                 } else {
  63.                     std::cout << *englishWord << '\n';
  64.                 }
  65.             }
  66.         }
  67.     }
  68.  
  69. };
  70.  
  71.  
  72. int main() {
  73.     LatinEnglishDict dictionary;
  74.     dictionary.FillDict();
  75.     dictionary.PrintDict();
  76.     return 0;
  77. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top