我有一个2D数组传递给c++函数的问题。该函数应该打印2D数组的值。但是得到错误。
函数void showAttributeUsage(int)
数组下标的int(int)类型无效。
我知道问题是我传递特定数组给函数的语法,但我不知道如何解决这个问题。
代码:
#include <iostream>
using namespace std;
void showAttributeUsage(int);
int main()
{
int qN, aN;
cout << "Enter Number of Queries : ";
cin >> qN;
cout << "nEnter Number of Attributes : ";
cin >> aN;
int attVal[qN][aN];
cout << "nEnter Attribute Usage Values" << endl;
for(int n = 0; n < qN; n++) { //for looping in queries
cout << "nn***************** COLUMN " << n + 1 << " *******************nn";
for(int i = 0; i < aN; i++) { //for looping in Attributes
LOOP1:
cout << "Use(Q" << n + 1 << " , " << "A" << i + 1 << ") = ";
cin >> attVal[n][i];
cout << endl;
if((attVal[n][i] > 1) || (attVal[n][i] < 0)) {
cout << "nnTHE VALUE MUST BE 1 or 0 . Please Re-Enter The Valuesnn";
goto LOOP1; //if wrong input value
}
}
}
showAttributeUsage(attVal[qN][aN]);
cout << "nnYOUR ATTRIBUTE USAGE MATRIX ISnn";
getch();
return 0;
}
void showAttributeUsage(int att)
{
int n = 0, i = 0;
while(n != ' ') {
while(i != ' ') {
cout << att[n][i] << " ";
i++;
}
cout << endl;
n++;
}
}
我建议使用std::vector
:实例
void showAttributeUsage(const std::vector<std::vector<int>>& att)
{
for (std::size_t n = 0; n != att.size(); ++n) {
for (std::size_t i = 0; i != att.size(); ++i) {
cout << att[n][i] << " ";
}
cout << endl;
}
}
这样称呼它:
showAttributeUsage(attVal);
看看你的代码,我看不出你不能使用std::vector
的理由。
首先,您的代码使用一个非标准的c++扩展,即Variable Length Arrays
(VLA)。如果你的目标是编写标准c++代码,那么你所写的就不是有效的标准c++。
第二,你最初通过int
的尝试是错误的,但是如果你使用vector
,你通过int
的尝试看起来和使用vector
几乎一样。
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
typedef std::vector<int> IntArray;
typedef std::vector<IntArray> IntArray2D;
using namespace std;
void showAttributeUsage(const IntArray2D&);
int main()
{
int qN, aN;
cout << "Enter Number of Queries : ";
cin >> qN;
cout << "nEnter Number of Attributes : ";
cin >> aN;
IntArray2D attVal(qN, IntArray(aN));
//... Input left out ...
showAttributeUsage(attVal);
return 0;
}
void showAttributeUsage(const IntArray2D& att)
{
for_each(att.begin(), att.end(),
[](const IntArray& ia) {std::copy(ia.begin(), ia.end(), ostream_iterator<int>(cout, " ")); cout << endl;});
}
我省略了代码的输入部分。vector
像使用普通数组一样使用[]
,因此声明vector后无需重写代码。您可以使用molbdnilo在另一个答案中提供的代码来输入数据(不使用goto
)。
第二,只是为了把它扔进混合,showAttributeUsage
函数使用copy
算法输出信息。for_each
抛出vector的每一行,对于一行元素调用std::copy
。如果您使用的是兼容c++ 11的编译器,那么上面的代码应该可以编译。
你应该这样声明这个函数。
void array_function(int m, int n, float a[m][n])
{
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
a[i][j] = 0.0;
}
这个问题在这里已经有了答案。你需要使用指针或模板。还有其他的解决方案。
总之,像这样做:
template <size_t rows, size_t cols>
void showAttributeUsage(int (&array)[rows][cols])
{
for (size_t i = 0; i < rows; ++i)
{
std::cout << i << ": ";
for (size_t j = 0; j < cols; ++j)
std::cout << array[i][j] << 't';
std::cout << std::endl;
}
}
您正在使用一个编译器扩展,该扩展允许您声明在运行时确定大小的数组
没有办法将具有这种维度的2D数组传递给函数,因为作为函数参数的数组除了一个维度之外的所有维度都必须在编译时已知。
可以使用固定的维度,并使用读取的值作为传递给函数的限制:
const int max_queries = 100;
const int max_attributes = 100;
void showAttributeUsage(int array[max_queries][max_attributes], int queries, int attributes);
int main()
{
int attVal[max_queries][max_attributes];
int qN = 0;
int aN = 0;
cout << "Enter Number of Queries (<= 100) : ";
cin >> qN;
cout << "nEnter Number of Attributes (<= 100) : ";
cin >> aN;
cout << "nEnter Attribute Usage Values" << endl;
for (int n = 0; n < qN; n++)
{
cout << "nn***************** COLUMN " << n + 1 <<" *******************nn";
for (int i = 0; i < aN; i++)
{
bool bad_input = true;
while (bad_input)
{
bad_input = false; // Assume that input will be correct this time.
cout << "Use(Q" << n + 1 << " , " << "A" << i + 1 << ") = ";
cin >> attVal[n][i];
cout << endl;
if (attVal[n][i] > 1 || attVal[n][i] < 0)
{
cout << "nnTHE VALUE MUST BE 1 or 0 . Please Re-Enter The Valuesnn";
bad_input = true;
}
}
}
}
cout << "nnYOUR ATTRIBUTE USAGE MATRIX ISnn";
showAttributeUsage(attVal, qN, aN);
getch();
return 0;
}
void showAttributeUsage(int att[max_queries][max_attributes], int queries, int attributes)
{
for (int i = 0; i < queries; i++)
{
for (int j = 0; j < attributes; j++)
{
cout << att[i][j] << " ";
}
cout << endl;
}
}
作为比较,相同的程序使用std::vector
,几乎相同,但没有大小限制:
void showAttributeUsage(vector<vector<int> > att);
int main()
{
cout << "Enter Number of Queries (<= 100) : ";
cin >> qN;
cout << "nEnter Number of Attributes (<= 100) : ";
cin >> aN;
vector<vector<int> > attVal(qN, vector<int>(aN));
cout << "nEnter Attribute Usage Values"<<endl;
for (int n = 0; n < qN; n++)
{
cout<<"nn***************** COLUMN "<<n+1<<" *******************nn";
for (int i = 0; i < aN; i++)
{
bool bad = true;
while (bad)
{
bad = false;
cout << "Use(Q" << n + 1 << " , " << "A" << i + 1 << ") = ";
cin >> attVal[n][i];
cout << endl;
if (attVal[n][i] > 1 || attVal[n][i] < 0)
{
cout << "nnTHE VALUE MUST BE 1 or 0 . Please Re-Enter The Valuesnn";
bad = true;
}
}
}
}
cout << "nnYOUR ATTRIBUTE USAGE MATRIX ISnn";
showAttributeUsage(attVal);
getch();
return 0;
}
void showAttributeUsage(vector<vector<int> > att);
{
for (int i = 0; i < att.size(); i++)
{
for (int j = 0; j < att[i].size(); j++)
{
cout << att[i][j] << " ";
}
cout << endl;
}
}
special Logic为我工作。终于找到了。: -)
int** create2dArray(int rows, int cols) {
int** array = new int*[rows];
for (int row=0; row<rows; row++) {
array[row] = new int[cols];
}
return array;
}
void delete2dArray(int **ar, int rows, int cols) {
for (int row=0; row<rows; row++) {
delete [] ar[row];
}
delete [] ar;
}
void loadDefault(int **ar, int rows, int cols) {
int a = 0;
for (int row=0; row<rows; row++) {
for (int col=0; col<cols; col++) {
ar[row][col] = a++;
}
}
}
void print(int **ar, int rows, int cols) {
for (int row=0; row<rows; row++) {
for (int col=0; col<cols; col++) {
cout << " | " << ar[row][col];
}
cout << " | " << endl;
}
}
int main () {
int rows = 0;
int cols = 0;
cout<<"ENTER NUMBER OF ROWS:t";cin>>rows;
cout<<"nENTER NUMBER OF COLUMNS:t";cin>>cols;
cout<<"nn";
int** a = create2dArray(rows, cols);
loadDefault(a, rows, cols);
print(a, rows, cols);
delete2dArray(a, rows, cols);
getch();
return 0;
}
如果是c++,那么你可以使用一个可以处理任意维度的模板
template<typename T>
void func(T& v)
{
// code here
}
int main()
{
int arr[][7] = {
{1,2,3,4,5,6,7},
{1,2,3,4,5,6,7}
};
func(arr);
char triplestring[][2][5] = {
{
"str1",
"str2"
},
{
"str3",
"str4"
}
};
func(triplestring);
return 0;
}