Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ equivalent to Python's "if x in [string1, string2, …]"

Tags:

c++

string

My mini project is to make a chatbot; I have not google open sources, nor have I researched how to build. I'm trying this as to see how well I understand C++: Saying that;

I'm trying to make a "Box" of sort in which holds all "questions" that can be given and seeing "if" that "question" is "in" the "Box" it'll execute said code.

In Python it would be more or less:

Box = ["Yes", "YES", "yes", "yEs", "YeS", "yES"]

print "Will you be working today?"
response = raw_input("> ")
if response in Box:
    print "Very well, then how can I assist you?"

So how would I go about doing so in C++. Or what is it called in C++? An array? A list? Vector? It's a bit confusing to differentiate those in C++.

like image 669
Azhorabai Avatar asked Dec 06 '22 21:12

Azhorabai


1 Answers

For this I would consider converting the response to all lower-case then doing a straight comparison:

#include <string>
#include <cctype>
#include <functional>
#include <algorithm>

// convert string to lowercase
std::string lower_case(std::string s)
{
    std::transform(s.begin(), s.end(), s.begin()
        , std::ptr_fun<int, int>(std::tolower));
    return s;
}

int main()
{
    std::string response;

    // ask question and get response

    // now you don't need to look at every combination
    if(lower_case(response) == "yes")
    {
        // positive response code
    }

    // etc...
}
like image 107
Galik Avatar answered Dec 22 '22 03:12

Galik