String segmentation function splitestring

xiaoxiao2021-03-05  54

The string segmentation function is a function that is commonly used in program development. It is relatively simple, here is my implementation:

Using namespace std;

Template

Void __splitestring (tchar * p, const tchar ctok, functor func)

{

TCHAR * E = P;

DO

{

e = _TCSCHR (P, CTOK);

IF (e! = NULL)

* E = _t ('/ 0');

Func (P);

P = E;

} while (p! = null);

}

Template

Void splitestring (const tchar * psz, const tchar ctok, functor func)

{

String str = psz;

__Splitestring (tchar *) Str.c_str (), CTOK, FUNC);

}

Template

Void __splitestring (tchar * p, const tchar * sztok, functor func)

{

INT LEN = _TCSLEN (P);

TCHAR * E = P;

DO

{

e = _tcsstr (p, sztok);

IF (e! = NULL)

{

* E = _t ('/ 0');

E = LEN;

}

Func (P);

P = E;

} while (p! = null);

}

Template

Void splitestring (const tchar * psz, const tchar * sztok, functor func)

{

String str = psz;

__SpliteString ((tchar *) str.c_str (), sztok, func);

} The code is very simple to explain. The application example is given below: typedef vector string_vector;

Typedef string_vector :: item string_iterator;

Struct InTovector

{

INTOVECTOR (string_vector & str_VEC): _STR_VEC (STR_VEC) {}

Void Operator () (const tchar * str)

{

_STR_VEC.PUSH_BACK (STR);

}

String_vector & _str_vec;

}

Void Println (Const String & Str)

{

COUT << str << endl;

}

void main ()

{

Tchar szsource [] = "Hello | this | IS | a | test | string";

String_vector splite;

SplitString (Szsource, '|', InTovector (SPLITED);

For_each (splited.begin (), splited.end (), println);

}

The above code is divided by the vertical line division, put into a VECTOR container and prints branch. If you want to return the number of strings after cutting? Change the: typedef vector string_vector;

Typedef string_vector :: item string_Iterator; Struct InTovector

{

INTOVECTOR (String_Vector & Str_VEC): _STR_VEC (STR_VEC), _ count (0) {}

Void Operator () (const tchar * str)

{

_STR_VEC.PUSH_BACK (STR);

_count;

}

String_vector & _str_vec;

Int_count;

}

Void Println (Const String & Str)

{

COUT << str << endl;

}

void main ()

{

Tchar szsource [] = "Hello | this | IS | a | test | string";

String_vector splite;

INTOVECTOR INTOVECTOR (SPLITED);

Splitestring (Szsource, '|', (InTovector &) InTovector;

For_each (splited.begin (), splited.end (), println);

Cout << INTOVECTOR._COUNT << Endl;

} It looks perfect: in order to prevent copy re-constructing the INTOVECTOR object, the special mandatory type is converted to the INTOVECTOR. The truth is always unexpected and annoyed, and the print results are still 0. Tracking results You can find that spliteString is also passed or the object of INTOVECTOR copy constructs, not reference. The reason is not the scope of discussion, let me express me, I also express it. However, the solution is still there. Method 1. Put the count as an external object: Struct InTovector

{

InTovector (string_vector & str_vec, int count): _STR_VEC (STR_VEC), _ count (count) {}

Void Operator () (const tchar * str)

{

_STR_VEC.PUSH_BACK (STR);

_count;

}

String_vector & _str_vec;

INT & _COUNT;

}

void main ()

{

Tchar szsource [] = "Hello | this | IS | a | test | string";

String_vector splite;

INT country;

INTOVECTOR INTOVECTOR (Splited, Count);

Splitestring (Szsource, '|', INTOVECTOR);

For_each (splited.begin (), splited.end (), println);

Cout << count << Endl;

} Method 2. Of course there is a better solution. Boost is a good thing only needs to modify the line oksplitestring (szsource, '|', boost :: bind (boost :: ref (intovector), _1)); why do you write? Not the discussion here. Check out the Boost source code will be helpful. Method 3. This is a UGLY method - modify the SplitString function, change the pasters to refer to reference: Void splitestring (const tchar * psz, const tchar * sztok, functor & func);

转载请注明原文地址:https://www.9cbs.com/read-32904.html

New Post(0)