Вопрос

I have strings like this

10z45
9999i4a

Basically int-char-int-optionalchar

I want to do this function prototype

void process(std::string input, int &first, char &c, int &last, bool &optional)

Only thing is I'm not sure the best way to iterate over the string to extract these values. Would rather not use regex library, seems like can be done simply?

Это было полезно?

Решение

Use a string stream:

#include <sstream>
...
std::istringstream iss(input);
iss >> first >> c >> last >> optional;

If there's no final character, the value of optional won't be touched, so I'd recommend setting it to 0 beforehand.

Другие советы

Use std::istringstream, read int, char, int, then try next char:

std::istringstream is(input);
is >> first >> c >> last;
char c2;
optional = (is >> c2);

I'm not sure this is 100% what you want -but I'd do it in this way.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top