- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 istringstream
逐字读取 string
。但是,当我的条件失败时,我需要能够将 istringstream
恢复到读取前一个单词之前。我的示例代码有效,但我想知道是否有更直接的方法来使用流来完成此操作。
std::string str("my string");
std::istringstream iss(str);
std::ostringstream ossBackup << iss.rdbuf(); // Writes contents of buffer and in the process changes the buffer
std::string strBackup(ossBackup.str()); // Buffer has been saved as string
iss.str(strBackup); // Use string to restore iss's buffer
iss.clear(); // Clear error states
iss >> word; // Now that I have a backup read the 1st word ("my" was read)
// Revert the `istringstream` to before the previous word was read.
iss.str(strBackup); // Restore iss to before last word was read
iss.clear(); // Clear error states
iss >> word; // "my" was read again
最佳答案
如果您愿意,您可以使用tellg()
和seekg()
来保存和恢复您的位置:
#include <string>
#include <sstream>
int main()
{
std::istringstream iss("some text");
std::string word;
// save the position
std::streampos pos = iss.tellg();
// read a word
if(iss >> word)
std::cout << word << '\n';
iss.clear(); // clear eof or other errors
iss.seekg(pos); // move to saved position
while(iss >> word)
std::cout << word << '\n';
}
关于c++ - 如何保存和恢复 std::istringstream 的缓冲区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26222332/
以下是我的代码的简化版本: #include #include #include int main() { std::istringstream f; f.clear();
我想在检测到某个子字符串后输出每一行。不知何故,我得到了 iss 的五倍输出。这是我的代码: //load all data from txt string data;
当我尝试使用 istringstream 从输入中提取有效数字时,我从 istringstream 得到以下错误行为: 例如: void extract(void) { double x; strin
由于使用 atoi 和 atof,我正在尝试修复 Coverity 关于污染值的一些发现。我切换到 istringstream,但它没有为 10 以外的基产生预期结果。 如果我切换到 16 进制,输入
我无法让 istringstream 在如下所示的 while 循环中继续。数据文件也如下所示。我使用输入文件中的 getline 获取第一行并将其放入 istringstream lineStrea
我正在尝试编写一个 C++ opengl 应用程序来加载 .OBJ 文件,我使用这段代码来读取顶点: char buf[256]; while(!objFile.eof()) { objFil
我正在使用 istringstream 来分解字符串的标记。假设我有这样一个字符串: print "this is a test" 它将它分成 5 个标记: print | "this | is |
我正在使用以下代码将输入字符串转换为 float : template T parseString(const std::string &s) { T val; std::istri
我遇到一个问题,istringstream 没有存储它读取的值。这是我所拥有的: if(inputFile.good()){
我有这段代码: if(flag == 0) { // converting string value to integer istringstream(temp) >> value ; value =
所以我从文件中读出行,然后通过 stringstream 读出这些行。 我发现问题是由于行的格式很少将 2 个单独的部分写在一起并作为一个字符串一起读取。我试图通过将错误的读取值放回流中并再次读取来解
我有一串数字。我正在尝试使用 istringstream 将其打印为字符串中每个单个数字的 int 类型。如果将整个字符串作为参数传递给 main 中的转换函数,它工作正常,但如果我按索引传递它,则会
我有一个代码,用于读取行中存储有 float 的文件,如下所示:“3.34|2.3409|1.0001|...|1.1|”。我想使用 istringstream 阅读它们,但它并不像我期望的那样工作:
这个: #include #include #include using namespace std; int main (void) { istringstream iss("123
我有这样的输入:(50.1003781N, 14.3925125E) 存储在 const string & input 中。我想要做的是提取括号,获取代表 GPS 坐标的数字 - 50.1003781
我有一个文件: a 0 0 b 1 1 c 3 4 d 5 6 使用istringstream,我需要得到a,然后是b,然后是c,等等。但是我不知道该怎么做,因为网上和我的书中都没有很好的例子。 到目
因此 istringstream 在初始化时复制字符串的内容,例如 string moo("one two three four"); istringstream iss(moo.c_str());
在尝试用我的代码回答另一个问题时,我发现以下代码无法编译 #include #include #include #include using namespace std; // (main o
您好,我想问一下如何从一个字符串中解析多个由“/”和空格分隔的 float 。 文件中的文本格式为“f 1/1/1 2/2/2 3/3/3 4/4/4”我需要将这行文本中的每个整数解析为几个 int
#include #include #include #include #include void reverse_words(const std::string &file) {
我是一名优秀的程序员,十分优秀!