我想製作一個程序,它會讀取字符串格式的一些數字並輸出,如下所示:如果數字是12345,它應該輸出12 23 34 45。我嘗試從C++字符串庫中使用substr()函數,但它給了我奇怪的結果 - 它輸出1 23 345 45而不是預期的結果。爲什麼?如何使用string.substr()函數?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main(void)
{
string a;
cin >> a;
string b;
int c;
for(int i=0;i<a.size()-1;++i)
{
b = a.substr(i,i+1);
c = atoi(b.c_str());
cout << c << " ";
}
cout << endl;
return 0;
}
[不應該使用'atoi'](http://stackoverflow.com/q/17710018/995714) – 2017-03-04 10:53:18