Board index » cppbuilder » -> deque problem. Help! <-

-> deque problem. Help! <-


2005-02-21 01:05:05 AM
cppbuilder87
Hi,
I am trying to use a deque as a FIFO stack
My problem is that I can't use a queue as I can't
resize it at will
I insert boolean values into the deque and then
try to get them back by rolling the deque down.
All values are false.
So here is my code.
std::deque<int>alarmStack ;
alarmStack.resize( 5 ) ;
alarmStack.push_front( true ) ;
alarmStack.push_front( false ) ;
alarmStack.push_front( false ) ;
alarmStack.push_front( true ) ;
alarmStack.push_front( false ) ;
bool result ;
result = alarmStack.back() ; // Returns false
alarmStack.pop_back() ;
result = alarmStack.back() ; // Returns false
alarmStack.pop_back() ;
result = alarmStack.back() ; // Returns false
alarmStack.pop_back() ;
result = alarmStack.back() ; // Returns false
alarmStack.pop_back() ;
result = alarmStack.back() ; // Returns true
alarmStack.pop_back() ;
It looks like pop_back() doesn't roll down anything.
What is wrong?
Why do I get false, false, false, false, true instead
of true, false, false, true, false?
Thanks for any help.
Sincerely,
Steve JORDI
(Remove the K_I_L_LSPAM from my email address)
------------------------------------------------
1197 Prangins Email: XXXX@XXXXX.COM
Switzerland WWW: www.sjordi.com
------------------------------------------------
Volcanoes at www.sjordi.com/volcanoes
MovieDB at www.sjmoviedb.com
------------------------------------------------
 
 

Re:-> deque problem. Help! <-

"Steve JORDI" < XXXX@XXXXX.COM >wrote in message news: XXXX@XXXXX.COM ...
Quote
Hi,
I am trying to use a deque as a FIFO stack
My problem is that I can't use a queue as I can't
resize it at will

I insert boolean values into the deque and then
try to get them back by rolling the deque down.
All values are false.

So here is my code.
std::deque<int>alarmStack ;
alarmStack.resize( 5 ) ;
What if you change this to alarmStack.reserve(5);
Resize() is creating a deque of size 5 filled with
default values( most likely false). Reserve is
reserving the space for the allotted number so
that push_back(), push_front() doesn't force
a resize while within this range.
If you use resize, you can then access the values
directly to set them to something other than the
default.
------------------------------------------------
 

Re:-> deque problem. Help! <-

I just changed everything to a queue instead of
a deque and now everything works perfectly.
Actually queue seems a bit more flexible as it doesn't
care about the size itself.
Thanks for the tip though. I'll check this.
Sincerely,
Steve JORDI
(Remove the K_I_L_LSPAM from my email address)
------------------------------------------------
1197 Prangins Email: XXXX@XXXXX.COM
Switzerland WWW: www.sjordi.com
------------------------------------------------
Volcanoes at www.sjordi.com/volcanoes
MovieDB at www.sjmoviedb.com
------------------------------------------------
 

{smallsort}

Re:-> deque problem. Help! <-

"Steve JORDI" < XXXX@XXXXX.COM >wrote in message
Quote
I just changed everything to a queue instead of
a deque and now everything works perfectly.
Actually queue seems a bit more flexible as it doesn't
care about the size itself.

Thanks for the tip though. I'll check this.
Remember to consider the affect of resize()
as opposed to reserve() though. If you resize()
a container, you're creating default values. Subsequent
push_back() calls are increasing the size so if you're
trying to prevent swapping, for example, it's not doing
what you think.
 

Re:-> deque problem. Help! <-

"Duane Hebert" < XXXX@XXXXX.COM >writes:
Quote
>std::deque<int>alarmStack ;
>alarmStack.resize( 5 ) ;

What if you change this to alarmStack.reserve(5);
Resize() is creating a deque of size 5 filled with
default values( most likely false).
Confirmed. resize(5), applied to an empty container of int, inserts 5
times the value 0.