我有 std::string
,其值为 1T23
:
如何获得x = 1, y = 2, z = 3
;
我尝试过:
int x = std::atoi(&myString.at(0));
int y = std::atoi(&myString.at(2));
int z = std::atoi(&myString.at(3));
但它返回:x = 0, y = 23, z = 3
?
最佳答案
你可以这样做:
int x = std::stoi(myString.substr(0, 1));
int y = std::stoi(myString.substr(2, 1));
int z = std::stoi(myString.substr(3, 1));
关于c++ - 解析给定字符串中位置的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61344593/