C ++中的多维可变大小数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1946830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multidimensional variable size array in C++
提问by SuperString
hi I want to do something like this:
嗨,我想做这样的事情:
int op(string s1, string s2){
int x = s1.size();
int y = s2.size();
int matrix = new int[x][y]
/* do stuff with matrix */
}
For some reason I get the following errors:
出于某种原因,我收到以下错误:
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
Thanks!
谢谢!
回答by AraK
Here is a summary of how to build a 2d array in C++ using various techniques.
这里总结了如何使用各种技术在 C++ 中构建二维数组。
Static 2D Matrix:
静态二维矩阵:
const size_t N = 25; // the dimension of the matrix
int matrix[N][N]; // N must be known at compile-time.
// you can't change the size of N afterwards
for(size_t i = 0; i < N; ++i)
{
for(size_t j = 0; j < N; ++j)
{
matrix[i][j] = /* random value! */;
}
}
Dynamic 2d Matrix:
动态二维矩阵:
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;
Matrix using std::vector:
使用 std::vector 的矩阵:
// 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;
}
3d matrix using boost::multi_array (taken from boost multi_array docs):
使用 boost::multi_array 的 3d 矩阵(取自 boost multi_array 文档):
// 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;
}
回答by Zanson
You need to declare the matrix var as int* matrix
, as a dynamic array is declared as a pointer. But you can't do a 2d array in one new with both dimensions being variable. You can do a 1D array and do the indexing math on your own.int* matrix = new int[x*y];
// Set element x1,y1 to 5
matrix[x1+y1*x] = 5;
您需要将矩阵 var 声明为int* matrix
,因为动态数组被声明为指针。但是你不能在一个新的二维数组中创建一个两个维度都是可变的。你可以做一个一维数组并自己做索引数学。int* matrix = new int[x*y];
// Set element x1,y1 to 5
matrix[x1+y1*x] = 5;
回答by Klaim
Use boost::multi_array. See the doc and this questionfor details.
使用boost::multi_array。有关详细信息,请参阅文档和此问题。
That will help you avoid a lot of errors.
这将帮助您避免很多错误。
回答by Chinmay Kanchi
If the size of matrix
does not need to change through the function, you can declare the int
s storing the string
length as const
. This allows you to create a multi-dimensional array that can vary in size for each function call, but retains a constant size for the duration of the function.
如果matrix
不需要通过函数改变的大小,则可以将int
存储string
长度的s声明为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;
}
EDIT: On reading one of the other answers posted while I was writing mine, I suppose you should use const size_t
instead of const int
. Sorry, my C++ is just a little rusty.
编辑:在阅读我在写我的时候发布的其他答案之一时,我想你应该使用const size_t
而不是const int
. 抱歉,我的 C++ 有点生疏。
回答by Pavel Radzivilovsky
You cannot have a matrix of non-constant row size.
您不能拥有非常量行大小的矩阵。
You may choose to have an "array of pointers to arrays" structure which can be indexed as pp[a][b] just as a matrix. You cannot allocate such a structure with a single new
. You will have to build it manually within a buffer.
您可以选择拥有一个“指向数组的指针数组”结构,它可以像矩阵一样索引为 pp[a][b]。您不能使用单个new
. 您必须在缓冲区内手动构建它。
回答by billyswong
It's more like a syntax problem.
这更像是一个语法问题。
Last check in gcc 4.4, int matrix[x][y];
seems to work as expected. If your array don't need resizing in the middle of the function. You may try this syntax and see if it works in your compiler.
最后检查 gcc 4.4,int matrix[x][y];
似乎按预期工作。如果您的数组不需要在函数中间调整大小。您可以尝试这种语法,看看它是否适用于您的编译器。