如何实现无限多维数组? [英] How to implement infinite multidimensional array?
问题描述
我想用下面的code,我想用它来输入未知大小。比如有一个数组 INT CAC [1000] [1000]
。我可以使用矢量<矢量<&INT GT; >阵列;
,那我怎么才能用初始化-1
?有什么建议?
的#include< sstream>
#包括LT&;&iostream的GT;
#包括LT&;矢量>
#包括LT&; cstdlib>
#包括LT&;&memory.h GT;使用命名空间std;INT CAC [1000] [1000];
串水库[1000] [1000];
矢量<字符串>话;
INT米;INT去(INT A,INT B){
如果(CAC [A] [B]≥= 0)返回CAC [A] [B];
如果(A == b)退回0; INT csum = -1;
的for(int i为一个; I< B; ++ I){
csum + =词语[I] .size()+ 1;
}
如果(csum&下; = M ||一个== B-1){
字符串九月=;
的for(int i为一个; I< B; ++ I){
RES [A] [B] .append(SEP);
水库[A] [B] .append(字由[i]);
九月=;
}
返回CAC [A] [B] =(M-csum)*(M-csum);
} INT RET = 10亿;
INT best_sp = -1;
对于(INT SP = A + 1; SP< B; ++ SP){
INT CUR =去(A,SP)+去(SP,B);
如果(CUR< = RET){
RET = CUR;
best_sp = SP;
}
}
水库[A] [B] =水库[一个] [best_sp] +\\ N+解析度[best_sp] [B];
返回CAC [A] [B] = RET;
}
INT主(INT ARGC,字符** argv的){
memset的(CAC,-1,的sizeof(CAC));
M =的atoi(ARGV [1]);
串词;
而(CIN>>字)words.push_back(字);
去(0,words.size());
COUT<<水库[0] [words.size()]下;&下; ENDL;
}
从使用STL的std ::矢量比下面的解决方案,它指出在这篇文章的评论更为直接。我发现这个网站有效地解释了话题:的http:// www.learncpp.com/cpp-programming/16-2-stl-containers-overview/
无限大小的数组实际上不可能。但是,您可以使用动态分配基本上达到这样的效果。下面是一些示例code:
INT计数器= 0;
为int * myArray的=新INT [1000];
用数据填充数组,每次添加一个值时间计数器递增。当计数器达到1000,请执行以下操作:
为int * largerArray = INT新[2000];
的for(int i = 0; I< 1000;我++)
{
largerArray [I] = myArray的[I]
}
删除[] myArray的;
myArray的= largerArray;
通过这种方法,您可以创建一个无限大小的数组最接近可能的,我不相信性能会与复制件问题
I want to use the code below and I want to use it for "unknown size of input". For example there is an array int cac[1000][1000]
. I can use vector<vector<int> > array;
, then how can i initialize it with -1
? Any suggestions?
#include <sstream>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <memory.h>
using namespace std;
int cac[1000][1000];
string res[1000][1000];
vector<string> words;
int M;
int go(int a, int b){
if(cac[a][b]>= 0) return cac[a][b];
if(a == b) return 0;
int csum = -1;
for(int i=a; i<b; ++i){
csum += words[i].size() + 1;
}
if(csum <= M || a == b-1){
string sep = "";
for(int i=a; i<b; ++i){
res[a][b].append(sep);
res[a][b].append(words[i]);
sep = " ";
}
return cac[a][b] = (M-csum)*(M-csum);
}
int ret = 1000000000;
int best_sp = -1;
for(int sp=a+1; sp<b; ++sp){
int cur = go(a, sp) + go(sp,b);
if(cur <= ret){
ret = cur;
best_sp = sp;
}
}
res[a][b] = res[a][best_sp] + "\n" + res[best_sp][b];
return cac[a][b] = ret;
}
int main(int argc, char ** argv){
memset(cac, -1, sizeof(cac));
M = atoi(argv[1]);
string word;
while(cin >> word) words.push_back(word);
go(0, words.size());
cout << res[0][words.size()] << endl;
}
Using std::vector from the STL is much more straightforward than the following solution, which was pointed out in the comments for this post. I find that this site explains that topic effectively: http://www.learncpp.com/cpp-programming/16-2-stl-containers-overview/
An array of infinite size is not actually possible. However, you can achieve basically that effect using dynamic allocation. Here's some sample code:
int counter = 0;
int* myArray = new int[1000];
Fill the array with data, incrementing counter each time you add a value. When counter reaches 1000, do the following:
int* largerArray = new int[2000];
for( int i = 0; i < 1000; i++ )
{
largerArray[i] = myArray[i];
}
delete[] myArray;
myArray = largerArray;
With this method, you create the closest thing possible to an infinitely sized array, and I don't believe performance will be an issue with the copy piece
这篇关于如何实现无限多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!