《Essential C++》系列笔记之第四章(基于对象的编程风格)之第一节(如何实现一个Class)

《Essential C++》系列笔记之第四章(基于对象的编程风格)之第一节(如何实现一个Class)_第1张图片
代码实践

  • main.cpp

    #include 
    using namespace std;
    #include "Stack.h"
    
    int main()
    {
    	//class stack; //提供了一个前置声明 下面两句才可以使用
    	//stack* pa = 0;
    	//void process(const stack & s); 
    
    	//练习4_1
    #if 0
    
    	Stack stack;
    	cout << "----------" << endl;
    	cout << "size: " << stack.size() << endl;
    	cout << "empty: " << stack.empty() << endl;
    	cout << "full: " << stack.full() << endl;
    	string hi;
    	cout << "pop: " << stack.pop(hi) << endl;
    	cout << "peek: " << stack.peek(hi) << endl;
    	cout << "hi: " << hi << endl;
    	cout << "-----------" << endl;
    
    	cout << "push: " << stack.push("Hello");
    	cout << "push: " << stack.push("Class");
    	cout << "push: " << stack.push("!");
    
    	cout << "size: " << stack.size() << endl;
    	cout << "empty: " << stack.empty() << endl;
    	cout << "full: " << stack.full() << endl;
    	cout << "peek: " << stack.peek(hi) << endl;
    	cout << "hi: " << hi << endl;
    	cout << "pop: " << stack.pop(hi) << endl;
    	cout << "peek: " << stack.peek(hi) << endl;
    	cout << "hi: " << hi << endl;
    #endif
    
    	练习4_2
    #if 1
    	Stack stack;
    	string hi;
    	cout << "push Hello: " << stack.push("Hello") << ' ';
    	cout << "push Class: " << stack.push("Class") << ' ';
    	cout << "push !: " << stack.push("!") << endl;
    	cout << "size: " << stack.size() << endl;
    	cout << "empty: " << stack.empty() << endl;
    	cout << "full: " << stack.full() << endl;
    	cout << "peek: " << stack.peek(hi) << endl;
    	cout << "peek hi: " << hi << endl;
    	cout << "pop: " << stack.pop(hi) << endl;
    	cout << "peek: " << stack.peek(hi) << endl;
    	cout << "hi: " << hi << endl;
    	cout << "find Hello: " << stack.find("Hello") << endl;
    	cout << "find World: " << stack.find("World") << endl;
    	cout << "count Hello: " << stack.count("Hello") << endl;
    	cout << "count World: " << stack.count("World") << endl;
    
    #endif
    
    	system("pause");
    	return 0;
    }
    
  • Stack.h

    #pragma once
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    
    class Stack
    {
    public:
    	bool push(const string&);
    	bool pop(string&);
    	bool peek(string&);
    	bool find(const string&);
    
    	bool empty();
    	bool full();
    	
    	int size()
    	{
    		return _stack.size();
    	}
    	int count(const string&);
    
    private:
    	vector<string> _stack;
    };
    
    inline bool Stack::empty()
    {
    	return _stack.empty();
    }
    
    inline bool Stack::full()
    {
    	return _stack.size() == _stack.max_size();
    }
    
  • Stack.cpp

    #include "Stack.h"
    
    bool Stack::push(const string& elem)
    {
    	if (full())
    	{
    		return false;
    	}
    	
    	_stack.push_back(elem);
    	return true;
    }
    
    bool Stack::peek(string& elem)
    {
    	if (empty())
    	{
    		return false;
    	}
    
    	elem = _stack.back();
    	return true;
    }
    
    bool Stack::pop(string& elem)
    {
    	if (empty())
    	{
    		return false;
    	}
    
    	elem = _stack.back();
    	_stack.pop_back();
    	return true;
    }
    
    bool Stack::find(const string& elem)
    {
    	/*vector::iterator it = _stack.begin();
    
    	for (; it != _stack.end(); it++)
    	{
    		if (*it == elem)
    		{
    			return true;
    		}
    	}
    	return false;*/
    	//注意这里的::的作用
    	return (::find(_stack.begin(), _stack.end(), elem) != _stack.end());
    }
    
    int Stack::count(const string& elem)
    {
    	/*if (!find(elem))
    	{
    		return 0;
    	}
    
    	int cnt = 0;
    	vector::iterator it = _stack.begin();
    	for (; it != _stack.end(); it++)
    	{
    		if (*it == elem)
    		{
    			cnt++;
    		}
    	}
    	return cnt;*/
    	return ::count(_stack.begin(), _stack.end(), elem);
    }
    
    
    

今天是20200315 虽然人人平等,但是人人自由,所以人人不等

你可能感兴趣的:(《Essential,C++》系列笔记)