如何取字符串的反面
How to take reverse of string
我有字符串
string s1="5 2 13 * +" //as prefix expression.
我想得到上面矩阵的反转。
"+ * 13 2 5"
我尝试过字符串流,但它生成了"+ * 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
另一种方式
std::string s = "5 2 13 * +";
std::forward_list<std::string> lst;
std::istringstream is( s );
for ( std::string t; std::getline( is, t, ' ' ); ) lst.push_front( t );
s.clear();
for ( std::string t : lst ) s += t + ' ';
std::cout << "s = " << s << std::endl;
或者没有std::getline
std::string s = "5 2 13 * +";
std::forward_list<std::string> lst;
std::istringstream is( s );
while ( is >> s ) lst.push_front( s );
s.clear();
for ( std::string t : lst ) s += t + ' ';
std::cout << "s = " << s << std::endl;
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 如何打开并写入一个名称取自C++中字符串的文件
- (C++) 取字符串的每个字符并将其放入数组中并获取所述数组的大小?
- 从C 中的字符串中取 /- int
- C 从字符串中取一个公式
- 字符 * 和字符串在C++中有何不同?(在描述中编写代码)
- 如何取字符串的反面
- 抓取字符串的一部分,例如 substr
- 取字节字符串的模数
- C++多个字符串抓取器(正则表达式)
- 取一个包含任意数量单词的字符串,并将这些单词存储在不同的字符串变量中
- 字符提取与字符串提取有何不同
- 从字符串中取一个字符
- 取和跳过字符串组
- 从文件中抓取值以查看它们是整型还是字符串
- 取多行输入,以逗号分隔,将每行存储到字符串数组中
- c++读取csv文件;每行取两个字符串
- 从lldb调用带有字符串参数的函数:取2
- Qt正则表达式增强正则表达式转换从字符串内部取数
- gettwindowtextw不抓取宽字符串