C++ alternative to sscanf()

2019-02-27 17:03发布

问题:

I have the following function:

static void cmd_test(char *s)
 {
    int d = maxdepth;
    sscanf(s, "%*s%d", &d);
    root_search(d);
  }

How can I achieve the same output in a C++ way, rather than using sscanf?

回答1:

int d = maxdepth;
sscanf(s, "%*s%d", &d);

Reads a string (which does not store anywhere) and then reads a decimal integer. Using streams it would be:

std::string dont_care;
int d = maxdepth;

std::istringstream stream( s );
stream >> dont_care >> d;


标签: c++ cin scanf