Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.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++_Arrays_Function - Fatal编程技术网

C++ 如何使填充数组的函数发送到函数?而不是其参数中的数组

C++ 如何使填充数组的函数发送到函数?而不是其参数中的数组,c++,arrays,function,C++,Arrays,Function,如何创建一个函数来填充发送给函数的原始数组,而不是函数中唯一的数组?到目前为止,在我的代码中,我是手动操作的,但我想创建一个函数来存储值并对数组进行排序。我不知道如何将其应用于原始数组,而不是函数参数中定义的数组 #include <iostream> #include <ctime> #include <cstdlib> #include <string> using namespace std; class Functions { priv

如何创建一个函数来填充发送给函数的原始数组,而不是函数中唯一的数组?到目前为止,在我的代码中,我是手动操作的,但我想创建一个函数来存储值并对数组进行排序。我不知道如何将其应用于原始数组,而不是函数参数中定义的数组

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>

using namespace std;

class Functions
{
private:
    int input{};
    int numRows{};
    int numColumns{};
    int holder{};

public:
    void rndArrayMaxNumber (int x, int y)
    {
        int tempArray [x][y]{};
        srand(time(0));

        for (int j=0;j<x;j++)
        {
            for (int i=0;i<y;i++)
            {
                tempArray[j][i]= (rand()%99)+1;
            }
        }

        for (int j=0;j<x;j++)
        {
            for (int i=0;i<x-1;i++)
            {
                for (int k=0;k<y-1;k++)
                {
                    if (tempArray[i][k] < tempArray[i][k+1])
                    {
                        holder=tempArray[i][k];
                        tempArray[i][k]=tempArray[i][k+1];
                        tempArray[i][k+1]=holder;
                    }
                }

            }
        }

        for (int j=0;j<y;j++)
        {
            for (int i=0;i<y-1;i++)
            {
                for (int k=0;k<x-1;k++)
                {
                    if (tempArray[k][i] < tempArray[k+1][i])
                    {
                        holder=tempArray[k][i];
                        tempArray[k][i]=tempArray[k+1][i];
                        tempArray[k+1][i]=holder;
                    }
                }

            }
        }

        for (int i=0;i<y;i++)
        {
            for (int k=0;k<x;k++)
            {
                cout << tempArray[i][k] << "\t";
            }
            cout << endl;
        }
        cout << endl << "The greatest number is " << tempArray[0][0];
    }

    void arrayChoice ()
    {
        cout << "Enter the number of rows: ";
        cin >> numRows;
        cout << "Enter the number of columns: ";
        cin >> numColumns;
        cout << endl;
    }

    void menu ()
    {
        while (input!=7)
        {
            cout << "1. 2D array random numbers and show highest number" << endl;
            cout << "2. Exit" << endl << endl;
            cout << "Enter the number of the menu option you want to proceed with: ";
            cin >> input;
            cout << endl;
            switch (input)
            {
                case 1:
                    arrayChoice();
                    rndArrayMaxNumber(numRows, numColumns);
                    cout << endl << endl;
                    break;
            }
        }
    }
};

int main()
{
    Functions program;
    program.menu();
    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
类函数
{
私人:
int输入{};
int numRows{};
int numColumns{};
int持有人{};
公众:
无效rndArrayMaxNumber(整数x,整数y)
{
int tempArray[x][y]{};
srand(时间(0));

对于(intj=0;j有几种方法可以解决这个问题

您可以使用头中定义的begin()和end()函数。这些函数返回数组中第一个指针和最后一个指针之后的一个指针。这允许您执行以下操作

void arr(int *b, int *e)
{
        /*code that reads/changes array i.e b[1] = 1 b[3] = 2 etc.*/
}
int main() {
        int a[10]; 
        arr(begin(a), end(a)); 
}
另一种方法是将指针与数组的大小一起传递到数组中的第一个元素

void arr(int *b, size_t sz)
{
        /*code that reads/changes array */
}
int main() {     
        int a[10]; 
        arr(a, 10); 
}
您还可以通过引用或指向函数的指针传递数组,如下所示

void array\u函数(T(*指向数组的指针)[n])

其中,
T
表示类型,
n
表示数组的大小。 因此,传递大小为10的int类型数组如下所示:

inta[10];

array_函数(&a);

从这里,您可以通过使用deference和subscript操作符访问函数体中数组中的元素:

*指向数组[元素]的指针
对函数中变量所做的更改反映在数组
a

您还可以通过引用传递数组:

void array\u函数(T(&reference\u to\u array)[n])

数组以类似的方式传递给函数,但没有运算符
&

inta[10];

array_函数(a);

可以使用参数名和下标运算符在函数体中访问数组及其元素:

引用到数组[元素]


<> P> >对代码< >参考文献>数组> <代码>也反映在<代码> A/<代码>

< P>数组不能可靠地动态地调整大小,因为C++程序要求在编译程序时知道所有数组的大小。一些编译器允许动态大小数组作为扩展,但由于所有事物都是非标准的,支持。不能指望。如果不是出于可移植性的原因,最好避免使用可变长度数组。还有很多其他原因()

下一个棘手的问题是,当传递数组时,除了第一个维度以外的所有维度都必须在编译时知道。这使得如果编译器允许创建动态大小的2D数组,就不可能传递它们

好的,那么你会怎么做呢?通常的解决方法是

std::vector<std::vector<int>> array_2d(x, std::vector<int>(y));
不需要传递
x
,并且
y
向量
知道它有多大

这很简单,而且可以自我维护。这可能就是您所需要的。如果您正在编写高性能代码,您可能不会问这个问题或阅读这个答案

也就是说,它有一些性能缺陷,因为单个
向量
保证是一个连续的内存块,而
向量
则不是。这注定了程序会在内存中四处追逐指针,并且容易出现糟糕的缓存行为。每次程序都必须离开CPU和高速cac他想从RAM(或者上帝禁止,一个交换文件)中取出一些东西。你离开了一个运行在几千兆赫的环境,换成了一个运行在几百兆赫甚至更糟的环境

更多关于这会有多糟糕的信息:

加上每一次分配都有成本。一次大的分配的成本远低于1个
vector
大小
x
x
vector
s大小
y

因此,我推荐一个基于单个
向量的简单矩阵类

#include <iostream>
#include <vector>


// template because if you are going to write this, why restrict yourself to just int?
template<class TYPE>
class Matrix
{
private:
    // good to know how big you are
    size_t rows, columns;
    // and the vector containing the data 
    std::vector<TYPE> matrix;
public:
    Matrix(size_t numrows, size_t numcols) :
            rows(numrows), // remember the size
            columns(numcols), 
            matrix(rows * columns) // and allocate storage
    {
    }

    // does the same thing as the other constructor, except it sets a default value
    Matrix(size_t numrows, size_t numcols, TYPE init) :
            rows(numrows), columns(numcols), matrix(rows * columns, init)
    {
    }

    // gets the value at row, column and allows it to be modified        
    TYPE & operator()(size_t row, size_t column)
    {
        // check bounds here if you want
        // note the indexing math mapping 2 dimensions into 1
        return matrix[row * columns + column];
    }

    // gets a copy of the the value at row, column
    TYPE operator()(size_t row, size_t column) const
    {
        return matrix[row * columns + column];
    }

    // obvious what the next two methods do
    size_t getRows() const
    {
        return rows;
    }
    size_t getColumns() const
    {
        return columns;
    }

};

// handy dandy output helper.
friend std::ostream & operator<<(std::ostream & out, const Matrix & in)
{
    for (int i = 0; i < in.getRows(); i++)
    {
        for (int j = 0; j < in.getColumns(); j++)
        {
            out << in(i, j) << ' ';
        }
        out << '\n';
    }

    return out;
}
#包括
#包括
//模板,因为如果你要写这个,为什么要限制自己只写int?
模板
类矩阵
{
私人:
//很高兴知道你有多大
行、列的大小;
//和包含数据的向量
向量矩阵;
公众:
矩阵(大小numrows、大小numcols):
行(numrows),//记住大小
列(numcols),
矩阵(行*列)//并分配存储
{
}
//除了设置默认值之外,它与其他构造函数执行相同的操作
矩阵(大小numrows、大小numcols、类型init):
行(numrows)、列(numcols)、矩阵(行*列,init)
{
}
//获取行、列的值,并允许对其进行修改
类型和运算符()(大小行、大小列)
{
//如果需要,请检查此处的边界
//请注意,索引数学将2个维度映射为1
返回矩阵[行*列+列];
}
//获取行、列处的值的副本
类型运算符()(大小行、大小列)常量
{
返回矩阵[行*列+列];
}
//接下来的两种方法的作用显而易见
大小\u t getRows()常量
{
返回行;
}
大小\u t getColumns()常量
{
返回列;
}
};
//方便的输出助手。

friend std::ostream&Operator什么样的数组?
std::array
?或者指针数组?或者什么?显示您目前拥有的内容。一个规则的整数数组,因此我相信std::array请查看如何编辑您的问题!
int A[10]
std::array
是两个独立的东西。像我的第一个示例中那样传递一个C数组并不能达到您认为的效果。
#include <iostream>
#include <vector>


// template because if you are going to write this, why restrict yourself to just int?
template<class TYPE>
class Matrix
{
private:
    // good to know how big you are
    size_t rows, columns;
    // and the vector containing the data 
    std::vector<TYPE> matrix;
public:
    Matrix(size_t numrows, size_t numcols) :
            rows(numrows), // remember the size
            columns(numcols), 
            matrix(rows * columns) // and allocate storage
    {
    }

    // does the same thing as the other constructor, except it sets a default value
    Matrix(size_t numrows, size_t numcols, TYPE init) :
            rows(numrows), columns(numcols), matrix(rows * columns, init)
    {
    }

    // gets the value at row, column and allows it to be modified        
    TYPE & operator()(size_t row, size_t column)
    {
        // check bounds here if you want
        // note the indexing math mapping 2 dimensions into 1
        return matrix[row * columns + column];
    }

    // gets a copy of the the value at row, column
    TYPE operator()(size_t row, size_t column) const
    {
        return matrix[row * columns + column];
    }

    // obvious what the next two methods do
    size_t getRows() const
    {
        return rows;
    }
    size_t getColumns() const
    {
        return columns;
    }

};

// handy dandy output helper.
friend std::ostream & operator<<(std::ostream & out, const Matrix & in)
{
    for (int i = 0; i < in.getRows(); i++)
    {
        for (int j = 0; j < in.getColumns(); j++)
        {
            out << in(i, j) << ' ';
        }
        out << '\n';
    }

    return out;
}