1. string segmentation algorithm (standard library version)
Void split (const string & s, char c, vector <string> & v)
{
String: size_type I = 0;
String: size_type j = s. find (c );
While (j! = String: npos)
{
V. push_back (s. substr (I, j-I ));
I = ++ j;
J = s. find (c, j );
If (j = string: npos)
V. push_back (s. substr (I, s. length ()));
}
}
Template <typename T>
Void split (basic_string <T> & s, T c, vector <basic_string <T> & v)
{
Typename basic_string <T >:: size_type I = 0;
Typename basic_string <T >:: size_type j = s. find (c );
While (j! = Basic_string <T >:: npos)
{
V. push_back (s. substr (I, j-I ));
I = ++ j;
J = s. find (c, j );
If (j = basic_string <T>: npos)
{
V. push_back (s. substr (I, s. length ()));
}
}
}
2. string segmentation algorithm (implemented by boost Library)
Int main ()
{
String _ str = "I | am | a | bad | boy ";
List <string> myList;
Split (myList, _ str, is_any_of ("|"); // The boost group library method is used here: split.
List <string >:: iterator p;
For (p = myList. begin (); p! = MyList. end (); ++ p)
{
Cout <* p <"";
}
Cout <endl;
/*************************************** ********/
Return 0;
}