<分区>
我有字符串
string s1="5 2 13 * +" //as prefix expression.
我想像这样获得上述矩阵的反转。
"+ * 13 2 5"
我已经尝试过 stringstream 但它生成了 "+ * 31 2 5"
而我丢失了我的 "13"
而我得到了 "31"
。这不好我的计算。
我该怎么办?感谢您的帮助。
<分区>
我有字符串
string s1="5 2 13 * +" //as prefix expression.
我想像这样获得上述矩阵的反转。
"+ * 13 2 5"
我已经尝试过 stringstream 但它生成了 "+ * 31 2 5"
而我丢失了我的 "13"
而我得到了 "31"
。这不好我的计算。
我该怎么办?感谢您的帮助。
最佳答案
假设你要反转的是在str中,值之间的分隔符是空白字符:
stringstream ss(str);
string answer, current;
answer.clear();
while (ss >> current) answer = current + " " + answer;
answer.erase(answer.size()-1, 1); // eliminate space in the end of answer
关于c++ - 如何取字符串的反转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20275040/