简体   繁体   English

在两个函数之间发送数组C ++

[英]Sending an array between two functions C++

I am trying to send a array of 15 integers between two functions in C++. 我正在尝试在C ++中的两个函数之间发送15个整数的数组。 The first enables the user to enter taxi IDs and the second functions allows the user to delete taxi IDs from the array. 第一个功能使用户能够输入出租车ID,第二个功能使用户能够从阵列中删除出租车ID。 However I am having an issue sending the array between the functions. 但是我在函数之间发送数组时遇到问题。

void startShift ()
{
    int array [15]; //array of 15 declared 

    for (int i = 0; i < 15; i++)
    {
        cout << "Please enter the taxis ID: ";
        cin >> array[i]; //user enters taxi IDs

        if (array[i] == 0)
            break;
    }

    cout << "Enter 0 to return to main menu: ";
    cin >> goBack;
    cout << "\n";
    if (goBack == 0)
        update();
}

void endShift ()
{
    //need the array to be sent to here

    cout << "Enter 0 to return to main menu: ";
    cin >> goBack;
    cout << "\n";
    if (goBack == 0)
        update();
}

Any help is great valued. 任何帮助都非常宝贵。 Many thanks. 非常感谢。

Since the array has been created on the stack, you would just need to pass the pointer to the first element, as an int* 由于数组是在堆栈上创建的,因此您只需要将指针作为int *传递给第一个元素即可

void endshift(int* arr)
{
int val = arr[1];
printf("val is %d", val);
}

int main(void)
{
int array[15];
array[1] = 5;
endshift(array);
}

Since the array is created on the stack, it will no longer exist once the routine in which it was created has exited. 由于数组是在堆栈上创建的,因此一旦退出创建数组的例程,该数组将不再存在。

Declare the array outside of those functions and pass it to them by reference. 在这些函数之外声明数组,并通过引用将其传递给它们。

void startShift(int (&shifts)[15]) {
 // ...
}
void endShift(int (&shifts)[15]) {
 // ...
}

int main() {
  int array[15];
  startShift(array);
  endShift(array);
}

This isn't exactly pretty syntax or all that common. 这不是完全漂亮的语法,也不是所有常见的语法。 A much more likely way to write this is to pass a pointer to the array and its length. 一种更可能的写方法是将指针传递给数组及其长度。

void startShift(int* shifts, size_t len) {
  // work with the pointer
}

int main() {
  int array[15];
  startShift(array, 15);
}

Idiomatic C++ would be different altogether and use iterators to abstract away from the container, but I suppose that is out of scope here. 惯用的C ++会完全不同,并使用迭代器从容器中抽象出来,但是我认为这超出了本文的范围。 The example anyway: 无论如何,该示例:

template<typename Iterator>
void startShift(Iterator begin, Iterator end) {
  // work with the iterators
}

int main() {
  int array[15];
  startShift(array, array + 15);
}

You also wouldn't use a raw array, but std::array . 您也不会使用原始数组,而是使用std::array

It won't work to use a local array in the startShift() function. startShift()函数中使用局部数组将startShift() You are best off to do one or more of the following: 您最好执行以下一项或多项操作:

  1. Use an array in the function calling startShift() and endShift() and pass the array to these functions, eg: 在调用startShift()endShift()的函数中使用数组,并将该数组传递给这些函数,例如:

     void startShift(int* array) { ... } void endShift(int* array) { ... } int main() { int arrray[15]; // ... startShift(array); // ... endShift(array); // ... } 
  2. Don't use built-in arrays in the first place: use std::vector<int> instead: that class automatically maintains the current size of the array. 首先不要使用内置数组:而是使用std::vector<int> :该类自动维护数组的当前大小。 You can also return it from a function altough you are probably still best off to pass the objects to the function. 您也可以从函数中返回它,尽管您最好还是将对象传递给函数。

void endShift (int* arr)
{
    arr[0] = 5;
}

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

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