Int tokenizer

后端 未结 4 1567
小蘑菇
小蘑菇 2020-12-08 16:59

I know there are string tokenizers but is there an "int tokenizer"?

For example, I want to split the string "12 34 46" and have:

4条回答
  •  萌比男神i
    2020-12-08 17:09

    Yes there is: use a stream, e.g. a stringstream:

    stringstream sstr("12 34 46");
    int i;
    while (sstr >> i)
        list.push_back(i);
    

    Alternatively, you can also use STL algorithms and/or iterator adapters combined with constructors:

    vector list = vector(istream_iterator(sstr), istream_iterator());
    

提交回复
热议问题