c ++ istringstream()函数将字符串转换为int引发错误

ent*_*ize 0 c++ istringstream

我有一串数字.我试图使用istringstream将其作为int类型打印在字符串中的每个数字.如果将整个字符串作为参数传递给main中的转换函数,它可以正常工作但如果我通过索引传递它,则会引发错误.

如何使用索引使此代码工作以将字符串数组中的每个数字作为int打印.

这是我的代码.

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int strToNum(string str)
{
    istringstream ss(str);
    int n;
    ss>>n;
    cout<<n;
}

int main()
{
string str = "123";
for(int i=0; i<str.length(); i++)
//strToNum(str);  Works fine
strToNum(str[i]); //raises error
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*Łoś 5

str[i]是a char,而strToNum期望a string,因此类型错误.