switch/case statement in C++ with a QString type

后端 未结 14 2075
南笙
南笙 2020-12-16 10:19

I want to use switch-case in my program but the compiler gives me this error:

switch expression of type \'QString\' is illegal

How can I us

14条回答
  •  佛祖请我去吃肉
    2020-12-16 10:34

    As previously noted this is not a Qt problem, switch statements can only use constant expressions, look at the collection classes a QSet is a good solution

    void initStopQwords(QSet& stopSet)
    {
        // Ideally you want to read these from a file
        stopSet << "the";
        stopSet << "at";
        ...
    
    }
    
    bool isStopWord(const QSet& stopSet, const QString& word)
    {
        return stopSet.contains(word);
    }
    

提交回复
热议问题