C++ Tutorial/queue stack/stack

Материал из C\C++ эксперт
Перейти к: навигация, поиск

A stack for characters

#include <iostream>
#include <string>
#include <queue>
#include <stack>
using namespace std;
int main()
{
  stack<char> stck;
  cout << "A stack for characters.\n";
  cout << "Pushing A, B, C, and D.\n";
  stck.push("A");
  stck.push("B");
  stck.push("C");
  stck.push("D");
  cout << "Now, retrieve those values in LIFO order.\n";
  while(!stck.empty()) {
    cout << "Popping: ";
    cout << stck.top() << "\n";
    stck.pop();
  }
  return 0;
}

Instantiation of an STL Stack

#include <stack>
#include <vector>
int main ()
{
    using namespace std;
    // A stack of integers
    stack <int> stackIntegers;
    // A stack of doubles
    stack <double> stackDoubles;
    // A stack of doubles contained in a vector
    stack <double, vector <double> > stackDoublesInVector;
    return 0;
}

Modify the top element in a stack

/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <iostream>
#include <stack>
using namespace std;
int main()
{
    stack<int> st;
    // push three elements into the stack
    st.push(1);
    st.push(2);
    st.push(3);
    // pop and print two elements from the stack
    cout << st.top() << " ";
    st.pop();
    cout << st.top() << " ";
    st.pop();
    // modify top element
    st.top() = 77;
    // push two new elements
    st.push(4);
    st.push(5);
    // pop one element without processing it
    st.pop();
    // pop and print remaining elements
    while (!st.empty()) {
        cout << st.top() << " ";
        st.pop();
    }
    cout << endl;
}
3 2 4 77

Pass stack to a function

#include <iostream>
using std::cout;
using std::endl;
#include <stack>  // stack adapter definition
#include <vector> // vector class-template definition
#include <list>   // list class-template definition
template< typename T > void pushElements( T &stackRef );
template< typename T > void popElements( T &stackRef );
int main()
{
   std::stack< int > intDequeStack;
   cout << "Pushing onto intDequeStack: ";
   pushElements( intDequeStack );
   cout << endl << endl;
   cout << "Popping from intDequeStack: ";
   popElements( intDequeStack );
   cout << endl;
   return 0;
}
template< typename T > void pushElements( T &stackRef )
{
   for ( int i = 0; i < 10; i++ )
   {
      stackRef.push( i );
      cout << stackRef.top() << " ";
   }
}
template< typename T > void popElements( T &stackRef )
{
   while ( !stackRef.empty() )
   {
      cout << stackRef.top() << " ";
      stackRef.pop();
   }
}
Pushing onto intDequeStack: 0 1 2 3 4 5 6 7 8 9
Popping from intDequeStack: 9 8 7 6 5 4 3 2 1 0

Push and pop an int stack

#include <iostream>
using std::cout;
using std::endl;
#include <stack>  // stack adapter definition
#include <vector> // vector class-template definition
#include <list>   // list class-template definition
int main()
{
   // stack with default underlying deque
   std::stack< int > intStack;
   for ( int i = 0; i < 10; i++ )
   {
      intStack.push( i );
      cout << "\n\n\npushing: "<< intStack.top() << " \n";
   }

   while ( !intStack.empty() )
   {
      cout << "\n\n\ntopping: "<<intStack.top() << " \n";
      intStack.pop();
   }
   return 0;
}
pushing: 08202

pushing: 18202

pushing: 28202

pushing: 38202

pushing: 48202

pushing: 58202

pushing: 68202

pushing: 78202

pushing: 88202

pushing: 98202

topping: 98202

topping: 88202

topping: 78202

topping: 68202

topping: 58202

topping: 48202

topping: 38202

topping: 28202

topping: 18202

topping: 08202

Push and pop a stack of list

#include <iostream>
using std::cout;
using std::endl;
#include <stack>  // stack adapter definition
#include <vector> // vector class-template definition
#include <list>   // list class-template definition
int main()
{
   // stack with underlying list
   std::stack< int, std::list< int > > intListStack;
   for ( int i = 0; i < 10; i++ )
   {
      intListStack.push( i );
      cout << "\n\n\npushing: "<< intListStack.top() << " \n";
   }

   while ( !intListStack.empty() )
   {
      cout << "\n\n\ntopping: "<<intListStack.top() << " \n";
      intListStack.pop();
   }
   return 0;
}
pushing: 08202

pushing: 18202

pushing: 28202

pushing: 38202

pushing: 48202

pushing: 58202

pushing: 68202

pushing: 78202

pushing: 88202

pushing: 98202

topping: 98202

topping: 88202

topping: 78202

topping: 68202

topping: 58202

topping: 48202

topping: 38202

topping: 28202

topping: 18202

topping: 08202

Push and pop a vector stack

#include <iostream>
using std::cout;
using std::endl;
#include <stack>  // stack adapter definition
#include <vector> // vector class-template definition
#include <list>   // list class-template definition
int main()
{
   // stack with underlying vector
   std::stack< int, std::vector< int > > intVectorStack;
   for ( int i = 0; i < 10; i++ )
   {
      intVectorStack.push( i );
      cout << "\n\n\npushing: "<< intVectorStack.top() << " \n";
   }

   while ( !intVectorStack.empty() )
   {
      cout << "\n\n\ntopping: "<<intVectorStack.top() << " \n";
      intVectorStack.pop();
   }
   return 0;
}
pushing: 08202

pushing: 18202

pushing: 28202

pushing: 38202

pushing: 48202

pushing: 58202

pushing: 68202

pushing: 78202

pushing: 88202

pushing: 98202

topping: 98202

topping: 88202

topping: 78202

topping: 68202

topping: 58202

topping: 48202

topping: 38202

topping: 28202

topping: 18202

topping: 08202

stack of pairs

#include <iostream>
#include <stack>
#include <string>
#include <utility>
using namespace std;
template <class T>
void print(T& c){
   for( typename T::iterator i = c.begin(); i != c.end(); i++ ){
      std::cout << *i << endl;
   }
}

int main( )
{
   const int num_loads = 5;
   const int palettes[num_loads] = { 7, 6, 2, 5, 10 };
   const char* destinations[num_loads] = { "A", "B","C", "D", "E" };
   // load up the truck
   stack< pair<int,string> > truck;
   cout << "LOADING TRUCK";
   for( int i = 0; i < num_loads; ++i )
   {
      truck.push( make_pair( palettes[i], destinations[i] ) );
      cout << "\nLoaded " << truck.top().first << " palettes for " << truck.top().second;
   }
   // make the trip
   cout << "\n\nTRUCK EN ROUTE";
   while( !truck.empty() )
   {
      cout << "\nDelivered " << truck.top().first << " palettes to " << truck.top().second;
      truck.pop();
   }
}

stack of string and vector of string

#include <iostream>
#include <vector>
#include <stack>
#include <string>
using namespace std;
int main()
{
   stack<string, vector<string> > str_stack;
   string quote[3] ={"A","B","C" };
   for (int i =0; i < 3; ++i)
      str_stack.push(quote[i]);
      
   while (!str_stack.empty()) {
      cout << str_stack.top();
      str_stack.pop();
   }
}

Stack: size and push

#include <iostream>
#include <stack>
using namespace std;
int main()
{
  int thedata[] = {45, 34, 56, 27, 71, 50, 62};
  stack<int> s;
  cout << "The stack size is now " << s.size() << endl;
  cout << "Pushing 4 elements " << endl;
  for (int i = 0; i < 4; ++i)
    s.push(thedata[i]);
    
  cout << "The stack size is now " << s.size() << endl;
  cout << "Popping 3 elements " << endl;
  for (int i = 0; i < 3; ++i) {
    cout << s.top() << endl;
    s.pop();
  }
  cout << "The stack size is now " << s.size() << endl;
  cout << "Popping all elements" << endl;
  while (!s.empty()) {
    cout << s.top() << endl;
    s.pop();
  } 
  cout << "The stack size is now " << s.size() << endl;
  
  return 0;
}
The stack size is now 0
Pushing 4 elements
The stack size is now 4
Popping 3 elements
27
56
34
The stack size is now 1
Popping all elements
45
The stack size is now 0

Stack: size, pop and push

#include <iostream>
#include <stack>
using namespace std;
int main()
{
  int thedata[] = {45, 34, 56, 27, 71, 50, 62};
  stack<int> s;
  cout << "The stack size is now " << s.size() << endl;
  cout << "Pushing 4 elements " << endl;
  for (int i = 0; i < 4; ++i)
    s.push(thedata[i]);
    
  cout << "The stack size is now " << s.size() << endl;
  cout << "Popping 3 elements " << endl;
  for (int i = 0; i < 3; ++i) {
    cout << s.top() << endl;
    s.pop();
  }
  cout << "The stack size is now " << s.size() << endl;
  
  return 0;
}
The stack size is now 0
Pushing 4 elements
The stack size is now 4
Popping 3 elements
27
56
34
The stack size is now 1

Stack: top, empty

#include <iostream>
#include <stack>
using namespace std;
int main()
{
  int thedata[] = {45, 34, 56, 27, 71, 50, 62};
  stack<int> s;
  cout << "The stack size is now " << s.size() << endl;
  cout << "Pushing 4 elements " << endl;
  for (int i = 0; i < 4; ++i)
    s.push(thedata[i]);
    
  cout << "The stack size is now " << s.size() << endl;
  cout << "Popping 3 elements " << endl;
  for (int i = 0; i < 3; ++i) {
    cout << s.top() << endl;
    s.pop();
  }
  cout << "The stack size is now " << s.size() << endl;
  cout << "Popping all elements" << endl;
  while (!s.empty()) {
    cout << s.top() << endl;
    s.pop();
  } 
  cout << "The stack size is now " << s.size() << endl;
  
  return 0;
}
The stack size is now 0
Pushing 4 elements
The stack size is now 4
Popping 3 elements
27
56
34
The stack size is now 1
Popping all elements
45
The stack size is now 0

Working with a stack of Integers

#include <stack>
#include <iostream>
int main ()
{
    using namespace std;
    stack <int> stackIntegers;
    stackIntegers.push (25);
    stackIntegers.push (10);
    stackIntegers.push (-1);
    stackIntegers.push (5);
 
    cout << stackIntegers.size () << " elements";
    while (stackIntegers.size () != 0)
    {
        cout << stackIntegers.top();
        stackIntegers.pop ();
    }
    if (stackIntegers.empty ())
        cout << endl << "The stack is now empty!";
    return 0;
}