⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stack.h

📁 Essential C++ sampe code for Non-Windows environment
💻 H
字号:
/**************************************************
 * 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -