Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>stacks.end();</code> refers to the (nonexistent) element after the end of the vector. You can't dereference it; doing so will cause undefined behaviour, possibly a segmentation fault.</p> <p>It's not quite clear what you're doing there, but if you want an iterator for the last element, then either decrement it:</p> <pre><code>vector&lt;stack&lt;int&gt; &gt;::iterator it=stacks.end(); // points past the end --it; // points to last element </code></pre> <p>or use a reverse iterator (in which case, you use <code>++</code> rather than <code>--</code> to move backwards through the sequence):</p> <pre><code>vector&lt;stack&lt;int&gt; &gt;::reverse_iterator it=stacks.rbegin(); </code></pre> <p>Adding an element to a vector can invalidate it, so the <code>it-&gt;push_back(data)</code> at the end of <code>push()</code> is incorrect. You could avoid using an iterator here:</p> <pre><code>void push() { if (stacks.empty() || stacks.back().size()==stack_size) { // See below for explanation of this change stacks.push_back(stack&lt;int&gt;()); } stacks.back().push(data); } </code></pre> <p>In <code>pop()</code>, you probably want to remove the last stack if it's empty; otherwise, you'll end up with two empty stacks at the end, and your code will erroneously try to <code>pop</code> from one of those. Again, doing that could cause a segmentation fault or other undefined behavoiur. You probably want something like:</p> <pre><code>void pop() { if (stacks.empty()) { cout&lt;&lt;"\nEmpty Stack"; return ; } stacks.back().pop(); if (stacks.back().empty()) { stacks.pop_back(); } } </code></pre> <p>And now we've established an invariant that the last stack is never empty, <code>top</code> can be a bit simpler:</p> <pre><code>int top() { if (stacks.empty()) { cout&lt;&lt;"\nEmpty Stack"; return 0; } return stacks.back().top(); } </code></pre> <p>Also, you usually don't want to create objects using <code>new</code>, especially in a case like this where you're putting a copy of the object into the vector and then discarding the pointer, leaking the allocated memory. You can add an empty stack to the vector like this:</p> <pre><code>stacks.push_back(stack&lt;int&gt;()); </code></pre> <p>Sometimes (but not in this case) you might want to store pointers to allocated objects in a container; in that case, either remember to delete them when they're removed from the container, or store smart pointers such as <code>std::unique_ptr</code>. But in this case, just store objects.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload