#include <cstdlib>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
using namespace std;
int main() {
const char* foo[] = { "12", "1X", "X2" };
for (auto& i : foo) {
int temp = atoi(i);
(temp == 0) ? cout << "atoi: check string(0)\n" : cout << "atoi: " << temp << endl;
(istringstream(i) >> temp) ? cout << "istringstream::operator>>: " << temp << endl : cout << "istringstream::operator>>: error flag(0)\n";
try {
cout << "stoi: " << stoi(i) << endl;
} catch(const invalid_argument& e) {
cout << "stoi: threw " << e.what() << endl;
}
}
return 0;
}
I2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHNzdHJlYW0+CiNpbmNsdWRlIDxzdGRleGNlcHQ+CiNpbmNsdWRlIDxzdHJpbmc+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1haW4oKSB7Cgljb25zdCBjaGFyKiBmb29bXSA9IHsgIjEyIiwgIjFYIiwgIlgyIiB9OwoKCWZvciAoYXV0byYgaSA6IGZvbykgewoJCWludCB0ZW1wID0gYXRvaShpKTsKCgkJKHRlbXAgPT0gMCkgPyBjb3V0IDw8ICJhdG9pOiBjaGVjayBzdHJpbmcoMClcbiIgOiBjb3V0IDw8ICJhdG9pOiAiIDw8IHRlbXAgPDwgZW5kbDsKCgkJKGlzdHJpbmdzdHJlYW0oaSkgPj4gdGVtcCkgPyBjb3V0IDw8ICJpc3RyaW5nc3RyZWFtOjpvcGVyYXRvcj4+OiAiIDw8ICB0ZW1wIDw8IGVuZGwgOiBjb3V0IDw8ICJpc3RyaW5nc3RyZWFtOjpvcGVyYXRvcj4+OiBlcnJvciBmbGFnKDApXG4iOwoKCQl0cnkgewoJCQljb3V0IDw8ICJzdG9pOiAiIDw8IHN0b2koaSkgPDwgZW5kbDsKCQl9IGNhdGNoKGNvbnN0IGludmFsaWRfYXJndW1lbnQmIGUpIHsKCQkJY291dCA8PCAic3RvaTogdGhyZXcgIiA8PCBlLndoYXQoKSA8PCBlbmRsOwoJCX0KCX0KCXJldHVybiAwOwp9