C++ C+中的多维可变大小数组+;
嗨,我想这样做:C++ C+中的多维可变大小数组+;,c++,arrays,C++,Arrays,嗨,我想这样做: int op(string s1, string s2){ int x = s1.size(); int y = s2.size(); int matrix = new int[x][y] /* do stuff with matrix */ } 由于某些原因,我会出现以下错误: SuperString.cpp(69) : error C2540: non-constant expression as array bound SuperStri
int op(string s1, string s2){
int x = s1.size();
int y = s2.size();
int matrix = new int[x][y]
/* do stuff with matrix */
}
由于某些原因,我会出现以下错误:
SuperString.cpp(69) : error C2540: non-constant expression as array bound
SuperString.cpp(69) : error C2440: 'initializing' : cannot convert from 'int (*)[1]' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
SuperString.cpp(71) : error C2109: subscript requires array or pointer type
谢谢 您需要将矩阵变量声明为
int*matrix
,因为动态数组声明为指针。但不能在两个维度都可变的情况下在一个新的数组中创建二维数组。您可以自己制作一维数组和索引数学。int*矩阵=新的int[x*y]
//将元素x1、y1设置为5
矩阵[x1+y1*x]=5代码>不能有非常量行大小的矩阵
您可以选择具有“指向数组的指针数组”结构,该结构可以作为pp[a][b]索引,就像矩阵一样。您不能为这样的结构分配单个新的。在缓冲区中,你必须手工构建它。 < P>这里是如何使用各种技术在C++中构建二维数组的总结。
静态2D矩阵:
const size\u t N=25;//矩阵的维数
整数矩阵[N][N];//N必须在编译时已知。
//之后不能更改N的大小
对于(尺寸i=0;i
动态二维矩阵:
const size\u t N=25;//矩阵的维数
整数**矩阵=新整数*[N];//每个元素都是指向数组的指针。
对于(尺寸i=0;i
使用std::vector的矩阵:
//注意:这有一些额外的开销
//一旦C++0x成为主流,就可以消除这种开销;)
//我说的是r值参考。
typedef向量<向量>矩阵;
typedef向量行;
常数大小N=25;//矩阵的维数
矩阵;
对于(尺寸i=0;i
这将帮助您避免许多错误。如果不需要通过函数更改矩阵的大小,您可以将存储字符串长度的int
声明为const
。这允许您创建多维数组,每个函数调用的大小可以不同,但保留一个const函数持续时间的蚂蚁大小
#include <iostream>
#include <string>
using namespace std;
int someFunc(string, string);
int someFunc(string s1, string s2)
{
const int x = s1.length();
const int y = s2.length();
int matrix[x][y];
int result=0;
for(int i=0;i<x;i++)
for(int j=0;j<y;j++)
matrix[i][j]=i*j;
for(int i=0;i<x;i++)
for(int j=0;j<y;j++)
result+=matrix[i][j];
return result;
}
int main()
{
string s1 = "fubar";
string s2 = "somethingelse";
cout<<someFunc(s1,s2)<<endl;
}
#包括
#包括
使用名称空间std;
int-someFunc(字符串,字符串);
int-someFunc(字符串s1,字符串s2)
{
常量int x=s1.length();
const int y=s2.length();
整数矩阵[x][y];
int结果=0;
对于(inti=0;i这更像是一个语法问题
上次检查GCC 4.4,<代码> int矩阵[x] [y];似乎按预期的方式工作。如果您的数组不需要在函数中间调整大小。您可以尝试此语法并查看它是否在编译器中工作。
他可能来自C.Y.他所做的是合法的:已经有许多问题从许多不同的角度来讨论这个问题(注意有些会被标记[C],因为C++从C获得它的数组行为)。一些链接:和…创建矩阵CLAS,见@ Zangs:仔细阅读问题。@ Zangs:“作为数组是指针”是不正确的。数组是“数组”。,它可能会在需要时转换为指针类型,例如,当将数组传递给函数时,调用函数中的数组会转换为指针类型,作为被调用函数中的形式参数。@Prasony是的,我在第一次回答时读到了编译器警告,但错过了2d部分。std::vector示例看起来是错误的。在Matrix矩阵(N)
声明,Matrix
有25行空行。然后再推25行非空行,结果是50行。@JWWalker是的,你完全正确。我编辑了答案,谢谢:)
const size_t N = 25; // the dimension of the matrix
int** matrix = new int*[N]; // each element is a pointer to an array.
for(size_t i = 0; i < N; ++i)
matrix[i] = new int[N]; // build rows
for(size_t i = 0; i < N; ++i)
{
for(size_t j = 0; j < N; ++j)
{
matrix[i][j] = /* random value! */;
}
}
// DON'T FORGET TO DELETE THE MATRIX!
for(size_t i = 0; i < N; ++i)
delete matrix[i];
delete matrix;
// Note: This has some additional overhead
// This overhead would be eliminated once C++0x becomes main-stream ;)
// I am talking about r-value references specifically.
typedef vector< vector<int> > Matrix;
typedef vector<int> Row;
const size_t N = 25; // the dimension of the matrix
Matrix matrix;
for(size_t i = 0; i < N; ++i)
{
Row row(N);
for(size_t j = 0; j < N; ++j)
{
row[j] = /* random value! */;
}
matrix.push_back(row); // push each row after you fill it
}
// Once you fill the matrix, you can use it like native arrays
for(size_t i = 0; i < N; ++i)
{
for(size_t j = 0; j < N; ++j)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
// Note that this is much more efficient than using std::vector!
int
main () {
// Create a 3D array that is 3 x 4 x 2
typedef boost::multi_array<double, 3> array_type;
typedef array_type::index index;
array_type A(boost::extents[3][4][2]);
// Assign values to the elements
int values = 0;
for(index i = 0; i != 3; ++i)
for(index j = 0; j != 4; ++j)
for(index k = 0; k != 2; ++k)
A[i][j][k] = values++;
// Verify values
int verify = 0;
for(index i = 0; i != 3; ++i)
for(index j = 0; j != 4; ++j)
for(index k = 0; k != 2; ++k)
assert(A[i][j][k] == verify++);
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int someFunc(string, string);
int someFunc(string s1, string s2)
{
const int x = s1.length();
const int y = s2.length();
int matrix[x][y];
int result=0;
for(int i=0;i<x;i++)
for(int j=0;j<y;j++)
matrix[i][j]=i*j;
for(int i=0;i<x;i++)
for(int j=0;j<y;j++)
result+=matrix[i][j];
return result;
}
int main()
{
string s1 = "fubar";
string s2 = "somethingelse";
cout<<someFunc(s1,s2)<<endl;
}