开发者

Ignore values from stringstream (like %*f in sscanf)

I'm trying to get rid of old unsafe C functions, including sscanf(). Right now I'm using

#include <sstream>
std::string str = "111 222.2 333 444.4 555";
std::stringstream sstr(str);
int i, j, k;
float dummy1, dummy2;
sstr >> i >> dummy1 >> j >> dummy2 >> k;  

I just need the intege开发者_运维百科rs from that. Is there any way to avoid those nasty dummy variables?

Thanks in advance and have a nice day!


sstr.ignore(128,' ');

Ignore until next space, or until 128 characters were read.


You can get by with a single dummy (per type):

std::string str("111 222.2 333 444.4 555");
std::stringstream sstr(str);
int i, j, k;
float dummy;
sstr >> i >> dummy >> j >> dummy >> k;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜