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

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

青灯夜游
Release: 2019-11-25 16:50:56
forward
3373 people have browsed it

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;
//以空格为分界的各数值则输入到了对应变量上
Copy after login

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 = ' ')
Copy after login

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, ' ');
  }
Copy after login
  假设输入字符串为:"a b,c,d,e,f g"
  分隔符为非空格:','
  则被替换为:"aSPACEb c d e fSPACEg"
Copy after login

3. Data segmentation:

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

4. Array generation:

  //根据计数结果动态开辟一个字符串数组空间
  string* results = new string[length]; 
  //将队列中的数据转入数组中
  for (int i = 0; i < length; i++) {
    results[i] = q.front();
    //将替换掉的空格进行还原
    if(token!=&#39; &#39;) replace(results[i].begin(), results[i].end(), SPACE, &#39; &#39;);
    q.pop();
  }
Copy after login

Complete code

#include <iostream>
#include <string>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;

string* split(int& length, string str,const char token = &#39; &#39;) {
  const char SPACE = 0;
  if(token!=&#39; &#39;) {
    replace(str.begin(), str.end(), &#39; &#39;, SPACE);
    replace(str.begin(), str.end(), token, &#39; &#39;);
  }
  istringstream i_stream(str);
  queue<string> 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!=&#39; &#39;) replace(results[i].begin(), results[i].end(), SPACE, &#39; &#39;);
  }
  return results;
}

//测试:
int main() {
  int length;
  string* results = split(length, "a b,c,d,e,f g", &#39;,&#39;);
  for (int i = 0; i < length; i++) cout<<results[i]<<endl;
  return 0;
}
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template