| | |
Infinite array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 19
Reputation:
Solved Threads: 0
Hi, everyone:
I want to create an infinite array, where normally we initialize an array like this:
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
I want to create an infinite array, where normally we initialize an array like this:
C++ Syntax (Toggle Plain Text)
int array[10];
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
•
•
Join Date: Jan 2008
Posts: 19
Reputation:
Solved Threads: 0
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.:
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.
I wanna use an array to record the repeat number of certain value, e.g.:
C++ Syntax (Toggle Plain Text)
0 0 1 0 2 0 3 0 ... 51 10 52 22 ... 60 5 61 9 ... 83 0 84 0
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?
If this is not what you meant, please explain clearer what your input and output should be.
Niek
c Syntax (Toggle Plain Text)
int possibilities[84]; possibilities[51] = 10;
If this is not what you meant, please explain clearer what your input and output should be.
Niek
•
•
Join Date: Apr 2008
Posts: 129
Reputation:
Solved Threads: 22
all wrong 
here is simple fully dynamic/infinite(up to long long) array
output:
0 1 2 3 4 5 6 7 8 9

here is simple fully dynamic/infinite(up to long long) array
CPP Syntax (Toggle Plain Text)
#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; }
0 1 2 3 4 5 6 7 8 9
Last edited by ivailosp; Apr 11th, 2008 at 12:16 pm.
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
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.
In a map, only the accessed elements are allocated.
You can use a std::map<int, int> for your array of ints using an integer index.
C++ Syntax (Toggle Plain Text)
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: bloodDonor class
- Next Thread: XML Parsing
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






