简体   繁体   English

将两个 arrays(一个字符串和一个整数)传递给 C++ 中的 function 的调用名称

[英]Calling name for passing two arrays (a string and an int) to a function in C++

I'm aware that when you write the call for your function you write it as displayArray(seasons,10) with the name of one array and its size.我知道,当您为 function 编写调用时,您将其写为 displayArray(seasons,10) 并带有一个数组的名称及其大小。 I'm stuck on how you would right the arguments to pass the two arrays listed in my code, seasons and cartoons.我不知道你将如何让 arguments 通过我的代码、季节和卡通中列出的两个 arrays。

#include<iostream>
#include<string>
#include<iomanip>

using namespace std;

void displayArray(string car[], int sea[], int size);


int main()
{
    int seasons[] = {5,10,8,2,12,7,31,9,3,4};
    string cartoon[] = { "Steven Universe","Adventure Time","Regular Show","Gravity Falls",
        "Spongebob Squarepants","Futurama","The Simpsons","Bob's Burgers","Avatar: The Last Airbender","Rick and Morty"};

    displayArray() // Error Message here
}

void displayArray(string car[], int sea[], int size)
{
    for (int x = 0; x < size; x++)
    {
        cout << "    " << car[x] << "\t\t" << sea[x] << right << endl;
    }

}

So you have to first create an array to pass your values with.所以你必须首先创建一个数组来传递你的值。 Then just pass the array.然后只需传递数组。

void function(int arr[]) {}

int arr[] = { 1, 2, 3, 4, 5 };

function(arr);

So in your code above, it should look like this:所以在你上面的代码中,它应该是这样的:

int main()
{
    int seasons[] = {5,10,8,2,12,7,31,9,3,4};
    string cartoon[] = { "Steven Universe","Adventure Time","Regular Show","Gravity Falls",
        "Spongebob Squarepants","Futurama","The Simpsons","Bob's Burgers","Avatar: The Last Airbender","Rick and Morty"};

    displayArray(cartoon, seasons, 10);
}

Hope this helps:)希望这可以帮助:)

displayArray(cartoon, seasons, 5); displayArray(卡通, 四季, 5);

This seems to work fine for me.这对我来说似乎很好。 You just pass each array in according to whichever is declared first in the function argument list.您只需根据 function 参数列表中首先声明的那个来传递每个数组。 Am I misunderstanding your question?我误解了你的问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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