Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;引用数组_C++_Arrays_Reference_Pass By Reference - Fatal编程技术网

C++ c++;引用数组

C++ c++;引用数组,c++,arrays,reference,pass-by-reference,C++,Arrays,Reference,Pass By Reference,我想知道我怎样才能使这个代码工作 #include <iostream> using namespace std; void writeTable(int (&tab)[],int x){ for(int i=0;i<x;i++){ cout << "Enter value " << i+1 <<endl; cin >> tab[i] ; } } int main(vo

我想知道我怎样才能使这个代码工作

#include <iostream>
using namespace std;

void writeTable(int (&tab)[],int x){
    for(int i=0;i<x;i++){
        cout << "Enter value " << i+1 <<endl;
        cin >> tab[i] ;
    }
}


int main(void){
    int howMany;
    cout << "How many elemets" << endl;
    cin >> howMany;

    int table[howMany];
    int (&ref)[howMany]=table;
    writeTable(ref,howMany);
    return 0;
}

感谢您的帮助

如果您想传递数组的大小,请删除引用

void f(int a[])
相当于

void f(int* a)
因此,如果担心这一点,就不会进行复制

如果要通过引用获取数组,则必须指定维度。e、 g

void f(int (&a)[10])

当然,这两种方法中最好的是第三种方法,即使用std::vector,并根据需要通过引用、引用const或通过值传递它们。HTH

引用数组是非法的,如果您正试图这样做的话。从标题中我看不是100%清楚。

如果将
writeTable
设置为函数模板,则无需指定数组的维度

template <typename T,size_t N>
void writeTable(T (&tab)[N]) //Template argument deduction
{
    for(int i=0 ; i<N ; i++){
       // code ....
    }
}

这里有一个稍微多的C++风格:

#include <iostream>
#include <vector>

void writeTable(std::vector<int> &tab)
{
    int val;

    for (unsigned int i=0; i<tab.size(); i++)
    {
        std::cout << "Enter value " << i+1 << std::endl;
        if (std::cin >> val)
        {
            tab[i] = val;
        }
    }
}


int main()
{
    int howMany;
    std::cout << "How many elements?" << std::endl;
    std::cin >> howMany;

    std::vector<int> table(howMany);
    writeTable(table);

    return 0;
}
#包括
#包括
void writeTable(std::vector和tab)
{
int-val;

对于(unsigned int i=0;i遵循Amardeep的答案,这里有一个C++11方法:

#include <iostream>
#include <vector>

void writeTable(std::vector<int> &tab)
{
    int val;

    for (auto& cell : tab)
    {
        std::cout << "Enter value " << i+1 << std::endl;
        if (std::cin >> val)
        {
            cell = val;
        }
    }
}


int main()
{
    int howMany;
    std::cout << "How many elements?" << std::endl;
    std::cin >> howMany;

    std::vector<int> table(howMany);
    writeTable(table);

    return 0;
}
#包括
#包括
void writeTable(std::vector和tab)
{
int-val;
用于(自动单元格:选项卡(&C)
{


cout-virtual-1:显然,他试图创建一个数组的引用,而不是references@Armen-语法太混乱了,我不同意。很可能你的答案是他需要的。@Steve:如果你看看代码(更不用说语法了),很明显,他并没有试图创建一个数组references@Armen-我年龄越大,对我来说就越明显me@Steve:我的青春是我的敌人:)但请注意,在这种情况下,您将无法将动态分配的数组传递给此函数。@Armen:是的!他为什么需要动态分配的数组(他将使用
std::vector
)?我的评论是针对OP的,不是针对你:)同意,传递指向数组的指针是这里的方法。好的,thx需要帮助,但我在第三个选项中还有一个问题,我可以使用变量传递维度?如果是这样,调用该函数及其原型会是什么样子?@Artur:请参阅Prasson Saurav的答案和我对它的评论。或者你认为呢表示向量的意思?对于向量,请参见Amardeep的答案:)@Artur-为什么要通过引用获取数组?与传递指针的更简单方法相比,您在这里试图实现什么?我必须使用引用来实现这一点,我不能使用任何其他技术,例如向量等,现在我知道如何做得更多。Thx寻求帮助和解释ns;)+1,但更标准/可移植的方法是将i的类型声明为vector::size_type@Armen:我通常使用size\u t,但设置了lazness。我不知道vector::size\u类型。谢谢你提供的信息。@Amardeph:通常vector::size\u类型的类型定义为size\u t,而size\u类型的类型定义通常为unsigned int。但是没有guarantees@Amardeep-+1-您还可以显示向量迭代器的替代品vs
选项卡[i]
@Steve:我保留数字循环计数器的唯一原因是因为原始功能在每次读取时都显示索引号。否则,每次读取一个for\u或while会更好。谢谢。
#include <iostream>
#include <vector>

void writeTable(std::vector<int> &tab)
{
    int val;

    for (unsigned int i=0; i<tab.size(); i++)
    {
        std::cout << "Enter value " << i+1 << std::endl;
        if (std::cin >> val)
        {
            tab[i] = val;
        }
    }
}


int main()
{
    int howMany;
    std::cout << "How many elements?" << std::endl;
    std::cin >> howMany;

    std::vector<int> table(howMany);
    writeTable(table);

    return 0;
}
#include <iostream>
#include <vector>

void writeTable(std::vector<int> &tab)
{
    int val;

    for (auto& cell : tab)
    {
        std::cout << "Enter value " << i+1 << std::endl;
        if (std::cin >> val)
        {
            cell = val;
        }
    }
}


int main()
{
    int howMany;
    std::cout << "How many elements?" << std::endl;
    std::cin >> howMany;

    std::vector<int> table(howMany);
    writeTable(table);

    return 0;
}