示例:给出一行用逗号隔开的数字,要求把这串数组存到数组a
中
21,30,62,5,31
写法一:双指针写法
int n = 0;
int a[N];
void getnum(string &s)
{for (int i = 0; i < s.size(); i++){int j = i, num = 0;while (j < s.size() && isdigit(s[j]))num = num * 10 + s[j++] - '0';a[n++] = num;i = j;}
}
写法二:利用getline
与stoi
函数
void getnum()
{string s;while (getline(cin, s, ','))a[n++] = stoi(s);
}