C++/Queue Stack/stack

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

Modify the top element in a stack

<source lang="cpp">

/* 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.
*/
  1. include <iostream>
  2. 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

*/        
   
 </source>


Pass stack to a function

<source lang="cpp">

  1. include <iostream>

using std::cout; using std::endl;

  1. include <stack> // stack adapter definition
  2. include <vector> // vector class-template definition
  3. 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

*/       
   
 </source>


Push and pop an int stack

<source lang="cpp">


  1. include <iostream>

using std::cout; using std::endl;

  1. include <stack> // stack adapter definition
  2. include <vector> // vector class-template definition
  3. 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

*/
       
   
 </source>


Push and pop a stack of list

<source lang="cpp">

  1. include <iostream>

using std::cout; using std::endl;

  1. include <stack> // stack adapter definition
  2. include <vector> // vector class-template definition
  3. 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

*/        
   
 </source>


Push and pop a vector stack

<source lang="cpp">


  1. include <iostream>

using std::cout; using std::endl;

  1. include <stack> // stack adapter definition
  2. include <vector> // vector class-template definition
  3. 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

*/
       
   
 </source>


Stack of string and vector<string>: push(), pop(), empty(), top()

<source lang="cpp">

  1. include <iostream>
  2. include <string>
  3. include <stack>
  4. include <vector>

using namespace std; int main() {

    stack< string, vector<string> > s;
    s.push( "me" );
    s.push( "to" );
    s.push( "talk" );
    while ( !s.empty() ) {
         cout << s.top() << " ";
         s.pop();
    }
    return 0;

}


 </source>


Stack: size and push

<source lang="cpp">


  1. include <iostream>
  2. 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

*/        
   
 </source>


Stack: size, pop and push

<source lang="cpp">

  1. include <iostream>
  2. 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

*/       
   
 </source>


Stack: top, empty

<source lang="cpp">

  1. include <iostream>
  2. 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

*/        
   
 </source>