【问题标题】:How could I implement a word skipping solution into my program?如何在我的程序中实施跳字解决方案?
【发布时间】:2019-11-05 06:19:34
【问题描述】:

我希望我的程序在用户命令中包含以下单词时忽略以下单词:“the”“with”“on”。

目前该程序只接受两个单词作为命令的动词和名词字符串。

string one = "the";
string two = "with";
string three = "on";
string four = "in";

int main()
{
    string noun = "";
    string verb = "";


    getline(cin, verb);
    int x = verb.find(' ');

    if (x > -1) {
        noun = verb.substr(verb.find(" ") + 1);
        verb = verb.substr(0, verb.find(" "));
    }

    if (verb == "living") {
        if (noun == "room") {
            Livingroom();
        }
    }
    else if (verb == "bed") {
        if (noun == "room") {
            Bedroom();
        }
    }


}

我希望程序能够正常工作,即使在用户命令中包含前面提到的单词。

【问题讨论】:

  • 为什么不将整行读入std::string(比如std::string line;)然后从line创建一个std::stringstream(比如std::stringstring ss(line);),然后循环while (ss >> word) { ... }并在循环检查单词以排除并在找到时忽略它们?最好使用std::vector<std::string> 来保存要排除的单词,这将使它们易于检查和忽略。
  • 将您的排除词放入std::setstd::unordered_set
  • @paddy, std::set 非常适合。甚至比std::vector<std::string> 上的基于范围的循环更短,以匹配排除词。 std::set

标签: c++ string function


【解决方案1】:

Sam,虽然不清楚您所期望的 noun/verb 输入顺序究竟是什么,但您可以使用其中一个标准容器来保存您想要排除的单词(以及一个特殊单词列表交换顺序)。

您已经在使用std::string。虽然我的第一个想法是std::vector<std::string> 用于排除,但@paddy 提出了一个很好的建议,可以使用std::unordered_set(或std::set)进一步简化事情。区别在于底层数据结构,其中std::unordered_set 的常数时间复杂度比排序的std::set 稍好(参见std::setstd::unordered_set 相比。

使用std::unordered 集合排除不需要的单词,您可以简单地循环使用.find(word) 对当前单词的命令输入,并与unordered_set 的.end() 元素进行比较以确定word 是否是在排除集中。

一个简短的例子可以是:

#include <iostream>
#include <string>
#include <array>
#include <unordered_set>

int main (void) {

    /* unordered set of words to exclude from the command */
    std::unordered_set<std::string> excl = { "in", "on", "the", "with" };
    std::string word;                       /* current word read from input */
    std::array<std::string, 2> command;     /* 2 string array (noun/verb) */
    size_t n = 0;                           /* simple counter */

    std::cout << "enter command: ";

    /* read words in command until the 1st two non-excluded words found */
    while (n < command.size() && std::cin >> word) {
        auto search = excl.find(word);      /* search for word in set */
        if (search == excl.end())           /* if word not excluded */
            command.at(n++) = word;         /* add to array, increment count */
    }

    /* output first two non-excluded words */
    std::cout << "\nnoun: " << command.at(0) << 
                "\nverb: " << command.at(1) << '\n';
}

(注意:特殊词的实现,如另一组中的"living""bed"由你决定,是否交换名词/动词的顺序以满足你的需要)

使用/输出示例

$ ./bin/set_noun_verb
enter command: in the bed in the room with the candlestick

noun: bed
verb: room

把事情看一遍。有很多方法可以实现这一点,但在std::unordered_set 的建议之后,这可能是从考虑中排除一组单词的最简单方法之一。您也可以为名词/动词command 使用您选择的容器。 std::array&lt;std::string, 2&gt; 有意义,但 std::vectorstd::pair 或您最终可能决定使用的任何其他容器也是如此。

【讨论】:

  • 使用 std::unordered_set 实现的解决方案效果很好。非常感谢。
  • 很高兴有帮助。给其他答案留点时间,你可能会得到一个你更喜欢的答案,然后看看What should I do when someone answers my question?什么是合理的时间量? 1/2 天,一天,由你决定。
猜你喜欢
  • 1970-01-01
  • 2015-04-01
  • 2020-10-03
  • 1970-01-01
  • 2012-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
相关资源
最近更新 更多