在不使用向量的情况下将字符串拆分为 C 中的数组

新手上路,请多包涵

我正在尝试将由空格分隔的字符串插入到字符串数组中, 而不 在 C++ 中使用向量。例如:

 using namespace std;
int main() {
    string line = "test one two three.";
    string arr[4];

    //codes here to put each word in string line into string array arr
    for(int i = 0; i < 4; i++) {
        cout << arr[i] << endl;
    }
}

我希望输出为:

 test
one
two
three.

我知道在 C++ 中已经有其他问题询问字符串 > 数组,但我找不到任何满足我的条件的答案:将字符串拆分为数组而不使用向量。

原文由 txp111030 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 454
2 个回答

可以使用 std::stringstream 类(其构造函数将字符串作为参数)将字符串转换为流。构建完成后,您可以在其上使用 >> 运算符(如基于常规文件的流),它将从中提取或 标记 单词:

 #include <iostream>
#include <sstream>

using namespace std;

int main(){
    string line = "test one two three.";
    string arr[4];
    int i = 0;
    stringstream ssin(line);
    while (ssin.good() && i < 4){
        ssin >> arr[i];
        ++i;
    }
    for(i = 0; i < 4; i++){
        cout << arr[i] << endl;
    }
}

原文由 didierc 发布,翻译遵循 CC BY-SA 4.0 许可协议

我为此制作了一个标题,您可以使用它来将字符串拆分为字符串数组,使用字符串作为拆分器继承人如何:

  1. 创建一个名为“WordSplitter.h”的文件并将其与 cpp 文件的文件夹一起保存。

  2. 复制粘贴到 WordSplitter.h 中:

 #ifndef WORDSPLITTER_H
#undef WORDSPLITTER_H
#include <iostream>
#include <string>
using namespace std;

class Word {
    public:
        string word;
        string list[99];

    Word(string s){
        word = s;
    }

    int size(string s){
        int size = 0;
        for(char c : s){
            size ++;
        }
        return size;
    }

    int size(){
        int size = 0;
        for (char c : word){
            size ++;
        }
        return size;
    }

    string slice(int start, int end){
        string result = "";
        for(int i = start ; i <= end ; i ++){
            result += word[i];
        }
        return result;
    }

    string sliceThis(string s, int start, int end){
        string result = "";
        for(int i = start ; i <= end ; i ++){
            result += s[i];
        }
        return result;
    }

    int count(string s){
        int count, start = 0;
        for(int end = size(s)-1 ; end < size() ; end ++){
            if(s == slice(start,end)){count ++;}
            start ++;
        }
        return count;
    }

    int listSize(){
        int size_ = 0;
        for(string str : list){
            if (size(str) > 0){
                size_ ++;
            }
        }
        return size_;
    }

    void split(string splitter){
        int splitSize = size(splitter) - 1;
        int split_start = 0;
        int split_end, end;
        int index = 0;

        if (count(splitter) > 0){
            for(end = splitSize ; end < size() ; end ++){
                int start = end - splitSize;
                if (splitter == slice(start,end)){
                    split_end = start - 1;
                    list[index] = slice(split_start,split_end);
                    split_start = end + 1;
                    end += splitSize;
                    index ++;
                }
            }
            list[index] = slice(split_end + size(splitter) + 1, size());
            //list[index] = slice(end + 1, size());
        }
    }
};

#endif

  1. 在您的 cpp 文件中:
 #include <iostream>
#include <string>
#include "WordSplitter.h"

using namespace std;

int main(){
    Word mySentence = Word("I[]got[]a[]jar[]of[]dirt");
    mySentence.split("[]"); //use a splitter

    for(int i = 0 ; i < mySentence.count("[]") + 1 ; i ++){
        cout << mySentence.list[i] << endl;
    }

    return 0;
}

输出:

 I
got
a
jar
of
dirt

原文由 Rollie Garcia 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题