Search code examples
c++stringtranslators

How to find if a string is equal to another from a list in C++


I'm making a Latin translator and I need some help. So the way I've done this is I've separated each word from a phrase the user inputs into separate strings. I also have a class where i just list all of the Latin words and its translation for example: string ac = "and"; and string accedo = "approach";

I want a way to check for example the first word to all the words from the list to find what the translation is, without adding an if statement for every word.


Solution

  • You can use a std::map like this

    #include <map>
    #include <iostream>
    
    int main()
    {
        std::map<std::string, std::string> words;
    
        words["ac"] = "and";
        words["acedo"] = "approach";
    
    
        std::cout << "ac = " << words["ac"] << '\n';
        std::cout << "acedo = " << words["acedo"] << '\n';
    }