我怎样才能按引用传递的数组在C ++函数? [英] How can I pass an array by reference to a function in C++?

查看:108
本文介绍了我怎样才能按引用传递的数组在C ++函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下计划,使两个变量是按引用传递到他们的价值会根据外部因素正在返回之前确定一个函数main()以便它们可以由其它功能一起使用。我想传递的第一个变量是一个 INT ,那去罚款,但对方是一个字符串数组,这是造成我一些问题。

我已经做了足够的研究,这要知道,你不能有一个数组或引用(虽然我还没有弄清楚为什么),我想知道是否有人能帮助我弄清楚如何做到这一点?我已经试过各种方法都导致了分段错误

NB :下code已被按值传递数组,因为我只是不知道要为它写

更新:我需要使用数组我的课程。其他一些数据结构,如矢量已暗示,将是巨大的,但我必须使用特定的结构。

 无效initialise_existing_devices(INT和放大器; no_of_existing_devices,串existing_devices [100]);诠释的main()
{
    INT no_of_existing_devices = 0;
    串existing_devices [100];    initialise_existing_devices(no_of_existing_devices,existing_devices [100]);
}无效initialise_existing_devices(INT和放大器; no_of_existing_devices,串existing_devices [100])
{
    串线;
    ifstream的DEVICELIST;
    DeviceList.open(设备/ device_list);
    而(真)
    {
        函数getline(DEVICELIST,行的'\\ n');
        如果(DeviceList.eof())
        {
            打破;
        }
        ++ no_of_existing_devices;
    }
    DeviceList.close();    DeviceList.open(设备/ device_list);
    的for(int i = 0; I< no_of_existing_devices;我++)
    {
        函数getline(DEVICELIST,行的'\\ n');
        existing_devices [I] =行;
    }
}


解决方案

对一个数组的引用如下:

 无效F(标准::字符串(安培;一)[N]){}

其中, A 是参数和 N 的名称数组中元素的个数。


然而,一般的在C ++中你不通过引用传递数组(即可;它只是不常见)。其他选项包括:


  • 传递的指针数组的初始元件;在这种情况下,考虑通过该阵列作为第二个参数的函数的大小


  • 使用的std ::矢量<标准::字符串> 的std ::阵列<的std ::字符串,N&GT ; 代替,并通过引用传递(也可以找到提振阵列伪容器;禁止说,考虑写自己如果看看升压源$ C ​​$ C,这是相当简单明了)。


  • 传递一对迭代器(begin和end)的功能和使用它们来操作的范围内。


最后一个选项是最地道的C ++方式;它也是最通用的,因为你可以使用任何类型的容器,包括数组,标准库容器或容器,你自己写的。


因为实际上要使用的参数作为out参数,它可能只是为了更好地返回的std ::矢量<串> 的std ::阵列<字符串,100〕包含结果;这是干净多了。

I have the following program where two variables are to be passed by reference to a function where their values will be determined based on external factors before being returned to main() so that they can be used by other functions. The first variable I am trying to pass is an int, and that goes fine, but the other is an array of strings, which is causing me some problems.

I've done enough research into this to know that you can't have an array or references (though I've yet to figure out why) and I was wondering if anyone could help me figure out how to do this? The various methods I've tried have all resulted in segmentation faults.

NB: The code below has the array being passed by value since I just don't know what to write for it.

Update: I'm required to use an array for my coursework. Some other data structure, such as the vector that has been suggested, would be great, but I have to use specific structures.

void initialise_existing_devices(int& no_of_existing_devices, string existing_devices[100]);

int main()
{
    int no_of_existing_devices = 0;
    string existing_devices[100];

    initialise_existing_devices(no_of_existing_devices, existing_devices[100]);
}

void initialise_existing_devices(int& no_of_existing_devices, string existing_devices[100])
{
    string line;
    ifstream DeviceList;
    DeviceList.open("devices/device_list");
    while (true)
    {
        getline(DeviceList, line, '\n');
        if (DeviceList.eof())
        {
            break;
        }
        ++ no_of_existing_devices;
    }
    DeviceList.close();

    DeviceList.open("devices/device_list");
    for (int i = 0; i < no_of_existing_devices; i ++)
    {
        getline(DeviceList, line, '\n');
        existing_devices[i] = line;
    }
}

解决方案

A reference to an array looks like:

void f(std::string (&a)[N]) { }

where a is the name of the parameter and N is the number of elements in the array.


However, usually in C++ you don't pass an array by reference (you can; it's just not common). Other options include:

  • Pass a pointer to the initial element of the array; in this case, consider passing the size of the array as a second argument to the function.

  • Use a std::vector<std::string> or a std::array<std::string, N> instead and pass it by reference (you can also find the array psuedo-container in Boost; barring that, consider writing your own. If you take a look at the Boost source code, it's quite simple and straightforward).

  • Pass a pair of iterators (begin and end) to the function and use them to manipulate the range.

The last option is the most idiomatic C++ approach; it is also the most generic because you can use any type of container, including arrays, standard library containers, or containers that you've written yourself.


Since you are actually trying to use the parameter as an "out" parameter, it's probably better just to return a std::vector<string> or a std::array<string, 100> containing the results; this is much cleaner.

这篇关于我怎样才能按引用传递的数组在C ++函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆