从字符串变量中逐字符读取单词
Read a word char by char from string variable
我正在开发一个小型表达式解析器,但遇到了问题:
我想把一个单词一个字符一个字符地解析成一个字符串变量。例如,如果我们在一个字符串变量中有1个PLUS/MINUS 1,我想让PLUS/MMINUS按字符读取,并存储到另一个字符串中,以便将来使用。任何帮助都将不胜感激,因为我是CPP 的新手
您可以使用std::istringstream
:的格式化输入功能
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::string orig = "1 PLUS 2"; // changed to "2" to show what goes where
std::istringstream is(orig); // put the string in an input stringstream
int a, b;
std::string word;
is >> a >> word >> b; // extract from the stringstream
std::cout << a << 'n'
<< word << 'n'
<< b << 'n';
}
输出:
1
PLUS
2
相关文章:
- 尝试一次将单个字符读取到大小不确定的数组中
- C++将 txt 数据和字符读取到 2D 数组中
- C++ UTF-8 瑞典语字符读取为 ASCII
- C++逐个字符读取二进制文件字符
- 逐个字符读取文件并使用<cctype>
- C 如何通过特定n字符读取TXT文件的值
- 如何使用4个字符读取C 的文本文件
- 使用C++中的字符读取具有文件 I/O 的文件
- 是否可以从C 中的文件中的线中的特定字符读取
- 将字符读取到数组
- 如何在 c++ 中逐个字符读取文件并通过 array_name[index] 访问它
- 如何从文本文件中读取,C++中逐个字符读取
- 如何将多个字符读取合并为std::向量
- 在 c++ 中逐个从文件字符读取
- C++ 中有多少个方法逐个字符读取字符串,以对每个字符执行所需的操作
- scanf 只能将 3 个字符读取到 5 位字符数组中
- 将行的前 X 个字符读取为字符串
- 逐个字符读取以查看它是否是数字
- 如何从字符**读取字符串
- 如何逐个字符读取文件的全部内容