【问题标题】:Splitting a string from a character从字符中拆分字符串
【发布时间】:2023-04-02 05:49:01
【问题描述】:

我正在尝试编写一个程序,将字符串拆分为二维数组token[100][100]。它将整个字符串拆分为单独的单词,但每次遇到句号时都应该token[i++][j]。到目前为止,我有这个。

#include <iostream>
#include <istream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    string code;
    getline(cin, code);
    string codef[100];
    string token[100][100];
    int j = 0, i=0;
    for (i = 0; i < 2; i++) {
        stringstream ssin(code);
        while (ssin.good() && j < 4) {
            ssin >> token[i][j];
            ++j;
            if (token[i][j] == ".") {
                break;
            }
        }
    }

    for (i = 0; i < 4; i++) {
        for (j = 0; j < 4; j++) {
            cout << token[i][j] << endl;
        }
        cout << endl;
    }
    return 0;
}

我这样做的方式要求您在句点之前放置一个空格,因为它会检查单独的字符串,如果您像这样将句点捆绑在一起:“你好。”它显然不会识别它。我不希望这种情况发生,有没有更好的方法来完成这项工作?现在我将字符串限制为只有 2 个句子和每个句子 4 个单词,包括句点,所以从技术上讲,只有 3 个单词然后是句点。

【问题讨论】:

    标签: c++ string split


    【解决方案1】:

    为什么不简单地使用std::string::find_first_of 在普通的 std::string 上搜索分隔符?当没有找到任何东西时,它将返回std::string::npos。顺便说一句:我真的建议为 std::array 或 std::vector 放弃那些好的旧数组内容。使用 std::vector 会让你摆脱那些糟糕的硬编码限制。

    无论如何,这就是我的建议。注意:我省略了对数组访问的限制检查,以便您更容易阅读代码,或者您迁移到向量并使用 push_back,或者您必须添加限制检查

    我认为代码几乎是不言自明的,只有一句话:if(pos &gt; last_pos) 是必需的,因为 pos == last_pos 当我们找不到另一个分隔符时。

    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string code = "The quick brown fox jumps over the lazy dog. Lorem ipsum dolor. Cooky";
    
        std::string token[100][100];
        std::size_t last_pos = 0, pos;
        int sentence = 0, word = 0;
        while ((pos = code.find_first_of(". ", last_pos)) != std::string::npos)
        {
            if (pos > last_pos)
                token[sentence][word] = code.substr(last_pos, pos-last_pos /*length*/ );
    
            if(code.substr(pos, 1)[0] == '.')
            {
                ++sentence;
                word = 0;
            }
            else
                ++word;
            last_pos = pos+1;
        }
        if (last_pos < code.length())
            token[sentence][word] = code.substr(last_pos, std::string::npos);
    
    
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                std::cout << token[i][j] << std::endl;
            }
            std::cout << std::endl;
        }
    
        return 0;
    }
    

    由于您的硬编码限制,输出有点模糊,但这与字符串拆分无关,所以我保持原样:

    The
    quick
    brown
    fox
    
    
    Lorem
    ipsum
    dolor
    
    
    Cooky
    

    【讨论】:

      猜你喜欢
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-27
      • 2012-04-20
      相关资源
      最近更新 更多