开发者

Iterating through an array of an unknown size C++

I'm having trouble with a push-down stack! I have an array with a maximum size of 10. I allow the user to input numbers into the stack with push and remove them with pop. Depending on how many numbers there are in the array, I have to iterate through the stack, which 开发者_StackOverflow社区won't always be at a length of 10. Let's say the stack has 5 numbers (the size of the array is 10) in it. I need to go iterate through the elements up through element 5, because everything past that isn't a number. How can I do this?


Why don't you keep track of how many elements there are on the stack in your C++ class? When someone calls push, increment the count, and when someone calls pop, decrement the count.


Use std::vector as the underlying storage for your stack and use iterators begin(), end() to get the range of valid element in your vector.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜