C++如何将一个函数用于另一个变量?

发布于 2024-12-10 11:29:43 字数 811 浏览 1 评论 0原文

如何在不创建新函数的情况下将函数 output_integer 用于数组 v?我需要打印 v 中最终的值,但我想使用与 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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

缱倦旧时光 2024-12-17 11:29:43

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

Make the output_integer to take the array as parameter, so that you can pass it any array

剩余の解释 2024-12-17 11:29:43

您可以更改函数签名以获取数组参数并打印它,而不是依赖变量的全局性。

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";
  }
}

为了使其更通用,您甚至可以考虑将其设为模板

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

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]);
可是我不能没有你 2024-12-17 11:29:43

只需提供指向该数组及其大小的指针:

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";
        }
}

用法:

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

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
各自安好 2024-12-17 11:29:43

您可以将参数传递给函数。

像这样定义 `output_integer:

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";
        }
}

然后像这样调用它:

output_integer(v);
output_integer(m);

实际上,将 mv 数组完全移出全局范围可能是一个很好的练习。仅在 main 函数内定义它们,以便将它们作为参数传递给需要访问它们的任何函数。

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.

ぃ双果 2024-12-17 11:29:43

您需要了解函数的基本概念:所有逻辑都应依赖于传递给函数的参数(如果是非成员)。你有一个自由函数(不是类的一部分),所以如果你需要打印一个数组,你应该将该数组作为参数传递:

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";
        }
}

由于你使用c++,我还建议使用std::vector 而不是数组:

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";
        }
}

完成更改后,用 google 搜索“神奇参数”。

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文