mpaymetov / oop-labs

Задания по объектно-ориентированному программированию
0 stars 0 forks source link

Замечания по dictionary #7

Open alexey-malov opened 6 years ago

alexey-malov commented 6 years ago
alexey-malov commented 6 years ago
alexey-malov commented 6 years ago

Проще всего покрыть запись и сохранение так: сохранить непустой словарь в stringstream, а потом загрузить и сравнить с сохранённым.

alexey-malov commented 6 years ago
alexey-malov commented 6 years ago
typedef std::multimap<std::string, std::string> DictionaryType;
alexey-malov commented 6 years ago
alexey-malov commented 5 years ago
alexey-malov commented 5 years ago
bool FindInDictionary(Dictionary& dictionary, const std::string& originalWord, std::string& translation)
{
    translation = "";
    for (DictionaryMap::iterator it = dictionary.forward.equal_range(originalWord).first; it != dictionary.forward.equal_range(originalWord).second; ++it)
    {
        translation += it->second + " ";
    }
    for (DictionaryMap::iterator it = dictionary.reverse.equal_range(originalWord).first; it != dictionary.reverse.equal_range(originalWord).second; ++it)
    {
        translation += it->second + " ";
    }

    if (translation == "")
    {
        return false;
    }
    return true;
}
alexey-malov commented 5 years ago
void ReadDictionaryFromStream(std::istream& input, Dictionary& dictionary)
{
    std::string word, translation;
    while (!input.eof())
    {
        std::getline(input, word);
        if (!input.eof())
        {
            std::getline(input, translation);
            InsertToDictionary(dictionary, word, translation);
        }
    }
    return;
}
void ReadDictionaryFromFile(const std::string& inputFileName, Dictionary& dictionary)
alexey-malov commented 5 years ago
void SaveDictionaryToStream(std::ostream& output, Dictionary& dictionary)
{
    for (DictionaryMap::iterator it = dictionary.forward.begin(); it != dictionary.forward.end(); ++it)
    {
        output << (*it).first << std::endl << (*it).second << std::endl;
    }
}
alexey-malov commented 5 years ago
alexey-malov commented 5 years ago
alexey-malov commented 5 years ago

Если выставить в настройках проекта поддержку C++ 17, то можно вот так извлекать значения из pair

void SaveDictionaryToStream(std::ostream& output, const Dictionary& dictionary)
{
    for (const auto& [word, translation] : dictionary.forward)
    {
        output << word << std::endl << translation << std::endl;
    }
}