练习12.6
using namespace std;
vector<int> *init_vector();
vector<int> *scan_vector(vector<int> *s);
void print_vector(vector<int> *s);
int main(){
auto s = init_vector();
scan_vector(s);
print_vector(s);
}
vector<int> *init_vector(){
vector<int> *s = new vector<int>;
return s;
}
vector<int> *scan_vector(vector<int> *s){
int input;
while(cin>>input){
s->push_back(input);
}
return s;
}
void print_vector(vector<int> *s){
for(out : *s){
cout << out << " ";
}
}
练习12.7
傻傻的写出了shared_ptr<vector> *,报错之后反应过来shared_ptr<vector>就已经是指针了,这里要记住shared_ptr要用make_shared初始化。
using namespace std;
shared_ptr<vector<int>> init_vector();
shared_ptr<vector<int>> scan_vector(shared_ptr<vector<int>> s);
void print_vector(shared_ptr<vector<int>> s);
int main(){
auto s = init_vector();
scan_vector(s);
print_vector(s);
}
shared_ptr<vector<int>> init_vector(){
shared_ptr<vector<int>> s = make_shared<vector<int>>() ;
return s;
}
shared_ptr<vector<int>> scan_vector(shared_ptr<vector<int>> s){
int input;
while(cin>>input){
s->push_back(input);
}
return s;
}
void print_vector(shared_ptr<vector<int>> s){
for(out : *s){
cout << out << " ";
}
}
练习12.8
最后图少一行,缺的是 return p。
一眼看出返回值类型不对。