如何將字符串的第n個字符轉換爲數字?我有一個很長的數字表示爲字符串,我想製作一個數組,每個字符都是單獨的數字。我試着用下面的一段代碼:字符串的第n個字符到int
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string str ="73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018";
int ints[1000] = {0};
for (int i = 0; i < str.size(); i++)
{
istringstream ss(str[i]);
ss >> ints[i];
}
cout << ints[9] << endl;
return 0;
}
但它不起作用。
是的,這是它。謝謝。你能解釋一下「 - '0'」是做什麼的? – user1626154
[ASCII](http://www.asciitable.com/index/asciifull.gif)中的'0'是48,而'1'是49,所以'1' - '0'是簡單的數學減法, 49 - 48',結果爲1.同樣適用於所有其他數字。 – jweyrich
@ user1626154它將數字的ASCII表示轉換爲其十進制等值。 '0'是十進制的48,所以減去'0'會給出十進制值0,其他數字也是如此。 – DaV