读取输入流而不提取

Reading an input stream without extracting

本文关键字:提取 输入流 读取      更新时间:2023-10-16

我试图将输入流中的字符数计算到第一个非数字,而无需实际从流中提取字符。 输入可以包含任意数量的字符。 我需要这样做来确定在将数组中的流中的数字存储之前是否应该增长动态数组。

我仅限于以下库:iostream,cstring,cctype

我在想这样的事情:

int counter = 0;
const char * s = cin.getline();
while( s[counter] <= '0' || s[counter] >= '9' )
{
   counter++;
}

但是我很难让它工作。 任何建议将不胜感激!

尝试类似操作:

std::istream::pos_type start = is.tellg();
while (std::isdigit((is >> std::ws).peek()) && is.ignore())
    ;
counter = in.tellg();
is.seekg(start, std::ios_base::beg);

在这种情况下,空格算作非数字字符吗?如果是这样,请从丢弃前导空格的while()循环中删除第二行。