微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

函数将每个单词从字符串中分离出来并将它们放入向量中,而不使用 auto 关键字?

如何解决函数将每个单词从字符串中分离出来并将它们放入向量中,而不使用 auto 关键字?

我真的被困在这里了。所以我不能编辑主函数,在它里面有一个函数调用,唯一的参数是字符串。如何让这个函数将字符串中的每个单词放入一个向量中,而不使用 auto 关键字?我意识到这段代码可能真的是错误的,但这是我对它应该是什么样子的最好尝试。

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

vector<string> extract_words(const char * sentence[])
{
   string word = "";
   vector<string> list;
   for (int i = 0; i < sentence.size(); ++i)
   {
      while (sentence[i] != ' ')
      {
         word = word + sentence[i];
      }
      list.push_back(word);
   }
}

int main()
{
    sentence = "Help me please" /*In the actual code a function call is here that gets input sentence.*/
    if (sentence.length() > 0)
    {
        words = extract_words(sentence);
    }
}

解决方法

你知道如何读std::cin中的“单词”吗?

然后您可以将该字符串放入 std::istringstream 中,它的工作方式类似于 std::cin,但用于“读取”字符串。

在循环中使用流提取运算符 >> 将所有单词一一获取,并将它们添加到向量中。

也许是这样的:

std::vector<std::string> get_all_words(std::string const& string)
{
    std::vector<std::string> words;

    std::istringstream in(string);
    std::string word;
    while (in >> word)
    {
        words.push_back(word);
    }

    return words;
}

对 C++ 及其标准类和函数有更多的了解,实际上可以使函数更短:

std::vector<std::string> get_all_words(std::string const& string)
{
    std::istringstream in(string);

    return std::vector<std::string>(std::istream_iterator<std::string>(in),std::istream_iterator<std::string>());
}
,

我建议将函数的参数设为 const std::string& 而不是 const char * sentence[]std::string 具有 many member functions,例如 find_first_offind_first_not_ofsubstr 等,它们可以提供很多帮助。

以下是使用上述内容的示例:

std::vector<std::string> extract_words(const std::string& sentence)
{
    /*  Control char's,"whitespaces",that we don't want in our words:
    \a  audible bell
    \b  backspace
    \f  form feed
    \n  line feed
    \r  carriage return
    \t  horizontal tab
    \v  vertical tab
    */
    static const char whitespaces[] = " \t\n\r\a\b\f\v";
    std::vector<std::string> list;
    std::size_t begin = 0;

    while(true)
    {
        // Skip whitespaces by finding the first non-whitespace,starting at
        // "begin":
        begin = sentence.find_first_not_of(whitespaces,begin);

        // If no non-whitespace char was found,break out:
        if(begin == std::string::npos) break;

        // Search for a whitespace starting at "begin + 1":
        std::size_t end = sentence.find_first_of(whitespaces,begin + 1);

        // Store the result by creating a substring from "begin" with the
        // length "end - begin":
        list.push_back(sentence.substr(begin,end - begin));

        // If no whitespace was found,break out:
        if(end == std::string::npos) break;

        // Set "begin" to the char after the found whitespace before the loop
        // makes another lap:
        begin = end + 1;    
    }

    return list;
}

Demo

添加限制“no breaks”,这可能是一个变体。它与上面的完全相同,但不使用 break:

std::vector<std::string> extract_words(const std::string& sentence)
{
    static const char whitespaces[] = " \t\n\r\a\b\f\v";
    std::vector<std::string> list;
    std::size_t begin = 0;
    bool loop = true;
    while(loop)
    {
        begin = sentence.find_first_not_of(whitespaces,begin);
        if(begin == std::string::npos) {
            loop = false;
        } else {
            std::size_t end = sentence.find_first_of(whitespaces,begin + 1);
            list.push_back(sentence.substr(begin,end - begin));
            if(end == std::string::npos) {
                loop = false;
            } else {
                begin = end + 1;
            }
        }
    }

    return list;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。