954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Infinite array

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 ;)

Suraine
Newbie Poster
19 posts since Jan 2008
Reputation Points: 30
Solved Threads: 0
 

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

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

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

bugmenot
Posting Whiz in Training
225 posts since Nov 2006
Reputation Points: 53
Solved Threads: 34
 

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.:)

Suraine
Newbie Poster
19 posts since Jan 2008
Reputation Points: 30
Solved Threads: 0
 

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

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

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

ivailosp
Junior Poster
129 posts since Apr 2008
Reputation Points: 21
Solved Threads: 22
 

Why not use linked list instead?

jason0202
Newbie Poster
5 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

they lack direct access...

ivailosp
Junior Poster
129 posts since Apr 2008
Reputation Points: 21
Solved Threads: 22
 

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

Suraine
Newbie Poster
19 posts since Jan 2008
Reputation Points: 30
Solved Threads: 0
 

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 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.

dougy83
Posting Whiz in Training
275 posts since Jun 2007
Reputation Points: 85
Solved Threads: 45
 
all wrong :P


screw you

bugmenot
Posting Whiz in Training
225 posts since Nov 2006
Reputation Points: 53
Solved Threads: 34
 
screw you


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

dougy83
Posting Whiz in Training
275 posts since Jun 2007
Reputation Points: 85
Solved Threads: 45
 

here it is

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

That is it , Enjoy

Nizar.H
Newbie Poster
2 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 
here it is

When I read a post like yours, I always wonderwhy 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?

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

>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[] .

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

Nizar.H
Newbie Poster
2 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 
try to explain more please

Comments inred:

#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

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 
int size; // not assigning a value here
cin>>size;


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

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
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:

<strong>int i = 0;</strong>
//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 :)

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 
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:

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You