Hi, everyone:

I want to create an infinite array, where normally we initialize an array like this:

int array[10];

There is 10 spaces in the computer reserved for array[].
However if I wish to have an array with no limit space, but the number of spaces of an array is increasing based on need, e.g. below 10 spaces or above, would that be possible?

Thanks ;)

Recommended Answers

All 19 Replies

An infinite array would take up an infinite amount of memory and I suppose you don't have that?

if you want a dynamic array, which has a finite size that grows and shrinks as you add or remove elements, take a look at std::vector

commented: Thanks for the suggestion :) +1

ya, i dun have infinite amount of memory, instead a limited one.

I wanna use an array to record the repeat number of certain value, e.g.:

0   0
1   0
2   0
3   0
...
51  10
52  22
...
60  5
61  9
...
83  0
84  0

those numbers with ... have 0 possibilities.

Since 84 numbers are taken into consideration, there are 84 spaces that have to reserved for them.

However, numbers with 0 possibilities, e.g. 0-51, 53-59 and 62-82, are not needed, so the extra 80 spaces will be a waste, since i have a limited memory system. Hence i need an infinite array.

So, niek_k, u mean, in my condition, infinite array will not be a good condition for me, right?

Thanks, bugmenot, I'll try the vector out.
Thanks for the suggestion.:)

Why not make an array of ints of a size of 84?

int possibilities[84];
possibilities[51] = 10;

If this is not what you meant, please explain clearer what your input and output should be.

Niek

all wrong :P
here is simple fully dynamic/infinite(up to long long) array

#include <iostream>
using namespace std;

class array{
	int* __date;
	unsigned long long __size;
	void resize(unsigned long long _size){
		int* tmp = new int[_size];
		copy(__date,__date+size(),tmp);
		delete[] __date;
		__date = tmp;
		__size = _size;
	}
public:
	array(){
		__date = NULL;
		__size = 0;
	}
	~array(){
		delete[] __date;
	}
	unsigned long long size(){
		return __size;
	}
	int& operator[](unsigned long long index){
		if (index+1 > size()){
			resize(index+1);
		}
		return __date[index];
	}
};

int main(int argc, char **argv) {
	array sexy;
	for(int i = 0; i < 10; ++i)
		sexy[i] = i;
	for(size_t i = 0; i < sexy.size(); ++i)
		cout << sexy[i] << ' ';
	return 0;
}

output:
0 1 2 3 4 5 6 7 8 9

commented: Thanks for the time replying +1

Why not use linked list instead?

they lack direct access...

Hey, guys, all thanks.
I'll try them out... :) ;p

I think by 'infinite array' you are meaning something like 'sparse array' - where only a few elements over a large range are non-zero.

You can use a std::map<int, int> for your array of ints using an integer index.

map<int, int> array;

array[20] = 5;
array[51] = 6;

cout << array[10] << "," << array[20] << "," << array[51] << endl;

In a map, only the accessed elements are allocated.

all wrong :P

screw you

screw you

I second that. ivailosp's "infinite" array doesn't account for the "wasted space" of all the unreferenced elements.

here it is

#include<iostream.h>
main(){
int size;
cin>>size;
int *L;
L = new int [ size ];
}

That is it , Enjoy

here it is

When I read a post like yours, I always wonder why you reply. I mean, this thread hasn't had a reply in over 1.5 years, what gave you the idea that it needed your code? Which brings me to the code. Sorry to be harsh, but ...well...you know... it sucks. Fact is that it'll only run on something that starts with Turbo and that's just the tip of the iceberg. I'm glad to explain why, if you're interested?

>Sorry to harsh, but ...well...you know... it sucks.
It could always be worse. For example, he could be using a variable length array extension instead of new[] .

until now i don't know what is the big deal wiith my code
i am just a biggeneer with it
try to explain more please

commented: Have some clue how could we know what you need. Don't jack someone's thread. +0

try to explain more please

Comments in red:

#include<iostream.h> // This header doesn't exist in standard C++. Change to <iostream> 
main(){ // main is defined as int main, not void main.
int size; // not assigning a value here
cin>>size;
int *L; 
L = new int [ size ];
 // forgot to delete allocated memory
// return 0; is a good idea.
}

And you might want to ident your code.
And you replied to a two year old thread.
And you need to learn about using code-tags

int size; // not assigning a value here
cin>>size;

So? If the value is not used before the cin , why do you need to set it to a value before that?

So? If the value is not used before the cin , why do you need to set it to a value before that?

I know, it's a matter of personal opinion. I always give variables an initial value, just to be sure. Here's a simple example why:

int i;
cin >> i;
cout << i;

this works just fine. But now I need to debug it and comment a line out:

int i;
//cin >> i;
cout << i; // uh-oh...

but if I had written:

[B]int i = 0;[/B]
//cin >> i;
cout << i; // no problem, just display 0

So you're right, but I prefer to give variables a value when declaring them. Just a failsafe for myself :)

So? If the value is not used before the cin , why do you need to set it to a value before that?

I know, it's a matter of personal opinion.

Then state it as an opinion. What you stated was a requirement... :icon_wink:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.