简体   繁体   中英

C++ How to use a function for another variable?

How can I use my function output_integer for the array v, without making a new function? I need to print the values that end up in v, but i want to use the same function as i do for m:

#include <iostream>
#include <cmath>
using namespace std;

int m[10];
int v[10];
void input_integer()
{
    for (int i=0; i<10; i++)
    {
        cout<<"Element "<<i+1<<"=";
        cin>>m[i];
    }
}
void output_integer()
{
    cout<<"The values of the array are:\n";
        for (int i=0; i<10; i++)
        {
            cout<<"Element "<<i+1<<" = "<<m[i]<<"\n";
        }
}
void copy_div3()
{
    int k=0;
    for (int i=0; i<10; i++)
    {
        if (m[i]%3==0)
        {
            v[k]=m[i];
            k++;
        }
    }
}

int main()
{
    //input_integer();
    output_integer();
    copy_div3();
    return 0;
}

使output_integer将数组作为参数,以便可以将其传递给任何数组

You can change the function signature to take the array argument and print it, instead of relying on the global-ness of the variable.

void output_integer(const int (&arr)[10])
{
  cout<<"The values of the array are:\n";
  for (unsigned int i=0; i<10; i++)
  {
    cout<<"Element "<<i+1<<" = "<<arr[i]<<"\n";
  }
}

To make it more generic, you can even think of making it a template :

template<unsigned int SIZE>
void output_integer(const int (&arr)[SIZE]);

Just provide a pointer to that array and its size:

void output_integer_array(int* array, int size)
{
    cout<<"The values of the array are:\n";
        for (int i=0; i<size; i++)
        {
            cout<<"Element "<<i+1<<" = "<<array[i]<<"\n";
        }
}

usage:

output_integer_array(m, 10);// you may want to store the size as a const variable instead of a magic number

You can pass arguments to functions.

Define `output_integer like this:

void output_integer(int* array)
{
    cout<<"The values of the array are:\n";
        for (int i=0; i<10; i++)
        {
            cout<<"Element "<<i+1<<" = "<<array[i]<<"\n";
        }
}

And then call it like this:

output_integer(v);
output_integer(m);

Actually, it would probably be a good exercise to move the m and v arrays away from the global scope entirely. Define them only inside the main function so that they have to be passed as parameters to any function that needs to access them.

You need to understand the basic concept of functions: all logic should depend on parameters passed to the functions (if non-member). You have a free function (not part of the class), so if you need to print an array, you should pass that array as a parameter:

void output_integer(const int* m)
{
    cout<<"The values of the array are:\n";
        for (int i=0; i<10; i++)
        {
            cout<<"Element "<<i+1<<" = "<<m[i]<<"\n";
        }
}

Since you use c++, I also suggest using std::vector instead of arrays:

void output_integer(const std::vector<int>& m)
{
    cout<<"The values of the array are:\n";
        for (int i=0; i<10; i++)
        {
            cout<<"Element "<<i+1<<" = "<<m[i]<<"\n";
        }
}

When you're done with the changes, google for "magic parameters".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM