Reputation: 66006
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?
Upvotes: 1
Views: 877
Reputation: 24412
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.
Upvotes: 1
Reputation: 61910
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.
Upvotes: 3