C + + Primer Fifth Edition after class
Exercise 6.32: Is the following function legal? If it is legal, explain its function, and if it is not legal, modify the error and explain why.
#include <iostream>using namespace Std;int &get (int *arry, int index) {return arry[index];} int main () { int ia[10]; for (int i = 0; I! = ten; ++i) get (ia, i) = i; return 0;}
Legal. Assigns a value to each element in the IA array. The reference type is returned, and the left value is obtained, so it can be assigned a value.
Exercise 6.33: Write a recursive function that outputs the contents of a vector object.
#include <iostream> #include <vector>using namespace Std;typedef vector<int>::iterator vec_ip;void Re_print_vector (vector<int> &vec, vec_ip ID) {if (id! = Vec.end ()) {cout << *id << Endl;re_print_vec Tor (VEC, ++id);}} int main () {vector<int> vec;for (int i = 1; i <=; ++i) vec.push_back (i); Re_print_vector (VEC, Vec.begin ()); retur n 0;}
It's too difficult to name it.
The beginning of my re_print_vector statement is (vector<int> VEC, vec_ip ID), and no reference, so the dead loop = =, later found to pass the formal parameter, then the parameter storage location and the actual parameter must be different AH =, The ID can never be equal to the formal parameter vec.end ().
Why do I think of using iterators? The direct use of digital subscript is not much simpler [sweat].
Functions (c + + primer reading notes)