C++ strtok應(yīng)用方式淺析
作者:佚名
C++ strtok的正確應(yīng)用方式是怎樣的?它在實際程序開發(fā)中起來哪些作用。下面就讓我們從一段代碼中來對此進行詳細的解讀。
在C++編程語言中,對于字符的處理,可以通過使用C++ strtok來進行具體的操作。那么正確的應(yīng)用方法我們將會在這篇文章中為大家詳細介紹,希望能對大家有所幫助,提高實際程序開發(fā)效率。
C++ strtok原形如下:
- char *strtok(
- char *strToken,
- const char *strDelimit
- );
- // crt_strtok.c
- /**//* In this program, a loop uses strtok
- * to print all the tokens (separated by commas
- * or blanks) in the string named "string".
- */
- #include < string.h>
- #include < stdio.h>
- char string[] = "A string\tof ,,tokens\nand some more tokens";
- char seps[] = " ,\t\n";
- char *token;
- int main( void )
- {
- printf( "Tokens:\n" );
- /**//* Establish string and get the first token: */
- token = strtok( string, seps );
- while( token != NULL )
- {
- /**//* While there are tokens in "string" */
- printf( " %s\n", token );
- /**//* Get next token: */
- token = strtok( NULL, seps );
- }
- }
C++ strtok輸出:
- A
- string
- of
- tokens
- and
- some
- more
- tokens
Notes:
- Strtok(char *strToken, const char *strDelimit )
其中,strToken 和 strDelimit 一定要用字符數(shù)組格式的.也就是說.入口只能是字符數(shù)組元素地址。
以上就是對C++ strtok的相關(guān)介紹。
【編輯推薦】
責(zé)任編輯:曹凱
來源:
博客園