Home  >  Article  >  Backend Development  >  How to implement the string splitting function split in C++? (code example)

How to implement the string splitting function split in C++? (code example)

青灯夜游
青灯夜游forward
2019-11-25 16:50:563321browse

While learning the basic usage of strings in C, I discovered that sstream's istringstream[1] can input strings in a console-like manner. In fact, this behavior is equivalent to using spaces to enter a string. segmentation.

How to implement the string splitting function split in C++? (code example)

So I considered that this feature can be used to implement the string splitting function 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;
//以空格为分界的各数值则输入到了对应变量上

Implementation details

The purpose is to easily obtain the processed string array by calling a function just like in js, and then adjust the parameters according to the actual situation of c.

1. Input and output:

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

Return: the first address of the processed string array
Pass in: string str, delimiter token (default The parameter is a space), and the reference parameter length, indicating the length of the dynamically allocated array after processing

2. Data transparent processing:
Since istringstream will treat spaces like cin, is the boundary between data, so when the delimiter is not a space, the incoming delimiter needs to be replaced with a space, and the original space must be processed transparently in advance
Character replacement uses replace() in the library algorithm [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. Data segmentation:

  //实例化一个字符串输入流,输入参数即待处理字符串
  istringstream i_stream(str); 
    //将length置零
  length = 0; 
  queue98c455a79ddfebb79781bff588e7b37e q;
  //用一个string实例s接收输入流传入的数据,入队并计数
  string s;
  while (i_stream>>s) {
    q.push(s);
    length++;
  }

4. Array generation:

  //根据计数结果动态开辟一个字符串数组空间
  string* results = new string[length]; 
  //将队列中的数据转入数组中
  for (int i = 0; i 3ed72bf3b4ac09015dc6737cb8a5f76a
#include 98c455a79ddfebb79781bff588e7b37e
#include 578d2716abf2876f231498269846f87d
#include b9d007fdd0a9230760ee80bd9f78ebf5
#include e23c27865115669ba6cc99530e9d22b3
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);
  queue98c455a79ddfebb79781bff588e7b37e 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<<results[i]<<endl;
  return 0;
}

This article comes from the C#.Net Tutorial column, welcome to learn!

The above is the detailed content of How to implement the string splitting function split in C++? (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete