在C++编程语言中,对于字符的处理,可以通过使用C++ strtok来进行具体的操作。那么正确的应用方法我们将会在这篇文章中为大家详细介绍,希望能对大家有所帮助,提高实际程序开发效率。
/ e* i1 R5 p: Q& Y E/ J3 p$ A C++ strtok原形如下:
+ h, L v& u1 [0 E$ a: J char *strtok(
5 ?, S" q5 j& k# n. H( A char *strToken,$ X% P* Y1 s; P. V- W4 i/ |
const char *strDelimit );
$ o7 H; Y: i% P# k3 \5 d! f b% f // crt_strtok.c4 ~( N A- w, R) d. ]
/**//* In this program, a loop uses strtok& ?+ K% X" x+ D* Y* s+ b: M
* to print all the tokens (separated by commas
, i" j: w R/ l6 }4 u6 B * or blanks) in the string named "string". */
1 V h y8 v6 l% L' j6 H" v% e' Y #include < string.h>
( l3 e+ ?9 C c/ m9 O* g #include < stdio.h>+ u3 P6 g' Z# G. b- f
char string[] = "A string\tof ,,tokens\nand some more tokens";1 ~4 n/ e( Q6 ]
char seps[] = " ,\t\n";
+ [( E5 V% ^) X5 m char *token;
9 L1 W3 _% c$ b4 O0 f. e int main( void )" |/ ~9 T" B% o
{
! p, W. b% d" ?; S: [# D! A printf( "Tokens:\n" );; j% C6 X) z% q( O
/**//* Establish string and get the first token: */
# a! I5 G0 ~3 x( {, `3 Q token = strtok( string, seps );
4 e8 U) r$ \* v- ^ while( token != NULL )
; w0 }, D! p8 P/ S- u { /**//* While there are tokens in "string" */
% T4 v- W m. C: [+ @ printf( " %s\n", token );# C+ }1 M S- y2 T2 z
/**//* Get next token: */
+ A0 _0 `- z& P% W token = strtok( NULL, seps );
! K. g/ b4 ?/ I, F5 J5 A# e }6 z( k, x ]! ?) i3 {7 b+ s. H
}
0 q9 J8 X* C' Z n; Y# R C++ strtok输出:
6 E8 K6 I* ^- [" m) W: g! ?5 p9 L' G A
{2 g0 ~! p' X- d string
7 v$ q9 U, P5 [# _1 n% T( G of# C0 x' o, s$ Q/ b* W& Q6 m
tokens
& l) L7 J0 g! R and
# w) q6 n8 R1 B6 F! S some) R3 a- ?; Q( _7 A5 y
more
4 I/ b, t& f# h- P- j tokens) C m ~9 c) k4 d
Notes:
; j& Y% Z" O3 \- L Strtok(char *strToken, const char *strDelimit ) 其中,strToken 和 strDelimit 一定要用字符数组格式的.也就是说.入口只能是字符数组元素地址。
& ^' r# d7 Z" c8 P z9 J2 a 以上就是对C++ strtok的相关介绍。 |