Functions (c + + primer reading notes)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.