Thank you for the response.
Perhaps I should use another container format for my purpose.
A deque seems to offer all the functionality I need and a similar test shows their items use only 8bytes.
However, I don't understand why...
Aren't they linked in the same way?
When I write:
1 2 3 4
|
deque<double> deq;
deque<double>::iterator it;
it = deq.begin()
it++;
|
I expected that to 'raise the iterator by one' it needs a pointer to the memory location of the next element.
Which would again require the same space for the pointers (the 16 or 24 bytes including the double), because also the deque allows me to go trough it both ways (using it++ or it--).
Is there any reason why these pointers are not needed in deques??
Is it perhaps because deques don't allow middle insertion and therefore the elements are stored like 0x00000000, 0x00000001, ... ??
If so, what if another program tries to allocate some space at the same time??
Sorry for the stupid questions.
Here is a test which shows the functionality I want to use.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#include <iostream>
#include <deque>
using namespace std;
int main() {
char buf;
cout << "Program will now allocate doubles in a double-ended queue." << endl
<< "sizeof(double) = " << sizeof(double) << " bytes" << endl
<< "(<enter> to continue)";
cin.get(buf);
deque<double> q;
for(int i=1; i<=10; i++) {
for(int j=0; j<10; j++) {
q.push_back(j*i);
if(q.size() > 15) q.pop_front();
cout << "q = ";
deque<double>::iterator it = q.begin();
for(it=q.begin(); it!=q.end(); it++) {
cout << *it << ',' ;
} cout << endl;
}
cout << "Stop: pushed " << i*10 << " numbers (<enter> to continue) ";
cin.get(buf);
}
return 0;
}
|