C ++字符串终结符 [英] C++ String Terminator

查看:542
本文介绍了C ++字符串终结符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

:))

我知道C ++字符串不会像C字符串那样以NULL终结符终止,所以 终止了它们用?我正在制作一个计算器,它通过分离数字和运算符并按照操作顺序将它们放入列表来解析表达式。当我解析表达式时,我使用 for 循环遍历字符串,并针对switch语句测试每个字符。如果字符是数字(0-9),则将其添加到单独的字符串中,当交换机到达运算符时,它会将单独的字符串转储到列表中并重新开始。问题是得到最后一个数字,因为之后没有运算符。想想22 + 33,33之后没有什么可以测试开关,因此,字符串永远不会被转储到列表中。我可以用任何方式测试字符串的结尾,或者任何人有不同的想法来完成整个过程吗?


谢谢,

奥斯汀

Hi, : )
I know that C++ Strings are not terminated with a NULL terminator like C strings, so what are they terminated with? I''m making a calculator currently that parses an expression by separating the numbers and operators and putting them in a list with accordance to the order of operations. When I parse the expression, I go through the string with a for loop, and test each character against a switch statement. If the character is a number (0-9), then its added to a separate string, when the switch reaches an operator, it dumps the separate string into the list and starts over. The problem is getting the last number, because there is no operator after it. Think "22+33", there is nothing after 33 to test the switch against, and thus, the string never gets dumped into the list. Any way I can test for the end of the string, or anyone got a different idea to go about this whole process?

Thanks,
Austen

推荐答案

:)

我知道C ++字符串不会像C字符串那样用NULL终结符终止,那么它们终止了吗?我正在制作一个计算器,它通过分离数字和运算符并按照操作顺序将它们放入列表来解析表达式。当我解析表达式时,我使用 for 循环遍历字符串,并针对switch语句测试每个字符。如果字符是数字(0-9),则将其添加到单独的字符串中,当交换机到达运算符时,它会将单独的字符串转储到列表中并重新开始。问题是得到最后一个数字,因为之后没有运算符。想想22 + 33,33之后没有什么可以测试开关,因此,字符串永远不会被转储到列表中。我可以用任何方式测试字符串的结尾,或者任何人有不同的想法来完成整个过程吗?


谢谢,

Austen
Hi, : )
I know that C++ Strings are not terminated with a NULL terminator like C strings, so what are they terminated with? I''m making a calculator currently that parses an expression by separating the numbers and operators and putting them in a list with accordance to the order of operations. When I parse the expression, I go through the string with a for loop, and test each character against a switch statement. If the character is a number (0-9), then its added to a separate string, when the switch reaches an operator, it dumps the separate string into the list and starts over. The problem is getting the last number, because there is no operator after it. Think "22+33", there is nothing after 33 to test the switch against, and thus, the string never gets dumped into the list. Any way I can test for the end of the string, or anyone got a different idea to go about this whole process?

Thanks,
Austen



如果在字符串的末尾需要一个''\ 0''字符,你甚至可以模仿它:

If you need a ''\0'' character at the end of the string you can even mimic it:

展开 | 选择 | < span class =codeLinkonclick =WordWrap(this);> Wrap | 行号


即使使用c ++字符串thr也是一个空终止....因为你可以通过字符串makng迭代一个条件你继续,除非你遇到NULL。 ..


ie,
even with c++ strings thr is a null termination....cause u can iterate thru the string makng a condition tht u continue unless u meet the NULL...

i.e.,
展开 | 选择 | Wrap | 行号



即使使用c ++字符串thr也是一个空终止....因为你可以通过字符串makng迭代你的条件继续,除非你遇到NULL ...
even with c++ strings thr is a null termination....cause u can iterate thru the string makng a condition tht u continue unless u meet the NULL...



这不是真的;尝试at()成员函数而不是operator []并自己查看

。 (at()执行范围检查,operator []简单地执行berzerk)。


亲切的问候,


Jos

That is not true; try the at() member function instead of operator[] and see for
yourself. (at() performs a range check, operator[] simply goes berzerk).

kind regards,

Jos


这篇关于C ++字符串终结符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆