473,382 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Accessing elements in a stack?

I need to be able to access and move elements within a stack (C++ STL). I have tried using an iterator to navigate to the element that I want to move, but then I realized that I don't know how to remove that element. Is there a way to use pop to remove the element that my iterator is pointing to? Can you even use iterators on a stack? I'm kind of lost. Any help would be greatly appreciated.
Mar 31 '08 #1
6 16221
weaknessforcats
9,208 Expert Mod 8TB
A stack is a LIFO queue (last-in-first-out) so there is no provision to access members of the stack or move them around.

That is done using a vector.
Mar 31 '08 #2
A stack is a LIFO queue (last-in-first-out) so there is no provision to access members of the stack or move them around.

That is done using a vector.
Thats kind of what I thought, but the homework question reads: part a

"Write a function
Expand|Select|Wrap|Line Numbers
  1.  template <typename T>
  2. T second(const stack<T>& s);
that uses stack operations to return the second element on the stack."

For this part I just popped to T temp then used top( ) then push(temp) and I output the value that I got from top( )

But for part b:

Write a function
Expand|Select|Wrap|Line Numbers
  1.  template <typename T>
  2. void n2top(stack<T>& s, int n);
that moves the nth element (counting from the top, which is element 1) of the stack to the top, leaving the order of all other elements unchanged.

So I'm still not really sure what its looking for.
Mar 31 '08 #3
Laharl
849 Expert 512MB
Are you allowed to use a second stack and/or temporary variables? If so, consider how you could store the order and the value you seek using them.
Mar 31 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
Oh, I see now. An academic exercise.

To see the nth element of a stack,

1) pop all the elements before n and push them into a second stack.
2) do a top() to see the value of the nth element
3) push all of the elements from the second stack back onto the first stack.
4) delete the second stack
Apr 1 '08 #5
thanks everyone, I figured it out before the previous post, but that just confirms it. It seems like a stupid question now that I figured it out, so thanks everyone for your help.
Apr 1 '08 #6
Ganon11
3,652 Expert 2GB
thanks everyone, I figured it out before the previous post, but that just confirms it. It seems like a stupid question now that I figured it out, so thanks everyone for your help.
It's not a stupid question, it just requires a little thinking about how stacks work and how you might be able to use them.
Apr 1 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Rodrigo Daunaravicius | last post by:
Is there an elegant way to directly refer the 2nd dimension of a multi-dimensional sequence (like the nth character in a list of strings). An example would be deleting the newline in all the...
4
by: Paweł | last post by:
We can access the Nth element in the first form by document.forms.elements imagine that theres function like <INPUT TYPE=TEXT" onclick="somefunction(this)"> and... we have function...
2
by: Jeremy Gollehon | last post by:
I'm trying to help a friend out but couldn't find an answer so thought I'd post here. Is it possible to access the frame window at http://www.5460.net (a chinese site)? We're trying to get the...
3
by: Christopher Benson-Manica | last post by:
I appreciate all the responses to my earlier post about accessing named elements. However, I'm still wondering about my actual problem, which is that I need to initialize some arrays of named...
11
by: mickey22 | last post by:
Hi all, Actually, I have read a data file of an image from its sub header stored the data in a buffer.*imdata is a pointer to the buffer for data. The data size is 100x100x81 and no. of bytes per...
1
by: Gandalf | last post by:
Hi everyone, How can I access to specific element by javascript and not by using the getElementById method thanks
1
by: Ranmini | last post by:
when you add an object to an array list how do u access the elements of the objects that u add???? eg: vertex v; arrayliast.add(v) now how do you get the value of the weight element of the v...
5
by: Paul Brettschneider | last post by:
Hello, I have a global static array of structs and want to access a given element using an identifier. I don't want to use the element subscript, because it will change if I insert elements...
5
dmjpro
by: dmjpro | last post by:
I want to access the elements of IFrame. Suppose here is my code .... <iframe .... ></iframe> Now i want to access the elements inside the IFrame ..how can i do that ?
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.