Use a std::stringstream
:
#include <iostream>
#include <sstream>
int main() {
std::stringstream ss;
int arr[] = {60, 321, 5};
for (unsigned i = 0; i < sizeof arr / sizeof arr [0]; ++i)
ss << arr [i];
int result;
ss >> result;
std::cout << result; //603215
}
Note that in C++11 that mildly ugly loop can be replaced with this:
for (int i : arr)
ss << i;
Also, seeing as how there is a good possibility of overflow, the string form of the number can be accessed with ss.str()
. To get around overflow, it might be easier working with that than trying to cram it into an integer. Negative values should be taken into consideration, too, as this will only work (and make sense) if the first value is negative.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…