简体   繁体   English

存储表达式的最佳做法

[英]Best Practices for Storing an Expression

I'm trying to store an expression (operators and operands) in a container. 我正在尝试将表达式(运算符和操作数)存储在容器中。 The best implementation I can think of is storing them in a string. 我能想到的最佳实现是将它们存储在字符串中。

For example: 例如:

2 30 + 5 *

would be stored as 将存储为

"2 30 + 5 *"

However, this incur the problem that, when extracting from the string using 但是,这会产生一个问题,当使用

string tk;
string expr;
stringstream e(expr);
e >> tk;

if there're any missing space, "2 30+ 5*", the error handling part would be some kind of distracting. 如果缺少任何空间“ 2 30+ 5 *”,那么错误处理部分将使您分心。 Is there any better way to do this? 有什么更好的方法吗?

Thanks. 谢谢。

无论您需要将表达式分解成多个部分(进行令牌化),都应先执行此操作,然后再存储令牌的数组/向量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM