main file:
/* Function: separates strings and selects three different methods to save the results. The final proof: the first method is probably the most convenient (convenient and fast, you don't have to do too much work on your own, you can maintain the memory space on your own) */# include <iostream> # include <vector> # include "z_string.h" using namespace STD; void spliter (char * psrc, char * spliter, vector <string> & V) {char * token = NULL; char * temp = new char [strlen (psrc) + 1]; strcpy (temp, psrc); token = strtok (temp, spliter ); // strtok will change the Temp value, so we copied the string above while (token) {v. push_back (token); token = strtok (null, spliter);} Delete temp;} V Oid display (vector <string> & V) {for (vector <string>: iterator it = V. Begin (); it! = V. end (); It ++) {cout <"#" <it-> c_str () <"#" <Endl ;}} string * spliter (char * psrc, char * spliter) {string * ret = NULL; char * token = NULL; vector <string> V; char * temp = new char [strlen (psrc) + 1]; strcpy (temp, psrc); token = strtok (temp, spliter); While (token) {v. push_back (token); token = strtok (null, spliter);} Delete temp; int n = v. size (); ret = new string [n + 1]; char T [20] = {0}; ITOA (n, T, 10); RET [0] = T; // The first element saves the array length for (INT I = 1; I <= N; I ++) {RET [I] = v. at (I-1);} return ret;} void display (string * plist) {int n = atoi (plist [0]. c_str (); For (INT I = 1; I <= N; I ++) {cout <"#" <plist [I]. c_str () <"#" <Endl ;}} void main () {char SRC [] = "What are you, Do 1.0, 2.33, 556 "; char spliter [] = ", \ t \ n"; // Method 1: Use Vector <string> to save the split result vector <string> V1; spliter (SRC, spliter, v1); display (V1); // Method 2: Use string [] to save the result cout <Endl; string * plist = spliter (SRC, spliter); display (plist ); delete [] plist; // if it is written as "delete plist;", error // method 3: Custom class to manage space cout <Endl; z_string Zs; Zs. spliter (SRC, spliter); Zs. display ();}
Z_string.h:
# Include <string> using namespace STD; // closed string [] class, dynamically variable number of elements and automatically manage allocated space class z_string {PRIVATE: int length; string * List; public: z_string (INT n = 0 );~ Z_string (); void spliter (char * SRC, char * op); int getsize (); string at (int n) const; void display (); void free ();};
Z_string.cpp:
/* Joeblackzqq (joeblackzqq@163.com) function: encapsulation of the string class, the main function is to split the string, and then save the results, you can maintain your own memory */# include <iostream> # include <vector> # include "z_string.h" z_string: z_string (INT Len) {If (LEN> 0) {list = new string [Len]; length = Len;} else {length = 0; List = NULL;} z_string ::~ Z_string () {free ();} void z_string: spliter (char * SRC, char * Separator) // splits the string and saves the result {free (); int Len = strlen (SRC) + 1; char * ptemp = new char [Len]; strcpy (ptemp, Src); char * token = NULL; vector <string> V; token = strtok (ptemp, separator); While (token) {v. push_back (token); token = strtok (null, separator);} Delete ptemp; length = v. size (); List = new string [length]; for (INT I = 0; I <length; I ++) {list [I] = v. at (I) ;}} int z_string: getsize () {return length;} string z_string: At (int I) const {if (I >= 0 & I <length) {return list [I];} elsereturn NULL;} void z_string: Display () {for (INT I = 0; I <length; I ++) {cout <list [I]. c_str () <Endl ;}} void z_string: Free () {If (list) {Delete [] list; List = NULL; length = 0 ;}}