题解 | #数组类的构造函数#
数组类的构造函数
https://www.nowcoder.com/practice/1f36f85726474afbb103f65a24106824
#include <iostream>
#include <bits/stdc++.h>
#include <sstream>
using namespace std;
class Array {
private:
int n;//数组大小
int* a;//数组
public:
// write your code here......
Array() {
cin >> n;
cin.ignore();
string s;
getline(cin, s);
a = new int[n];
memset(a, 0, n * sizeof (int));
stringstream ss(s);
int num;
int idx = 0;
while (ss >> num) { //ss中的字符传入num中
if (idx < n) {
a[idx] = num;
idx++;
}
}
// for(string::iterator itr=s.begin();itr != s.end();++itr){
// if(isdigit(*itr)){
// *p = *itr-'0';//字符类型转换为整型 赋值
// //因为字符型变量赋值给整型,就会将字符转换为ASCILL码值给整型变量
// p++;
// }
// }
}
~Array() {
delete []a;
}
void show() {
for (int i = 0; i < n; i++) cout << a[i] << ' ';
}
};
int main() {
Array a;
a.show();
return 0;
}

