943,678 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4892
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 11th, 2008
0

Infinite array

Expand Post »
Hi, everyone:

I want to create an infinite array, where normally we initialize an array like this:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 30
Solved Threads: 0
Newbie Poster
Suraine is offline Offline
19 posts
since Jan 2008
Apr 11th, 2008
0

Re: Infinite array

An infinite array would take up an infinite amount of memory and I suppose you don't have that?
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Apr 11th, 2008
1

Re: Infinite array

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
Reputation Points: 53
Solved Threads: 33
Posting Whiz in Training
bugmenot is offline Offline
224 posts
since Nov 2006
Apr 11th, 2008
0

Re: Infinite array

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.:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 30
Solved Threads: 0
Newbie Poster
Suraine is offline Offline
19 posts
since Jan 2008
Apr 11th, 2008
0

Re: Infinite array

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
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Apr 11th, 2008
1

Re: Infinite array

all wrong
here is simple fully dynamic/infinite(up to long long) array
CPP Syntax (Toggle Plain Text)
  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.
Reputation Points: 21
Solved Threads: 22
Junior Poster
ivailosp is offline Offline
129 posts
since Apr 2008
Apr 11th, 2008
0

Re: Infinite array

Why not use linked list instead?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jason0202 is offline Offline
5 posts
since Apr 2008
Apr 11th, 2008
0

Re: Infinite array

they lack direct access...
Reputation Points: 21
Solved Threads: 22
Junior Poster
ivailosp is offline Offline
129 posts
since Apr 2008
Apr 11th, 2008
0

Re: Infinite array

Hey, guys, all thanks.
I'll try them out... ;p
Reputation Points: 30
Solved Threads: 0
Newbie Poster
Suraine is offline Offline
19 posts
since Jan 2008
Apr 11th, 2008
0

Re: Infinite array

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.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: c++ and assembly: undefined reference to
Next Thread in C++ Forum Timeline: Im having trouble with class association





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC