String segmentation (c + +)

Source: Internet
Author: User

often encountered the problem of string segmentation, summarized here, also convenient for me to use later.

First, using strtok function for string segmentation

Prototype: Char *strtok (char *str, const char *delim);

Features: decomposes a string into a set of strings.

parameter Description:str is the string to be decomposed,Delim is the delimiter string.

return value: a segmented string starting from the beginning of the S tr. NULL is returned when there is no split string .

Other: strtok function thread is unsafe, you can use Strtok_r instead.

Example:

Using Strtok to achieve 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;
}

Operating Effect:

second, with STL to split a 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: Finds the position where the substring first appears.
Parameter description: STR is a substring and POS is the initial find location.
Return value: If found, returns the first occurrence of the position, otherwise returns String::npos

2. substr function
Prototype: String substr (size_t pos = 0, size_t n = npos) const;
function: Gets substring.
Parameter description: POS is the starting position (default is 0), n is the end position (default is NPOs)
return value: substring

The implementation is as follows:

Full code:

String splitting function
Std::vector<std::string> Split (std::string str,std::string pattern)
{
Std::string::size_type POS;
std::vector<std::string> result;
str+=pattern;//extending strings for ease of 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;
}

/*
File:split1.cpp
Author:mike
e-mail: [Email protected]
*/
#include <iostream>
#include <string>
#include <vector>


String splitting function
Std::vector<std::string> Split (std::string str,std::string pattern)
{
Std::string::size_type POS;
std::vector<std::string> result;
str+=pattern;//extending strings for ease of 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);//used to get a string with spaces
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;
}

Operating effect:

Third, using boost for string segmentation

Using the regular expression of the boost library to implement the string segmentation
The implementation is 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;
}

Full code:

The realization of this program is to use regular expressions to partition the string
Operating environment VC6.0 + Boost Library
/*
File:split2.cpp
Author:mike
e-mail: [Email protected]
*/
#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;
}

Operating effect:

Add:

Recently found that the boost has its own split function, if you use boost, or directly with Split's good, here is not much to say, 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;
}

All right, that's it, I hope it helps you.

String segmentation (c + +)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.