0

0

在C++中实现strtok()函数

WBOY

WBOY

发布时间:2023-09-01 19:21:04

|

1596人浏览过

|

来源于tutorialspoint

转载

在c++中实现strtok()函数

strtok()函数是C++中最常用的函数之一。使用分隔符作为指导,该函数可以将文本分割成较小的块或标记。由于strtok()函数的存在,使用字符串在C++中变得简单。本文将对strtok()函数进行详细的讲解,包括其定义、语法、算法和各种实现策略。需要记住的是,strtok函数有一些限制和潜在的缺点。例如,它不能用于const或只读字符串,因为它会直接改变原始字符串。边缘情况和意外输入,如空字符串或在序列中重复出现的分隔符,也可能难以处理。

尽管存在这些缺点,但这个函数仍然是许多程序员的有用工具,并且经常被广泛应用于各种应用程序中,包括文本处理、数据解析和网络协议。它是一个灵活而强大的函数,可以显著简化许多常规编程任务。

Characteristics of strtok() function

To split a string into smaller chunks or tokens based on a delimiter, use the strtok() function in C++. A pointer to the string that has to be tokenized and a string with the delimiter characters are the two inputs for the function. A pointer to the string's very first token is returned by the function. As long as there are tokens left in the string, successive calls to the function with the same string parameter will return additional tokens and this happens with the help of NULL pointer in while loop. When calling the'strtok()' function again, supplying NULL as the first argument instructs the function to pick up where it left off with the previous tokenization of the same string.

When the function'strtok()' is first called with the input string as the first argument, it starts looking for the first token in the string at the beginning. When it locates the initial token, it terminates it by replacing the delimiter that comes after it with the null character (''0''). When'strtok()' is used again with a NULL pointer as the first argument, the function picks up where it left off with the previous tokenization of the string, that is, directly after the previous token. It keeps looking for the delimiter's next appearance.

立即学习C++免费学习笔记(深入)”;

语法

Char* strtok(char* str, const char* delimiters);  

Where

- `str` is a pointer to the string to be tokenized.

- `delimiters` 是一个包含分隔符字符的字符串。

算法

  • Step 1 − The `strtok()` function takes two arguments - the first argument is the input string to be tokenized and the second argument is a string containing all the delimiters that should be used to split the input string into tokens.

  • Step 2 − When the function is called for the first time, the input string is passed as the first argument, and the delimiter string is passed as the second argument.

  • 步骤 3 − 该函数在输入字符串中搜索任何一个分隔符字符的第一个出现

  • Step 4 − When it finds a delimiter character, it replaces it with a null terminator (`'

    Step 4 − When it finds a delimiter character, it replaces it with a null terminator (`'\0'`) and returns a pointer to the beginning of the first token.

    '`) and returns a pointer to the beginning of the first token.

    新快购物系统
    新快购物系统

    新快购物系统是集合目前网络所有购物系统为参考而开发,不管从速度还是安全我们都努力做到最好,此版虽为免费版但是功能齐全,无任何错误,特点有:专业的、全面的电子商务解决方案,使您可以轻松实现网上销售;自助式开放性的数据平台,为您提供充满个性化的设计空间;功能全面、操作简单的远程管理系统,让您在家中也可实现正常销售管理;严谨实用的全新商品数据库,便于查询搜索您的商品。

    下载
  • 第5步 - 下次调用该函数时,第一个参数应设置为`NULL`,而不是原始输入字符串。这告诉函数继续从上次停下的地方开始,在字符串中搜索下一个标记。

  • Step 6 − The function continues to search for delimiter characters and returns pointers to the beginning of subsequent tokens until it reaches the end of the string, at which point it returns `NULL`.

Approaches to follow

方法1 − 使用循环和strtok()函数以及单个分隔符来展示代码的程序。

方法2 - 使用循环和strtok()函数以及多个分隔符来展示代码的程序

方法一

Below is the program to show code using a loop with strtok() function and single delimeter

这个示例演示了如何使用strtok按空格对字符串进行分词。输入字符串str首先与分隔符" "一起传递给strtok。第一次调用strtok返回指向第一个标记("Hi,")的指针,将其打印到控制台。strtok继续对字符串进行分词,直到没有更多的标记,并且每个标记都打印到控制台。请注意,对于后续对strtok的调用,我们将一个空指针作为第一个参数传递,表示我们要继续对原始字符串进行分词

示例1

#include 
#include  // Include the header for strtok()
using namespace std;

int main() {
   char str[] = "Hi, this topic tells about strtok() function";
   char* ptr;
   ptr = strtok(str, " ");
   while (ptr != NULL) {
      cout << ptr << endl;
      ptr = strtok(NULL, " ");
   }
   return 0;
}

Output

Hi, 
this  
topic 
tells  
about 
strtok()  
function 

方法二

下面是使用循环和strtok()函数以多个分隔符显示代码的程序。下面是相同的程序代码。

The '|' and'' (space) delimiters are used to tokenize the string "The boy|is|standing|with his pet|lazy dog." in this example. Once with the string as the first argument and the characters '|' and'' as the second argument, the strtok() method is called twice. Following calls return the remaining tokens, the initial call returns the first token, "The." Until all tokens have been extracted, the loop keeps running.

Example-2

#include  
#include  
 
int main() { 
   char str[] = "The boy|is|standing|with his pet|lazy dog."; 
   char* token = strtok(str, "| "); 
   while (token != NULL) { 
      std::cout << token << std::endl; 
      token = strtok(NULL, "| "); 
   } 
   return 0; 
} 

Output

The 
boy 
is 
standing 
with 
his 
pet 
lazy 
dog.

结论

总之,C++的strtok函数是一个有用且有效的用于操作字符串的工具。尽管它有一些重要的限制和潜在的缺点,但它仍然是解析和处理文本数据的常见选择,并且对于任何编码人员来说都是一个有用的工具。

相关文章

c++速学教程(入门到精通)
c++速学教程(入门到精通)

c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Java编译相关教程合集
Java编译相关教程合集

本专题整合了Java编译相关教程,阅读专题下面的文章了解更多详细内容。

11

2026.01.21

C++多线程相关合集
C++多线程相关合集

本专题整合了C++多线程相关教程,阅读专题下面的的文章了解更多详细内容。

4

2026.01.21

无人机驾驶证报考 uom民用无人机综合管理平台官网
无人机驾驶证报考 uom民用无人机综合管理平台官网

无人机驾驶证(CAAC执照)报考需年满16周岁,初中以上学历,身体健康(矫正视力1.0以上,无严重疾病),且无犯罪记录。个人需通过民航局授权的训练机构报名,经理论(法规、原理)、模拟飞行、实操(GPS/姿态模式)及地面站训练后考试合格,通常15-25天拿证。

16

2026.01.21

Python多线程合集
Python多线程合集

本专题整合了Python多线程相关教程,阅读专题下面的文章了解更多详细内容。

1

2026.01.21

java多线程相关教程合集
java多线程相关教程合集

本专题整合了java多线程相关教程,阅读专题下面的文章了解更多详细内容。

4

2026.01.21

windows激活码分享 windows一键激活教程指南
windows激活码分享 windows一键激活教程指南

Windows 10/11一键激活可以通过PowerShell脚本或KMS工具实现永久或长期激活。最推荐的简便方法是打开PowerShell(管理员),运行 irm https://get.activated.win | iex 脚本,按提示选择数字激活(选项1)。其他方法包括使用HEU KMS Activator工具进行智能激活。

2

2026.01.21

excel表格操作技巧大全 表格制作excel教程
excel表格操作技巧大全 表格制作excel教程

Excel表格操作的核心技巧在于 熟练使用快捷键、数据处理函数及视图工具,如Ctrl+C/V(复制粘贴)、Alt+=(自动求和)、条件格式、数据验证及数据透视表。掌握这些可大幅提升数据分析与办公效率,实现快速录入、查找、筛选和汇总。

6

2026.01.21

毒蘑菇显卡测试网站入口 毒蘑菇测试官网volumeshader_bm
毒蘑菇显卡测试网站入口 毒蘑菇测试官网volumeshader_bm

毒蘑菇VOLUMESHADER_BM测试网站网址为https://toolwa.com/vsbm/,该平台基于WebGL技术通过渲染高复杂度三维分形图形评估设备图形处理能力,用户可通过拖动彩色物体观察画面流畅度判断GPU与CPU协同性能;测试兼容多种设备,但中低端手机易卡顿或崩溃,高端机型可能因发热降频影响表现,桌面端需启用独立显卡并使用支持WebGL的主流浏览器以确保准确结果

25

2026.01.21

github中文官网入口 github中文版官网网页进入
github中文官网入口 github中文版官网网页进入

github中文官网入口https://docs.github.com/zh/get-started,GitHub 是一种基于云的平台,可在其中存储、共享并与他人一起编写代码。 通过将代码存储在GitHub 上的“存储库”中,你可以: “展示或共享”你的工作。 持续“跟踪和管理”对代码的更改。

7

2026.01.21

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Rust 教程
Rust 教程

共28课时 | 4.6万人学习

Kotlin 教程
Kotlin 教程

共23课时 | 2.7万人学习

Go 教程
Go 教程

共32课时 | 4万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号