Dynamically assigned vector pointer
Vector <int> * get_num (int n) {vector <int> * Pv = new vector <int> (n + 1); int I, X; for (I = 0; I <n; I ++) {CIN> X; <span style = "background-color: RGB (255, 0, 0);"> (* PV) [I] = (x); </span>} return PV ;}
If it is a dynamically allocated vector pointer, you can only use the array method (the above Code is red) when inserting elements into the vector. the push_back () function has an error. Why?
Use the shared_ptr vector pointer
Shared_ptr <vector <int> get_num (int n) {shared_ptr <vector <int> Pv = make_shared <vector <int> (); int I, X; for (I = 0; I <n; I ++) {CIN> X; <span style = "color: # ff0000;"> (* PV ). push_back (x); </span>} return PV ;}
If the shared_ptr vector pointer is used, only the elements inserted in the vector can be used. push_back method (the above Code is red). If the array insertion method is used, an error will occur, which is exactly the opposite of the dynamically allocated vector pointer. Why?
Zookeeper