{"id":1759,"date":"2021-02-28T00:15:04","date_gmt":"2021-02-28T05:15:04","guid":{"rendered":"http:\/\/www.jaimerios.com\/?p=1759"},"modified":"2025-12-02T23:36:57","modified_gmt":"2025-12-02T23:36:57","slug":"c-stack","status":"publish","type":"post","link":"https:\/\/jaimerios.com\/?p=1759","title":{"rendered":"C++ stack"},"content":{"rendered":"\n<p>In some sample code, you may see a reference to stack.<\/p>\n\n\n\n<p>This is a type of data structure, that according to <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\/stack\">cpp-reference<\/a> :<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 0 16px;font-size:0.8em;width:100%;text-align:left;background-color:#1E1E1E;font-style:italic;color:#D4D4D4\"><span style=\"border-bottom:1px solid rgba(234, 191, 191, 0.2)\">Markdown<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>The std::stack class is a container adapter that gives the programmer the functionality of a stack - specifically, a LIFO (last-in, first-out) data structure.<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">The std::stack class is a container adapter that gives the programmer the functionality of a stack - specifically, a LIFO (last-in, first-out) data structure.<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This is actually a wrapper for another container class.<\/p>\n\n\n\n<p>An example of it&#8217;s use is:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 0 16px;font-size:0.8em;width:100%;text-align:left;background-color:#1E1E1E;font-style:italic;color:#D4D4D4\"><span style=\"border-bottom:1px solid rgba(234, 191, 191, 0.2)\">C++<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>\/\/ Make a stack for us to play with\nauto stack_of_numbers = std::stack&lt;int>();\n\n\/\/ Push numeric values on to the stack\nfor (auto i = 1; i &lt;= 10; i++)\n  stack_of_numbers.push(i);\n\n\/\/ Display the top of the stack, then pop it, reducing the size of the stack by 1\nwhile (!stack_of_numbers.empty())\n{\n  std::cout &lt;&lt; stack_of_numbers.top() &lt;&lt; \"\\n\";\n  stack_of_numbers.pop();\n}\n\n\/\/ Little message to show that we got rid of the contents\nif (stack_of_numbers.empty())\n  std::cout &lt;&lt; \"Is empty\\n\";\n\n\/\/ Now confirming the size of the empty stack\nstd::cout &lt;&lt; \"Stack size: \" &lt;&lt; stack_of_numbers.size() &lt;&lt; \"\\n\";<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #6A9955\">\/\/ Make a stack for us to play with<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">auto<\/span><span style=\"color: #D4D4D4\"> stack_of_numbers = <\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #DCDCAA\">stack<\/span><span style=\"color: #D4D4D4\">&lt;<\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\">&gt;();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ Push numeric values on to the stack<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> (<\/span><span style=\"color: #569CD6\">auto<\/span><span style=\"color: #D4D4D4\"> i = <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">; i &lt;= <\/span><span style=\"color: #B5CEA8\">10<\/span><span style=\"color: #D4D4D4\">; i++)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #9CDCFE\">stack_of_numbers<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">push<\/span><span style=\"color: #D4D4D4\">(i);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ Display the top of the stack, then pop it, reducing the size of the stack by 1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> (!<\/span><span style=\"color: #9CDCFE\">stack_of_numbers<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">empty<\/span><span style=\"color: #D4D4D4\">())<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::cout &lt;&lt; <\/span><span style=\"color: #9CDCFE\">stack_of_numbers<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">top<\/span><span style=\"color: #D4D4D4\">() &lt;&lt; <\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D7BA7D\">\\n<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #9CDCFE\">stack_of_numbers<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">pop<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ Little message to show that we got rid of the contents<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> (<\/span><span style=\"color: #9CDCFE\">stack_of_numbers<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">empty<\/span><span style=\"color: #D4D4D4\">())<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::cout &lt;&lt; <\/span><span style=\"color: #CE9178\">&quot;Is empty<\/span><span style=\"color: #D7BA7D\">\\n<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ Now confirming the size of the empty stack<\/span><\/span>\n<span class=\"line\"><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::cout &lt;&lt; <\/span><span style=\"color: #CE9178\">&quot;Stack size: &quot;<\/span><span style=\"color: #D4D4D4\"> &lt;&lt; <\/span><span style=\"color: #9CDCFE\">stack_of_numbers<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">size<\/span><span style=\"color: #D4D4D4\">() &lt;&lt; <\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D7BA7D\">\\n<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In some sample code, you may see a reference to stack. This is a type of data structure, that according to cpp-reference : This is actually a wrapper for another container class. An example of it&#8217;s use is:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[143],"class_list":["post-1759","post","type-post","status-publish","format-standard","hentry","category-coding","tag-cpp"],"_links":{"self":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts\/1759","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1759"}],"version-history":[{"count":1,"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts\/1759\/revisions"}],"predecessor-version":[{"id":1893,"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts\/1759\/revisions\/1893"}],"wp:attachment":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}