Because the C + + string does not have the Split function, the string segmentation Word must be handwritten, it is equivalent to implement a split function!
If you need to split the word according to a single character, read it directly with Getline, very simple
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace Std;
int main ()
{
string words;
vector<string> results;
Getline (cin, words);
Istringstream SS (words);
while (!ss.eof ())
{
string Word;
Getline (SS, Word, ', ');
Results.push_back (word);
}
for (auto Item:results)
{
cout << Item << "";
}
}
If it is a variety of character segmentation, for example,.! And so on, you need to write a function similar to split:
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace Std;
vector<char> is_any_of (String str)
{
vector<char> Res;
for (auto S:str)
Res.push_back (s);
return res;
}
void Split (vector<string>& result, string str, vector<char> delimiters)
{
Result.clear ();
Auto start = 0;
while (Start < str.size ())
{
Split based on multiple delimiters
Auto Itres = Str.find (Delimiters[0], start);
for (int i = 1; i < delimiters.size (); ++i)
{
Auto it = Str.find (Delimiters[i],start);
if (It < itres)
Itres = it;
}
if (itres = = String::npos)
{
Result.push_back (Str.substr (Start, str.size ()-start);
Break
}
Result.push_back (Str.substr (Start, Itres-start));
start = Itres;
++start;
}
}
int main ()
{
string words;
vector<string> result;
Getline (cin, words);
Split (result, words, is_any_of (",.?!"));
for (auto Item:result)
{
cout << item << ';
}
}
For example: Enter Hello world! Welcome to my Blog,thank you!
Thanks to another implementation method provided by @chxuan, use strtok_s to split strings:
Std::vector<std::string> Split (const std::string& STR, const std::string& delimiter)
{
char* save = nullptr;
char* token = strtok_s (const_cast<char*> (Str.c_str ()), Delimiter.c_str (), &save);
std::vector<std::string> result;
while (token!= nullptr)
{
Result.emplace_back (token);
token = strtok_s (nullptr, Delimiter.c_str (), &save);
}
return result;
}
String segmentation using the Strtok function
Prototype: Char *strtok (char *str, const char *delim);
Function: Explode string as a set of strings.
Parameter description: STR is the string to be decomposed, Delim as the delimiter string.
Return value: A split string that starts at the beginning of the Str. Returns NULL when a string is not split.
Other: strtok function thread is unsafe and can be replaced with Strtok_r.
Example:
Using Strtok to realize split
#include <string.h>
#include <stdio.h>
int main ()
{
Char s[] = "Golden Global view,disk * desk";
const char *D = ", *";
Char *p;
p = strtok (s,d);
while (p)
{
printf ("%s\n", p);
P=strtok (NULL,D);
}
return 0;
}
The effect is as shown in the following illustration:
Second, using STL to divide the string
Two functions involving the string class find and substr:
1, find function
Prototype: size_t find (const string& str, size_t pos = 0) const;
Function: Find the position where the substring first appears.
Parameter description: STR is a substring, and POS is the initial lookup location.
Return value: If found, return to the first occurrence of the position, otherwise return String::npos
2. substr function
Prototype: String substr (size_t pos = 0, size_t n = npos) const;
Function: Gets the substring.
Parameter description: POS is the starting position (default is 0), n is the end position (default is NPOs)
return value: substring
Implemented as follows:
String Split function
Std::vector<std::string> Split (std::string str,std::string pattern)
{
Std::string::size_type POS;
std::vector<std::string> result;
str+=pattern;//extended strings for easy operation
int size=str.size ();
for (int i=0; i<size; i++)
{
Pos=str.find (Pattern,i);
if (pos<size)
{
std::string s=str.substr (i,pos-i);
Result.push_back (s);
I=pos+pattern.size ()-1;
}
}
return result;
}
Complete code:
/*
File:split1.cpp
Author:mike
E-mail:mike_zhang@live.com
*/
#include <iostream>
#include <string>
#include <vector>
String Split function
Std::vector<std::string> Split (std::string str,std::string pattern)
{
Std::string::size_type POS;
std::vector<std::string> result;
str+=pattern;//extended strings for easy operation
int size=str.size ();
for (int i=0; i<size; i++)
{
Pos=str.find (Pattern,i);
if (pos<size)
{
std::string s=str.substr (i,pos-i);
Result.push_back (s);
I=pos+pattern.size ()-1;
}
}
return result;
}
int main ()
{
std::string str;
std::cout<< "Please input str:" <<std::endl;
std::cin>>str;
Getline (STD::CIN,STR);
Std::string pattern;
std::cout<< "Please input pattern:" <<std::endl;
std::cin>>pattern;
Getline (Std::cin,pattern);//To get a string with a space
Std::vector<std::string> Result=split (Str,pattern);
std::cout<< "The result:" <<std::endl;
for (int i=0; i<result.size (); i++)
{
std::cout<<result[i]<<std::endl;
}
Std::cin.get ();
Std::cin.get ();
return 0;
}
The effect is as shown in the following illustration:
Third, using boost for string segmentation
Using the regular expression of boost library to implement string segmentation
Implemented as follows:
Std::vector<std::string> Split (std::string str,std::string s)
{
Boost::regex Reg (S.c_str ());
Std::vector<std::string> VEC;
Boost::sregex_token_iterator It (Str.begin (), Str.end (), reg,-1);
Boost::sregex_token_iterator end;
while (It!=end)
{
Vec.push_back (*it++);
}
return VEC;
}
Complete code:
This program realizes the use of regular expressions for string segmentation
Operating environment VC6.0 + Boost Library
/*
File:split2.cpp
Author:mike
E-mail:mike_zhang@live.com
*/
#include <iostream>
#include <cassert>
#include <vector>
#include <string>
#include "boost/regex.hpp"
Std::vector<std::string> Split (std::string str,std::string s)
{
Boost::regex Reg (S.c_str ());
Std::vector<std::string> VEC;
Boost::sregex_token_iterator It (Str.begin (), Str.end (), reg,-1);
Boost::sregex_token_iterator end;
while (It!=end)
{
Vec.push_back (*it++);
}
return VEC;
}
int main ()
{
Std::string str,s;
Str= "SSS/DDD/GGG/HH";
s= "/";
Std::vector<std::string> Vec=split (str,s);
for (int i=0,size=vec.size (); i<size;i++)
{
std::cout<<vec[i]<<std::endl;
}
Std::cin.get ();
Std::cin.get ();
return 0;
}