c++ - 解析给定字符串中位置的数字

标签 c++ pointers

我有 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/

相关文章:

c++ - 使用指向存储在 vector 中的对象的指针... C++

c - 不兼容的指针错误,会计代码计算器

c++ - 在 Arduino 中编程时避免使用指针和#defines?

c++ - 当像静态变量一样访问时,类的非静态成员的地址将地址打印为 1

c++ - 为什么设置为 sockaddr_un::sun_path[0] 的文件名要偏移 1?

c++ - boost ASIO SSL 收到的字节数

c++ - 类继承不安全

c++ - boost Asio 和 OpenSSL 1.1.0

c++ - 内存在单行函数调用中损坏?

c++ - 简单的 "catching"逻辑