在C++中不使用vector将字符串拆分为数组

ttygqcqt  于 2022-12-05  发布在  其他
关注(0)|答案(6)|浏览(131)

我尝试在C++中使用vector将一个由空格分隔的字符串插入到一个字符串数组中。例如:

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++中已经有其他问题在问string〉array,但是我找不到任何满足我的条件的答案:不使用vector将字符串拆分为数组。

lc8prwob

lc8prwob1#

可以使用std::stringstream类将字符串转换为流(其构造函数以字符串作为参数)。一旦构建好,就可以使用>>操作符(就像基于常规文件的流),它将从其中提取或 tokenize word:

#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;
    }
}
pvcm50d1

pvcm50d12#

#include <iostream>
#include <sstream>
#include <iterator>
#include <string>

using namespace std;

template <size_t N>
void splitString(string (&arr)[N], string str)
{
    int n = 0;
    istringstream iss(str);
    for (auto it = istream_iterator<string>(iss); it != istream_iterator<string>() && n < N; ++it, ++n)
        arr[n] = *it;
}

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

    splitString(arr, line);

    for (int i = 0; i < 4; i++)
       cout << arr[i] << endl;
}
jljoyd4f

jljoyd4f3#

#define MAXSPACE 25

string line =  "test one two three.";
string arr[MAXSPACE];
string search = " ";
int spacePos;
int currPos = 0;
int k = 0;
int prevPos = 0;

do
{

    spacePos = line.find(search,currPos);

    if(spacePos >= 0)
    {

        currPos = spacePos;
        arr[k] = line.substr(prevPos, currPos - prevPos);
        currPos++;
        prevPos = currPos;
        k++;
    }

}while( spacePos >= 0);

arr[k] = line.substr(prevPos,line.length());

for(int i = 0; i < k; i++)
{
   cout << arr[i] << endl;
}
brqmpdu1

brqmpdu14#

这里有一个建议:在字符串中使用两个索引,比如startendstart指向要提取的下一个字符串的第一个字符,end指向属于要提取的下一个字符串的最后一个字符之后的字符。start从零开始,end得到start后面第一个字符的位置,然后取[start..end)之间的字符串,并将其添加到数组中,一直添加到字符串的末尾。

xwbd5t1u

xwbd5t1u5#

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

int main() {

    string s1="split on     whitespace";
    istringstream iss(s1);
    vector<string> result;
    for(string s;iss>>s;)
        result.push_back(s);
    int n=result.size();
    for(int i=0;i<n;i++)
        cout<<result[i]<<endl;
    return 0;
}

输出:-
劈裂

空白

pw136qt2

pw136qt26#

我为这个做了一个头,你可以用它把一个字符串拆分成一个字符串数组,用一个字符串作为拆分器,方法如下:
1.创建一个名为“WordSplitter.h”的文件,并将其与cpp文件的文件夹沿着保存。
1.复制粘贴到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

相关问题