We often encounter the problem of string segmentation. Here we will summarize it for my convenience.
I. UseStrtokFunctionString segmentation
Prototype:Char * strtok (char * STR, const char * delim );
Function:Splits a string into a group of strings.
Parameter description:STrIs the string to be decomposed,DelimIt is a separator string.
Return Value:SlaveSTrSplit strings starting. Returns if no split string exists.Null.
Example:
1 // Use strtok to implement split
2 # Include < String . H>
3 # Include <stdio. h>
4
5 Int Main ()
6 {
7 Char S [] = " Golden global view, disk * desk " ;
8 Const Char * D = " ,* " ;
9 Char * P;
10 P = strtok (S, d );
11 While (P)
12 {
13 Printf ( " % S \ n " , P );
14 P = strtok (null, d );
15 }
16
17 Return 0 ;
18 }
Running effect:
Ii. UseSTLString segmentation
The following two functions are involved: Find and substr:
1. Find function
Prototype: size_t find (const string & STR, size_t Pos = 0) const;
Function: locate the first occurrence of a substring.
Parameter description: STR is a substring, and POS is the initial search position.
Returned value: if it is found, the first position is returned. Otherwise, string: NPOs is returned.
2. substr Function
Prototype: String substr (size_t Pos = 0, size_t n = NPOs) const;
Function: obtain a substring.
Parameter description: POS is the starting position (0 by default), and N is the ending position (NPOs by default)
Return Value: substring
The implementation is as follows:
1 // String segmentation Function
2 STD: vector <STD :: String > Split (STD :: String STR, STD :: String Pattern)
3 {
4 STD :: String : Size_type Pos;
5 STD: vector <STD :: String > Result;
6 STR + = pattern; // Extended string for easy operation
7 Int Size = Str. Size ();
8
9 For ( Int I = 0 ; I <size; I ++)
10 {
11 Pos = Str. Find (pattern, I );
12 If (Pos <size)
13 {
14 STD :: String S = Str. substr (I, pos-I );
15 Result. push_back (s );
16 I = POS + pattern. Size ()- 1 ;
17 }
18 }
19 Return Result;
20 }
CompleteCode:
View code
1 /*
2 File: split1.cpp
3 Author: Mike
4 E-mail: Mike_Zhang@live.com
5 */
6 # Include <iostream>
7 # Include < String >
8 # Include <vector>
9
10 // String segmentation Function
11 STD: vector <STD :: String > Split (STD :: String STR, STD :: String Pattern)
12 {
13 STD :: String : Size_type Pos;
14 STD: vector <STD :: String > Result;
15 STR + = pattern;// Extended string for easy operation
16 Int Size = Str. Size ();
17
18 For ( Int I = 0 ; I <size; I ++)
19 {
20 Pos = Str. Find (pattern, I );
21 If (Pos <size)
22 {
23 STD :: String S = Str. substr (I, pos-I );
24 Result. push_back (s );
25 I = POS + pattern. Size ()- 1 ;
26 }
27 }
28 Return Result;
29 }
30
31 Int Main ()
32 {
33 STD :: String STR;
34 STD: cout < " Please input STR: " <STD: Endl;
35 // STD: CIN> STR;
36 Getline (STD: Cin, STR );
37 STD :: String Pattern;
38 STD: cout < " Please input pattern: " <STD: Endl;
39 // STD: CIN> pattern;
40 Getline (STD: Cin, pattern ); // Used to obtain strings containing Spaces
41 STD: vector <STD :: String > Result = Split (STR, pattern );
42 STD: cout < " The result: " <STD: Endl;
43 For ( Int I = 0 ; I <result. Size (); I ++)
44 {
45 STD: cout <result [I] <STD: Endl;
46 }
47
48 STD: cin. Get ();
49 STD: cin. Get ();
50 Return 0 ;
51 }
Running effect:
3. Use boost to separate strings
Use the regular expression of the boost library to split strings
The implementation is as follows:
1 STD: vector <STD :: String > Split (STD :: String STR, STD :: String S)
2 {
3 Boost: RegEx Reg (S. c_str ());
4 STD: vector <STD :: String > VEC;
5 Boost: sregex_token_iterator it (Str. Begin (), str. End (), Reg ,- 1 );
6 Boost: sregex_token_iterator end;
7 While (It! = End)
8 {
9 VEC. push_back (* It ++ );
10 }
11 Return VEC;
12 }
Complete code:
View code
1 // BenProgramThe regular expression is used to separate strings.
2 // Running Environment vc6.0 + boost Library
3 /*
4 File: split2.cpp
5 Author: Mike
6 E-mail: Mike_Zhang@live.com
7 */
8 # Include <iostream>
9 # Include <cassert>
10 # Include <vector>
11 # Include <String >
12 # Include " Boost/RegEx. HPP "
13
14 STD: vector <STD :: String > Split (STD :: String STR, STD :: String S)
15 {
16 Boost: RegEx Reg (S. c_str ());
17 STD: vector <STD :: String > VEC;
18 Boost: sregex_token_iterator it (Str. Begin (), str. End (), Reg ,- 1 );
19 Boost: sregex_token_iterator end;
20 While (It! = End)
21 {
22 VEC. push_back (* It ++ );
23 }
24 Return VEC;
25 }
26 Int Main ()
27 {
28 STD :: String STR, S;
29 STR = " SSS/DDD/ggg/HH " ;
30 S = " / " ;
31 STD: vector <STD :: String > VEC = Split (STR, S );
32 For ( Int I = 0 , Size = Vec. Size (); I <size; I ++)
33 {
34 STD: cout <VEC [I] <STD: Endl;
35 }
36 STD: cin. Get ();
37 STD: cin. Get ();
38 Return 0 ;
39 }
Running effect:
Supplement:
Recently, we found that boost has built-in split functions. If boost is used, it is better to use split directly. I will not talk about it here. The Code is as follows:
# Include <iostream> # Include < String > # Include <Vector> # Include <Boost/algorithm/ String /Classification. HPP> # Include <Boost/algorithm/ String /Split. HPP> Using Namespace STD; Int Main (){ String S = " SSS/DDD, ggg " ; Vector < String > Vstr; Boost: Split (vstr, S, boost: is_any_of ( " ,/ " ), Boost: token_compress_on ); For (Vector < String >:: Iterator it = vstr. Begin (); it! = Vstr. End (); ++ It) cout <* It <Endl; Return 0 ;}
Okay, that's all. I hope it will help you.