How to implement split detail in c++
- 2020-06-23 01:12:01
- OfStack
preface
In learning c + + string related basic usage, found sstream istringstream [1] can be similar to the console input in the form of a string, but essentially this behavior is equivalent to the use of space division will be a string, so considering this feature can be used to implement c + + library functions in no string segmentation split function
string src("Avatar 123 5.2 Titanic K");
istringstream istrStream(src); // To establish src to istrStream The link
string s1, s2;
int n; double d; char c;
istrStream >> s1 >> n >> d >> s2 >> c;
// The values delimited by Spaces are entered into the corresponding variables
Implementation details
The purpose is to get the processed string array conveniently by calling 1 function just like in js, and then adjust the parameters according to the actual situation of c++.
1. Input/Output:
string* split(int& length, string str, const char token = ' ')
Returns the first address of the processed array of strings
Passed in: the string str, the delimiter token (the default parameter is a space), and the reference parameter length to indicate the array length to be dynamically allocated after processing
2. Data transparent processing:
Because istringstream treats Spaces as boundaries between data, as cin1 does, when the delimiters are not Spaces, the incoming delimiters need to be replaced with Spaces, and the original Spaces need to be transparently processed in advance
Character substitution makes use of replace() in library algorithm [2]
const char SPACE = 0;
if(token!=' ') {
// Let's replace the original space with ASCII Invisible characters in
replace(str.begin(), str.end(), ' ', SPACE);
// Then the delimiter transposed space, to the string stream processing
replace(str.begin(), str.end(), token, ' ');
}
[
Assume that the input string: "a b, c, d, e, f g"
The delimiter is non-space: ','
"aSPACEb c d e fSPACEg"
3. Data segmentation:
// instantiation 1 The input parameters are the strings to be processed
istringstream i_stream(str);
// will length zero
length = 0;
queue<string> q;
// with 1 a string The instance s Receive incoming data, queue and count
string s;
while (i_stream>>s) {
q.push(s);
length++;
}
4. Array generation:
// According to the count results dynamic opening 1 String array space
string* results = new string[length];
// Transfer the data from the queue into an array
for (int i = 0; i < length; i++) {
results[i] = q.front();
// Restores the replaced whitespace
if(token!=' ') replace(results[i].begin(), results[i].end(), SPACE, ' ');
q.pop();
}
The complete code
#include <iostream>
#include <string>
#include <queue>
#include <sstream>
#include <algorithm>
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<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!=' ') replace(results[i].begin(), results[i].end(), SPACE, ' ');
}
return results;
}
// Testing:
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;
}
reference
[1] C++ string class (C++ string) complete walkthrough
[2] C++ string replaces the specified character
conclusion