[英]I'm having trouble converting a string into an array of integers
這是我迄今為止的代碼:
(假設字符串少於 10 位)
string s;
cin >> s;
int array[10];
for(int i=10, j =s.length(); i>0 && j>0; i--,j--){
array[i]=(int)s[j];
}
for(int b=10; b>0;b--){
cout << array[b];
}
謝謝你的幫助!
你可能想這樣做:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
cin >> s;
int array[10];
for(int i=s.length(); i>=0; i--)
{
array[i]=s[i]-'0';
}
for(int b=0; b<s.length();b++)
{
cout << array[b];
}
//better way
int num =stoi (s,nullptr,0); // You can convert the entire string to an int like this.
cout << endl << num << endl;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.