Infinite array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 19
Reputation: Suraine is an unknown quantity at this point 
Solved Threads: 0
Suraine Suraine is offline Offline
Newbie Poster

Infinite array

 
0
  #1
Apr 11th, 2008
Hi, everyone:

I want to create an infinite array, where normally we initialize an array like this:
  1. 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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,861
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: Infinite array

 
0
  #2
Apr 11th, 2008
An infinite array would take up an infinite amount of memory and I suppose you don't have that?
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: Infinite array

 
1
  #3
Apr 11th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 19
Reputation: Suraine is an unknown quantity at this point 
Solved Threads: 0
Suraine Suraine is offline Offline
Newbie Poster

Re: Infinite array

 
0
  #4
Apr 11th, 2008
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.:
  1. 0 0
  2. 1 0
  3. 2 0
  4. 3 0
  5. ...
  6. 51 10
  7. 52 22
  8. ...
  9. 60 5
  10. 61 9
  11. ...
  12. 83 0
  13. 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,861
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: Infinite array

 
0
  #5
Apr 11th, 2008
Why not make an array of ints of a size of 84?

  1. int possibilities[84];
  2. possibilities[51] = 10;

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

Niek
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 129
Reputation: ivailosp is an unknown quantity at this point 
Solved Threads: 22
ivailosp ivailosp is offline Offline
Junior Poster

Re: Infinite array

 
1
  #6
Apr 11th, 2008
all wrong
here is simple fully dynamic/infinite(up to long long) array
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class array{
  5. int* __date;
  6. unsigned long long __size;
  7. void resize(unsigned long long _size){
  8. int* tmp = new int[_size];
  9. copy(__date,__date+size(),tmp);
  10. delete[] __date;
  11. __date = tmp;
  12. __size = _size;
  13. }
  14. public:
  15. array(){
  16. __date = NULL;
  17. __size = 0;
  18. }
  19. ~array(){
  20. delete[] __date;
  21. }
  22. unsigned long long size(){
  23. return __size;
  24. }
  25. int& operator[](unsigned long long index){
  26. if (index+1 > size()){
  27. resize(index+1);
  28. }
  29. return __date[index];
  30. }
  31. };
  32.  
  33. int main(int argc, char **argv) {
  34. array sexy;
  35. for(int i = 0; i < 10; ++i)
  36. sexy[i] = i;
  37. for(size_t i = 0; i < sexy.size(); ++i)
  38. cout << sexy[i] << ' ';
  39. return 0;
  40. }
output:
0 1 2 3 4 5 6 7 8 9
Last edited by ivailosp; Apr 11th, 2008 at 12:16 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 5
Reputation: jason0202 is an unknown quantity at this point 
Solved Threads: 0
jason0202 jason0202 is offline Offline
Newbie Poster

Re: Infinite array

 
0
  #7
Apr 11th, 2008
Why not use linked list instead?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 129
Reputation: ivailosp is an unknown quantity at this point 
Solved Threads: 22
ivailosp ivailosp is offline Offline
Junior Poster

Re: Infinite array

 
0
  #8
Apr 11th, 2008
they lack direct access...
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 19
Reputation: Suraine is an unknown quantity at this point 
Solved Threads: 0
Suraine Suraine is offline Offline
Newbie Poster

Re: Infinite array

 
0
  #9
Apr 11th, 2008
Hey, guys, all thanks.
I'll try them out... ;p
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Infinite array

 
0
  #10
Apr 11th, 2008
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.

  1. map<int, int> array;
  2.  
  3. array[20] = 5;
  4. array[51] = 6;
  5.  
  6. cout << array[10] << "," << array[20] << "," << array[51] << endl;

In a map, only the accessed elements are allocated.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC