如何拆分字符串并将其用作C ++中的命令? [英] How to split strings and use them as commands in C++?
问题描述
我在类中定义了一些函数,用于更改或打印或读取链表中的信息:add_book(int booknum)/ print_book等等。
用户编写如下命令:添加123
我希望我的程序将字符串拆分为:stringadd和int123并运行add_book(int booknum)(即:使用add作为关键字来运行功能add_book)
我尝试过的事情:
起初我以为我们可以使用argc,argv来解决问题,但是argv总是被认为是一个字符串(char)
I have defined some functions in a class that changes or print or read information from a linked list : add_book(int booknum) / print_book and so on ..
the user writes a command like :add 123
I want my program to split the string into :string"add" and int"123" and run the add_book(int booknum)(i.e: use "add" as a keyword to run the function add_book)
What I have tried:
at first I thought we can use argc,argv to solve the problem but argv is always considered as a string (char)
推荐答案
如果命令的第二个参数预计是一个整数,你可以使用 atoi [ ^ ]将字符串转换为整数。
If the second argument of the command is expected to be an integer, you can use atoi[^] on it to convert a string to integer.
这篇关于如何拆分字符串并将其用作C ++中的命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!