Hello everyone,
I encountered an interesting bug in a program I was writing that was
caused by the following unexpected behaviour. Basically, creating a
new Stack object from another results in a reversal of the item
ordering.
Example:
Stack<stringa = new Stack<string>();
a.Push("a"); a.Push("b"); a.Push("c"); a.Push("d");
// a has "d","c","b","a"
Stack<stringb = new Stack<string>(a);
// b has "a","b","c","d"
Does anyone know why this happens? Is it a bug in the class library,
or some mistaken assumption on my part?
For background, I was using a stack for a recursive function wherein
each function call required its own copy of the initial stack to pop
items off. Needless to say, having the stack reverse itself at each
depth caused some interesting behaviour.
Looking forward to your comments;
- Robert Estelle 5 8741 Ro***********@gmail.com wrote:
Hello everyone,
I encountered an interesting bug in a program I was writing that was
caused by the following unexpected behaviour. Basically, creating a
new Stack object from another results in a reversal of the item
ordering.
Example:
Stack<stringa = new Stack<string>();
a.Push("a"); a.Push("b"); a.Push("c"); a.Push("d");
// a has "d","c","b","a"
Stack<stringb = new Stack<string>(a);
// b has "a","b","c","d"
Does anyone know why this happens? Is it a bug in the class library,
or some mistaken assumption on my part?
For background, I was using a stack for a recursive function wherein
each function call required its own copy of the initial stack to pop
items off. Needless to say, having the stack reverse itself at each
depth caused some interesting behaviour.
Looking forward to your comments;
- Robert Estelle
It's a mistaken assumption on your part. There is no constructor for the
Stack class that takes a Stack as parameter and copies it.
You are using the constructor that takes an IEnumerable. That means that
it's using a Stack.Enumerator to get the elements in the original stack.
The enumerator reads the elements in the same order as if they were
poped from the stack, and the constructor pushes them in that order. The
result is of course that the new stack is a reversed copy of the original.
--
Göran Andersson
_____ http://www.guffa.com
Thanks -- you are correct of course, but it seems odd that the library
would choose not to overload its own constructor to allow for
"correct" functionality. Does anyone know what went into this design
decision?
Maybe Stack<stringb = a.Clone(); suits you?
/LM
<Ro***********@gmail.comwrote in message
news:11**********************@v33g2000cwv.googlegr oups.com...
Hello everyone,
I encountered an interesting bug in a program I was writing that was
caused by the following unexpected behaviour. Basically, creating a
new Stack object from another results in a reversal of the item
ordering.
Example:
Stack<stringa = new Stack<string>();
a.Push("a"); a.Push("b"); a.Push("c"); a.Push("d");
// a has "d","c","b","a"
Stack<stringb = new Stack<string>(a);
// b has "a","b","c","d"
Does anyone know why this happens? Is it a bug in the class library,
or some mistaken assumption on my part?
For background, I was using a stack for a recursive function wherein
each function call required its own copy of the initial stack to pop
items off. Needless to say, having the stack reverse itself at each
depth caused some interesting behaviour.
Looking forward to your comments;
- Robert Estelle Ro***********@gmail.com wrote:
Thanks -- you are correct of course, but it seems odd that the library
would choose not to overload its own constructor to allow for
"correct" functionality. Does anyone know what went into this design
decision?
Because, if they did alter that particular overload to work in reverse, it
wouldn't work as expected if you did give it something you wanted to
iterate over in the correct direction. It makes more sense to have it
working as you expect it to, for the type you are giving it, in this case a
class that implements IEnumerable.
Perhaps the clone method (as noted by LM, in the adjacent reply) is more
suited to your needs.
--
Hope this helps,
Tom Spink
Google first, ask later. Ro***********@gmail.com wrote:
Thanks -- you are correct of course, but it seems odd that the library
would choose not to overload its own constructor to allow for
"correct" functionality. Does anyone know what went into this design
decision?
I guess that copying a stack is not something that you would usually do,
and if you do, you should probably duplicate all the items in the stack,
not just copy the reference. In your special case where you are using
strings, you can safely copy the references as strings are immutable,
but remember that the Stack<Tclass is designed for any kind of objects.
--
Göran Andersson
_____ http://www.guffa.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: LedZep |
last post by:
What up everyone,
I have to write a program that uses a stack to determine whether a
string is a palindrome (a string that is spelled identically backward
and forward). The program has to...
|
by: alisaee |
last post by:
plz check what i have made wrong what is requierd her is to creat class
queue and class stack and run the push,pop operation .
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class...
|
by: Frederick Gotham |
last post by:
I was intrigued by someone the other day who posted regarding methods of
"mirror-imaging" the bits in a byte. I thought it might be interesting to
write a fully-portable algorithm for...
|
by: steele20 |
last post by:
hello I am getting this strange error
Linking...
Cpp2.obj : error LNK2001: unresolved external symbol "public: bool __thiscall Stack::push(char const &)" (?push@Stack@@QAE_NABD@Z)
Debug/Cpp2.exe...
|
by: raghu |
last post by:
// Program to implement both stack and queue on an array
int main(void)
{
int a,i;
for(i=0;i<5;i++)
scanf("%d",&a); // here i'm filling (pushing) the elements
for(i=4;i>=0;i--)...
| |
by: rhle.freak |
last post by:
This is a very simple implementation of push and pop functions of a
stack.ive compiled it on msvc++ on windows platform and it wrks fine.
ive used 'int' as the data for the push operations,however...
|
by: mark4asp |
last post by:
Stack of limited size containing unique items?
Hi guys,
How would I implement a stack of limited size containing unique items?
For example. Suppose my stack has . I add 2 to it and it is now...
|
by: Tarique |
last post by:
Hello all.I am trying to implement a stack which can store either
integer or float values.
The code is given below:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define...
|
by: xr0krx |
last post by:
Hi, Im having some problems with my program. I'm kinda new to this stuff; only been doing this for about a month. What it's suppose to do is ask the user to input a word, and then print out the word...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |