c语言将字符串按空格分割_如何在c++中实现字符串分割函数split详解

前言

在学习c++中string相关基本用法的时候,发现了sstream的istringstream[1]可以将字符串类似于控制台的方式进行输入,而实质上这个行为等同于利用空格将一个字符串进行了分割,于是考虑到可以利用这个特性来实现c++库函数中没有的字符串分割函数split

string src("Avatar 123 5.2 Titanic K");

istringstream istrStream(src); //建立src到istrStream的联系

string s1, s2;

int n; double d; char c;

istrStream >> s1 >> n >> d >> s2 >> c;

//以空格为分界的各数值则输入到了对应变量上

实现细节

目的是可以像js中一样,调用一个函数即可以方便地获取到处理完毕后的字符串数组,根据c++的实际情况再进行参数调整。

1. 输入输出:

string* split(int& length, string str, const char token = ' ')

返回:处理完的字符串数组的首地址

传入:字符串str、分隔符token(默认参数为空格)、以及引用参数length,指明处理完毕后动态分配的数组长度

2. 数据透明处理:

由于istringstream会像cin一样,把空格视为数据间的界限,所以当分隔符不是空格时,需要将传入的分隔符换为空格,并且要提前对原有空格进行数据透明处理

字符替换利用了库algorithm中的replace() [2]

const char SPACE = 0;

if(token!=' ') {

// 先把原有的空格替换为ASCII中的不可见字符

replace(str.begin(), str.end(), ' ', SPACE);

// 再把分隔符换位空格,交给字符串流处理

replace(str.begin(), str.end(), token, ' ');

}

假设输入字符串为:"a b,c,d,e,f g"

分隔符为非空格:','

则被替换为:"aSPACEb c d e fSPACEg"

3. 数据分割:

//实例化一个字符串输入流,输入参数即待处理字符串

istringstream i_stream(str);

//将length置零

length = 0;

queue q;

//用一个string实例s接收输入流传入的数据,入队并计数

string s;

while (i_stream>>s) {

q.push(s);

length++;

}

4. 数组生成:

//根据计数结果动态开辟一个字符串数组空间

string* results = new string[length];

//将队列中的数据转入数组中

for (int i = 0; i < length; i++) {

results[i] = q.front();

//将替换掉的空格进行还原

if(token!=' ') replace(results[i].begin(), results[i].end(), SPACE, ' ');

q.pop();

}

完整代码

#include

#include

#include

#include

#include

using namespace std;

string* split(int& length, string str,const char token = ' ') {

const char SPACE = 0;

if(token!=' ') {

replace(str.begin(), str.end(), ' ', SPACE);

replace(str.begin(), str.end(), token, ' ');

}

istringstream i_stream(str);

queue q;

length = 0;

string s;

while (i_stream>>s) {

q.push(s);

length++;

}

string* results = new string[length];

for (int i = 0; i < length; i++) {

results[i] = q.front();

q.pop();

if(token!=' ') replace(results[i].begin(), results[i].end(), SPACE, ' ');

}

return results;

}

//测试:

int main() {

int length;

string* results = split(length, "a b,c,d,e,f g", ',');

for (int i = 0; i < length; i++) cout<

return 0;

}

参考

[1] C++ string类(C++字符串)完全攻略

[2] C++ string 替换指定字符

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。

本文标题: 如何在c++中实现字符串分割函数split详解

本文地址: http://www.cppcns.com/ruanjian/c/288758.html