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

在这里插入图片描述
代码实践

  • main.cpp

    #include <iostream>
    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 <string>
    #include <iostream>
    #include <vector>
    #include <algorithm>
    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<string>::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<string>::iterator it = _stack.begin();
    	for (; it != _stack.end(); it++)
    	{
    		if (*it == elem)
    		{
    			cnt++;
    		}
    	}
    	return cnt;*/
    	return ::count(_stack.begin(), _stack.end(), elem);
    }
    
    
    

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值