stack.h

来自「ESSENTIAL C++的源代码」· C头文件 代码 · 共 82 行

H
82
字号
/**************************************************
 * Essential C++ -- Stanley Lippman
 * Addison-Wesley 
 * ISBN 0-201-48518-4
 * homepage: www.objectwrite.com
 * email: slippman@objectwrite.com
 *************************************************/

#ifndef STACK_H_
#define STACK_H_

class Stack {
public:  
   
	bool   push( const string& );
    bool   pop(  string &elem );
	bool   peek( string &elem );

	bool   empty();
	bool   full();

   // definition of size() is placed within class
   // other members are simply declared ...
	int    size() { return _stack.size(); }
private:
	vector<string> _stack;
};

void fill_stack( Stack &stack, istream &is = cin )
{
    string str;
    while ( is >> str && ! stack.full() )
            stack.push( str );

    cout << "Read in " << stack.size() << " elements\n";
}

inline bool
Stack::empty()

{ 
    return _stack.empty(); 
}

bool
Stack::pop( string &elem ) 
{
    if ( empty() )
         return false;

    elem = _stack.back();
    _stack.pop_back(); 
    
    return true;
}

inline bool
Stack::full()
{ 
    return _stack.size() == _stack.max_size(); 
}

bool
Stack::peek( string &elem ) 
{
    if ( empty() )
         return false;

    elem = _stack.back();
    return true;
}
bool
Stack::push( const string &elem ) 
{
    if ( full() )
         return false;

    _stack.push_back( elem );
    return true;
}

#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?