12.6 write a function and return a dynamically allocated int vector. This vector is passed to another function. This function reads standard input and stores the read value in the vector element. Then pass the vector to another function to print the read value. Remember to delete the vector at the right time.
#include<iostream>#include<new>#include<vector>using namespace std;vector<int>* f1(){ vector<int> *p=new vector<int>; return p;}vector<int>* f2(){ vector<int> *p=f1(); int n; while(cin>>n) p->push_back(n); return p;}int main(){ vector<int> *p=f2(); for(auto v:*p) cout<<v<<" "; delete p; return 0;}
12.7 use shared_ptr instead of built-in pointer.
#include<iostream>#include<vector>#include<memory>using namespace std;shared_ptr<vector<int>> f1(){ shared_ptr<vector<int>> p=make_shared<vector<int>>(); return p;}shared_ptr<vector<int>> f2(){ shared_ptr<vector<int>> p=f1(); int n; while(cin>>n) p->push_back(n); return p;}int main(){ shared_ptr<vector<int>> p=f2(); for(auto v:*p) cout<<v<<" "; return 0;}