c++ - std::string 部分转换为整数

标签 c++ string int

我有一个 std::string: 01001,我想得到每个数字:

std::string foo = "01001";
for (int i=0; i < foo.size(); ++i)
{
   int res = atoi( foo[i] );  // fail
   int res = atoi( &foo[i] ); // ok, but res = 0 in any case
}

怎么做?

最佳答案

这是我看到的最简单的方法:

std::string foo = "01001";
for (int i=0; i < foo.size(); ++i)
{
   int res = foo[i] - '0';
}

关于c++ - std::string 部分转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4728265/

相关文章:

c++ - 给定数字的最小公倍数

c++ - 我可以在 opencv 中使用哪个函数作为 matlab 中的 max()

c++ - 奇怪的 std::distance 输出

javascript - 使用 JSX 将代码渲染到页面,同时保留换行符

Java 字典搜索器

python - “float”对象不能解释为 int,但转换为 int 不会产生任何输出

c - 通过套接字c读取整数

c++ - OpenMsgStore 上的 MAPI_E_NOT_FOUND

c++ - OpenCV - cvCreateTrackbar()

c - 按位运算中的类型转换让我在 C 中感到困惑