首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::basic_istream::ignore

basic_istream& ignore( std::streamsize count = 1, int_type delim = Traits::eof() );

?

?

从输入流中提取和丢弃字符,直到包括delim...

ignore表现为UnformattedInputFunction.在构造和检查哨兵对象之后,它从流中提取字符并丢弃它们,直到出现下列任何一种情况:

  • count提取字符。在特殊情况下,此测试将被禁用。count等号std::numeric_limits<std::streamsize>::max()
  • 文件结束条件发生在输入序列中,在这种情况下,函数调用setstate(eofbit)
  • 下一个可用字符c在输入序列中是delim,由Traits::eq_int_type(Traits::to_int_type(c), delim).提取并丢弃分隔符字符。如果下列情况下,此测试将被禁用。delimTraits::eof()

参数

count

-

number of characters to extract

delim

-

delimiting character to stop the extraction at. It is also extracted.

返回值

*this...

例外

failure如果发生错误%28,则错误状态标志不是goodbit29%和exceptions()将被抛向那个州。

如果内部操作抛出异常,则会捕获该操作,并且badbit已经设定好了。如果exceptions()设置为badbit,异常将被重新抛出。

下面的示例使用ignore跳过非数字输入:

二次

代码语言:javascript
复制
#include <iostream>
#include <sstream>
#include <limits>
 
int main()
{
    std::istringstream input("1\n"
                             "some non-numeric input\n"
                             "2\n");
    for(;;) {
        int n;
        input >> n;
 
        if (input.eof() || input.bad()) {
            break;
        } else if (input.fail()) {
            input.clear(); // unset failbit
            input.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // skip bad input
        } else {
            std::cout << n << '\n';
        }
    }
}

二次

产出:

二次

代码语言:javascript
复制
1
2

二次

另见

get

extracts characters (public member function)

getline

extracts characters until the given character is found (public member function)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com