2012-07-30 40 views
2

我是编程新手,所以我想编写一个代码,让我输入一个2维数组(或者矩阵),然后打印出来。'int'的问题

#include <iostream> 
using namespace std; 

void printArray(const int *array, int count) 
    { 
     for (int i = 0; i < count; i++) 
      cout << array[ i ] << " "; 

     cout << endl; 
    } 

int main() { 
int n; 
cout<<"Please enter the length of your matrix : "<<endl; 
cin>>n; 
int * y=new int [n]; 
for (int w = 0; w <= n-1; w++) { 

    y[w] = new int [n]; 
    cout<<"Insert the elements "; 

      for (int z = 0; z <= n-1; z++) 
      { 
       cin >>y [w][z]; 
      } 
} 

printArray(y, n); 

} 

但是我得到“从‘诠释*’到‘廉政’无效转换”和错误“无效的类型int [INT]数组下标”。你能否检查我的代码并指出我的缺陷?

由于

+0

@RyanGray如果数组不是固定大小(不是),那么它必须是新的。当然,应该真正使用诸如'std :: vector'之类的结构。 – juanchopanza 2012-07-30 16:46:44

+0

@RyanGray:使用'new []'是创建一个数组的合法方式,它与'new'不一样。 – Rook 2012-07-30 16:46:50

+0

你需要使用双指针,你的数组需要被声明为int ** y = new int * [n] – user1084113 2012-07-30 16:47:25

回答

6

您声明yint*,它只会是一维的。您需要声明yint**,因为它是二维的。

代码不编译的原因是因为int* y指向的存储器中的单个(即是一个整数数组,换句话说,一堆int峰)。 y[w]是其中一个int s在这个数组中,因此y[w] = new int[n]无法编译,因为您试图将int*指定给int

更改yint**意味着y可以指向的int*秒的阵列。由于每个int*都可以指向一个int的数组,因此您将拥有一个2维数组。为10×10矩阵

实施例的代码与int**

int** iShouldUseStdVector = new int*[10]; // allocate 10 int* <-- 
for (int i = 0; i < 10; i++) 
{ 
    iShouldUseStdVector[i] = new int[10]; // allocate 10 int <-- 
    for (int k = 0; k < 10; k++) 
    { 
     iShouldUseStdVector[i][k] = k; 
    } 
} 

实施例的代码为10×10矩阵,std::vector

std::vector<std::vector<int>> thisIsEasy; 
for (int i = 0; i < 10; i++) 
{ 
    thisIsEasy.push_back(std::vector<int>()); 
    for (int k = 0; k < 10; k++) 
    { 
     thisIsEasy[i].push_back(k); 
    } 
} 

我建议使用std::vector<std::vector<int>> y;代替,因为它处理用于存储器你通过方便地增长,因为你想添加更多的元素,并在其被破坏时释放内存。

0

int * y=new int [n];是指向int值的动态分配的阵列;

y[w] = new int [n];试图将指针分配给数组的一个元素。

作为一个学习练习,乱搞原始数组是一个好主意。一旦你知道如何使用它们,你就足够了解停止使用它们(非常多),并使用自动存储类型,如std::vector

+0

感谢您的帮助! – user1563544 2012-08-01 10:52:10

1

int * y = new int [n];

这是一个长度为n的数组。你所需要的是:

int **y = new int*[n]; 
for (int i=0; i<n; i++) 
    y[i] = new int[n]; 

.... 

//delete[] y[i] in a loop 
delete[] y; 

由于您使用C++,为什么不:

#include <vector> 

... 
std::vector<std::vector<int> > matrix; 
+0

非常感谢! – user1563544 2012-08-01 10:50:21

0

1)你想声明y为指针的int数组:

int ** y = new int* [n]; 

2)在printArray你正在处理一个矩阵,而不是一个数组。要访问一个矩阵的成员使用两个嵌套循环for这样的:

for (int i = 0; i < nLines; i++) { 
    for (int j = 0; i < nColumns; j++) { 
     std::cout << matrix[i][j] << std::endl; 
    } 
} 

3)需要一个指针传递给的int阵列,而不是一个指针到一个intprintArray方法。

void printArray(const int **array, int count) 

找到工作的代码,这里的上述修正:http://ideone.com/2h9dR

+0

谢谢你的帮助!欣赏链接。 – user1563544 2012-08-01 10:50:48

0

首先,阅读其他的答案,然后也许重读在一个良好的C++的书指针章。现在,除非你需要极高的速度,否则使用vectorvectordouble。用C++ 11(较新的C++标准),这是非常好的,可读的,所以我张贴第一:

#include <iostream> 
#include <vector> 

void printArray(std::vector< std::vector<double> > & v) { 
    for (const auto & row : v){ 
     for (const auto & value : row){ 
     std::cout << value << " "; 
     } 
     std::cout << std::endl; 
    } 
} 

int main() { 
    int n; 
    std::cout<<"Please enter the length of your matrix : "<<std::endl; 
    std::cin>>n; 
    std::vector<std::vector<double>> y(n,std::vector<double>(n,0)); 
    for (auto & row : y){ 
     std::cout<<"Insert the elements of row :"; 
     for (auto & value : row){ 
     std::cin >> value; 
     } 
    } 
    printArray(y); 
} 

对于较旧的C++是这样的:

void printArray(std::vector< std::vector<double> > & v) { 
    for (std::vector<std::vector<double> >::const_iterator it = v.begin(); it != v.end();it++){ 
     for (std::vector<double>::const_iterator it2 = it->begin(); it2!= it->end();it2++) { 
    std::cout << (*it2) << " "; 
     } 
     std::cout << std::endl; 
    } 
} 

int main() { 
    int n; 
    std::cout<<"Please enter the length of your matrix : "<<std::endl; 
    std::cin>>n; 
    std::vector<std::vector<double> > y(n,std::vector<double>(n,0)); 
    for (std::vector<std::vector<double> >::iterator it = y.begin(); it!= y.end();it++){ 
     std::cout<<"Insert the elements of row :"; 
     for (std::vector<double>::iterator it2 = it->begin(); it2!= it->end();it2++) { 
    std::cin >> (*it2); 
     } 
    } 
    printArray(y); 
} 

注意y(n,std::vector<double>(n,0))手段,使n载体,每个载体有n零。您也可以使用y[1][2]来获取和设置值。如果您使用y.at(1).at(2),则可以进行适当的检查,以便在读取或写入界限时收到异常。

+0

谢谢你的帮助! – user1563544 2012-08-01 10:52:30

+0

谢谢。另一种感谢的方式是赞成。 ;-) – 2012-08-01 21:31:41