检查字符串是否包含多个单词的最快/最小方法

时间:2017-05-01 14:08:40

标签: c++ string

我正在使用c ++ 11,并且可以使用正则表达式的东西。我想知道检查一个字符串是否包含多个单词的最快方法是什么,如果有的话多少。在这种情况下,单词被定义为由空格分隔的字符组。

我有几个选择:

  1. 按空格拆分字符串,计算拆分的长度
  2. 使用某种正则表达式
  3. 计算空白字符
  4. 选项1是最简单的方法,但考虑到多个空白字符会使分割更加复杂。 2可能更慢,我不知道如何计算它。 3是我能想到的最快,但可能有很多角落要考虑。我希望我的解决方案尽可能高效,并尽可能少地包含额外的库。这对我来说是一个可以解决的问题,但我需要更深入地了解最佳解决方案是什么。

    我倾向于第一个,但那么什么功能最好? istringstream加上迭代器,stringstream,一些char*魔法?我不确定最快的方法是什么。

2 个答案:

答案 0 :(得分:1)

我会迭代字符串,计算单词并迭代任何连续的空格。

  • 每当从空格移动到非空格时增加字数。
  • 如果字符串以非空格开头,则增加字数

    int countWords(string& toCount, const string& whitespace){
        enum countStatus {
            startOfString,
            initialWhitespace,
            movePastWord,
            movePastWhitespace
        } status=startOfString;    
    
        int wordCount=0;
    
        for(char& c : toCount) {
            bool characterIsWhitespace=false;
    
            if (whitespace.find(c)!=string::npos) {
                characterIsWhitespace=true;
            }
    
            switch(status) {
                case startOfString:
                    if (characterIsWhitespace) {
                        status=initialWhitespace;
                    } else {
                        status=movePastWord;
                        wordCount++;
                    }
                break;
    
                case initialWhitespace:
                    if (!characterIsWhitespace) {
                        wordCount++;
                        status=movePastWord;
                    }
                break;
    
                case movePastWord:
                    if (characterIsWhitespace) {
                        status=movePastWhitespace;
                    }
                break;
    
                case movePastWhitespace:
                    if (!characterIsWhitespace) {
                        wordCount++;
                        status=movePastWord;
                    }
            }
        }
    
        return wordCount;
    }
    

答案 1 :(得分:0)

在这种情况下,您可以使用关联容器。 C ++有多种选择。例如,您可以使用std::map。在以下代码中,您可以计算文本中出现多少个单词。

#include <iostream>
#include <string>
#include <map>
#include <algorithm>


int main()
{
    std::map<std::string,int> strCount;
    std::string str("AA BB ABC AA GE AAf FF JJ BB CC "); 
    std::string temp;

    // Split String Based on Whitespace (i.e. you need to modify it to suit the text format you have. )
    for ( int i(0); i < str.size(); ++i ){
        temp += str[i];
        if ( str[i] == ' ' ){
            temp.pop_back();
            ++strCount[temp]; // <-- if element new, insert it in map and associate new counter, otherwise increment counter of element. 
            temp.clear();
       }
    }


    std::map<std::string,int>::const_iterator iter;   
    for( iter = strCount.begin(); iter != strCount.end(); iter++ ) {
        std::cout << "#: " << iter->second << " string: " << iter->first << std::endl;
    }
    return 0;
}

前面代码的输出是

#: 2 string: AA
#: 1 string: AAf
#: 1 string: ABC
#: 2 string: BB
#: 1 string: CC
#: 1 string: FF
#: 1 string: GE
#: 1 string: JJ
相关问题