解析一个单词的字符串

时间:2014-11-15 00:51:03

标签: c++

我想创建一个解析字符串的程序,并在找到字符串中的单词时执行某些操作。这就是我得到的:

size_t strPos;
string str = "";

switch(str.find(str, strPos)
{
   case 'APPLE':
   cout << "You are eating an apple!" << endl;
   break;

   case 'ORANGE':
   cout << "You are eating an orange!" << endl;
   break;

   default:
   cout << "You're eating something... that's for sure!" << endl;
   break;

   // etc...
};

问题是此代码运行但不起作用。如何解析字符串并搜索上面的单词?我认为使用switch语句不是正确的使用方法。

5 个答案:

答案 0 :(得分:2)

有几个问题。

  1. 在C ++中,您不能在字符串上使用开关。

  2. 搜索的开始位置(strPos)在代码中未初始化

  3. 即使它是0,

    str.find(str, strPos)
    

    返回字符串的起始位置(= 0)参见string::find

  4. &#39; APPLE&#39;不是一个字符串,&#34; APPLE&#34;会是。

  5. 正如我在输入此答案时的评论中所述,请使用

        if (str == "APPLE")
        {...}
        else if (str == "ORANGE")
        {...}
        else
        {...}
    

    编辑:

    1. 正如Ken White在评论中写道,str总是初始化为空字符串。

答案 1 :(得分:1)

除了一些语法错误之外,您的代码还存在一些问题。

在C ++中,您不能在switch语句中使用字符串文字。

case 'APPLE':

由单引号分隔的文字常量是多字符文字。这将创建一个int,其中字符值以实现定义的方式合并为单个值。

要在C ++中进行任何类型的模式匹配,我建议使用std::regex

using namespace std;

regex r0{"[a-zA-Z]*"};

string str = "APPLE ORANGE PEAR";

sregex_iterator ritr{str.begin(), str.end(), r0};
sregex_iterator rend;

while(ritr != rend){
    auto s = ritr->str();
    if(s == "APPLE"){
        cout << "I found an apple!" << endl;
    }
    if(s == "ORANGE"){
        cout << "I found an orange!" << endl;
    }
    if(s == "PEAR"){
        cout << "I found a pear!" << endl;
    }
    ++ritr;
}

你可以使用std::map将匹配的字符串映射到任何东西(仿函数会很好)来扩展它。

答案 2 :(得分:0)

您必须使用查找表std::mapif-else if-else阶梯。

每个数据结构取决于您要与字符串关联的内容:

  • 查找表 - 函数或数据。如果功能,他们必须有 相同的签名。
  • std :: map - 函数或数据。如果是功能,它们必须具有相同的功能 签名。
  • If-else阶梯 - 非常灵活。

查找表

这涉及创建条目或记录结构并搜索“键”值。找到“密钥”后,您可以访问函数指针或与密钥关联的数据。

struct  Lookup_Entry
{
  std::string item;    // The "key"
  std::string phrase;  // The "value"
};
Lookup_Entry lookup_table[] =
{
  { "APPLE", "You ate an apple!\n"},
  { "ORANGE", "You ate an orange!\n"},
  { "ROCK", "You should not eat rocks.\n"},
};

使用std::map

std::map< std::string, std::string> lookup_table;
lookup_table["APPLE"] = "You are eating an apple.\n";
lookup_table["ORANGE"] = "You are eating an orange.\n";
lookup_table["ROCK"] = "You broke some teeth eating the rock.\n";

if-else if-else ladder

if (text == "APPLE"}
{
  cout << "You ate an apple.\n";
}
else if (text == "ORANGE")
{
  cout << "You ate a yummy orange.\n";
}
else
{
  cout << "You ate something unknown, hope you don't get sick.\n";
}

答案 3 :(得分:0)

如果我理解你的问题,你需要这样的东西:

#include <iostream>
#include <sstream>
using namespace std;

int main() {
    string text = "APPLE enter your source code or insert template or sample or your template";
    stringstream ss; // we need this for easy way to parse the input text
    ss.str(text);
    for (string word; ss >> word;) {
        cout << word << endl; // or do what ever you want with word
        if (word == "APPLE") {
            cout << "found an APPLE" << endl;
        } else if (word == "ORANGE") {
            //...
        }
    }
    return 0;
}

您可以在http://ideone.com/Rsr9ya

播放此代码

答案 4 :(得分:0)

您可以使用regex进行复杂的模式匹配:

string s = "An apple a day keeps the doctor away and may be oranges too"; 

regex what("APPLE|ORANGE|etc", regex_constants::icase);  // what to search for
smatch sm;       
while (regex_search(s, sm, what)) {   // loop through found occurences 
    cout << "Found: " << sm.str() << endl;
    s = sm.suffix().str();
}

这将打印:

found apple
found orange

当然,代替cout,您可以拥有if条款:

if (sm.str()=="apple") {
    //...
} else if (sm.str()=="orange") {
    //...
} 

如果您更喜欢switch,那么您可以创建一个map<string, int> m;来将您要查找的每个字符串与int相关联,然后您可以使用此int一个switch(m[sm.str()])