I know how to convert a character array containing numbers to an integer using iostream:
char[] ar = "1234";
int num;
ar >> num;
but how would I convert the last four characters of that array to an int?
char[] ar = "sl34nfoe11intk1234";
int num;
????;
Is there a way to point to an element in the array and start streaming from ther开发者_JAVA百科e?
Ideally I would start streaming from max array size - 4.char* p = ar + strlen(ar) - 4;
Now p
points to the '1'
of "1234"
, and you can feed p
into the stream.
char ar[] = "abc1234";
std::istringstream ss(ar + 3);
int n = 0;
ss >> n;
Better yet, use std::string
:
std::string ar("abc1234");
std::istringstream ss(ar.substr(ar.size() - 4));
What about
char[] ar = "sl34nfoe11intk1234";
int num;
(ar + strlen(ar) - 4) >> num;
精彩评论