if (strStream.bad()) { throw std::runtime_error("strStream is corruted"); } elseif (std::cin.fail()) { strStream.clear(); strStream.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::cout << "string format error" << std::endl; } else { std::cout << i << std::endl; }
std::cout << "process complete" << std::endl;
2.int to string
1 2 3 4 5 6 7 8 9 10
//int to string int num = 100; std::stringstream strStream; strStream << num << std::endl; if (strStream.bad()) { throw std::runtime_error("strStream is corruted"); } else { std::cout << strStream.str() << std::endl; }
3.分割字符串
1 2 3 4 5 6 7 8 9 10
//切分字符串 std::string srcString("hello world i love you"); std::stringstream strStream(srcString); std::string destString; while (strStream >> destString) { std::cout << destString << std::endl; } if (strStream.bad()) { throw std::runtime_error("strStream is corruted"); }