How to read integers elegantly using C++ stream?

后端 未结 6 1313
醉话见心
醉话见心 2021-01-02 08:52

I have a file full of lines in this format:

1 - 2: 3

I want to only load numbers using C++ streams. Whats the most elegant way to do it? I

6条回答
  •  旧时难觅i
    2021-01-02 09:23

    I would recommend doing at least cursory sanity checks when reading this:

    int a, b, c;
    char dash, colon;
    
    if (not (cin >> a >> dash >> b >> colon >> c) or dash != '-' or colon != ':')
        Failure. Do something.
    

提交回复
热议问题