C++ Codes
Algorithms
Algorithm Analysis in C++
Beginners
Code Snippets
Graphics
Data Structures
File Manipulation
Games
Mathematics
Miscellaneous
Visual C++ Library
C++ > Visual C++ 5.0 Standard C++ Library sample source codes
Stack top - Returns a const reference to the element most recently
Stack top - Returns a const reference to the element most recently top Header <stack> const value_type& top()const Returns a const reference to the element most recently pushed on the stack. If the container is empty, the result is undefined. value_type& top() Returns a reference to the element most recently pushed on the stack. If the container is empty, the result is undefined. Sample //Compiler options: /GX #include <stack> #include <iostream> int main() { std::stack<int> si ; //Constructs an empty stack, uses deque as default container. int i ; std::stack<int>::allocator_type a1 = si.get_allocator() ; std::cout << "call si.empty()" << std::endl ; if (si.empty()) { std::cout << "Stack is empty" << std::endl ; } else { std::cout << "Stack contains some elements" << std::endl ; } std::cout << "si.size() = " << si.size() << std::endl ; std::cout << "Push Values on si = " ; for(i = 0; i < 10; i++) { std::cout << i << ", " ; si.push(i) ; } std::cout << std::endl ; std::cout << "si.size() = " << si.size() << std::endl ; std::cout << "Pop Values from si = " ; while (!si.empty()) { std::cout << si.top() << ", " ; si.pop() ; } std::cout << std::endl ; return 0 ; } Program Output call si.empty() Stack is empty si.size() = 0 Push Values on si = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, si.size() = 10 Pop Values from si = 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
Privacy Policy
|
Link to Us
|
Links