《Essential C++》课后习题和一点思考

今天是中秋节+国庆节,祝大家节日快乐!!

目录

第四章4.1&&4.2:造轮子stack

Stack.h

Stack.cpp

第六章6.1:将class改写成template

保证宏定义函数单句展开的do{…}while(0)技巧


第四章4.1&&4.2:造轮子stack

这里只是用class来造,所以只能支持一种类型,这里是指定string

后面会用template改写来支持所有类型!

Stack.h

#include <string>
#include <vector>
#include <algorithm>
using namespace std;

class Stack{
public:
    bool push(const string& );
    bool pop(string &elem);
    bool peek(string &elem);
    bool find(const string &elem) const;
    int count(const string &elem) const;
    // class的成员函数会被自动视为内联函数!
    bool empty() const {
        return _stack.empty();
    }
    bool full() const {
        return _stack.size()==_stack.max_size();
    }
    int size() const {
        return _stack.size();
    }

private:
    vector<string> _stack;
    
};

bool Stack::pop(string &elem){
    if(empty()){
        return false;
    }
    elem = _stack.back();
    return true;
}

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;
}

bool Stack::find(const string &elem) const {
    vector<string>::const_iterator end_it = _stack.end();
    return ::find(_stack.begin(), end_it, elem)!=end_it;
}

int Stack::count(const string &elem) const {
    return ::count(_stack.begin(),_stack.end(), elem);
}

Stack.cpp

#include "Stack.h"
#include <iostream>
using namespace std;

int main(){

    Stack st;
    string str;
    while(cin>>str && ! st.full()){
        st.push(str);
    }
    if(st.empty()){
        cout<< "No strings were read!"<<endl;
        return 0;
    }
    st.peek(str);
    if(st.size()==1&&str.empty()){
        cout<<"No strings were read!"<<endl;
        return 0;
    }
    while(st.size()){
        if(st.pop(str)){
            cout<<str<<' ';
        }
    }
    

    return 0;
}

第六章6.1:将class改写成template

#include <iostream>
using namespace std;
/*
class example{
public:
    example ( double min, double max );
    example ( const double *array, int size );

    double& operator[] (int index);
    bool operator == ( const example& ) const;

    bool insert ( const double*, int );
    bool insert ( double );

    double min() const {
        return _min;
    }
    double max() const {
        return _max;
    }

    void min(double);
    void max(double);

    int count(double value) const;

private:
    int size;
    double *parray;
    double _min;
    double _max;
};
*/
// 以传址的方式减少对象复制带来不必要的开销,保证效率最佳
template <typename elemType> class example{
public:
    example (const elemType &min, const elemType &max);
    example (const elemType *array, int size);

    elemType& operator==(int index);
    bool operator==(const example1& ) const;

    bool insert(const elemType*, int);
    bool insert(const elemType);

    elemType min() const{
        return _min;
    } 
    elemType max() const{
        return _max;
    }

    void min(const elemType& );
    void max(const elemType& );

    int const(const elemType &value) const;

private:
    int size;
    double *parray;
    double _min;
    double _max;

};

int main(){

    return 0;
}

保证宏定义函数单句展开的do{…}while(0)技巧

在纯C语言的环境下宏定义函数会被经常使用,但是在C++当中,常用其他的方式(例如const static)代替,详细可以参见effective C++

但是在宏定义函数当中会出现一个问题,试着思考:

#define help(x) {\
    // 执行语句1; \
    // 执行语句2; \
} 

这里将help(x)函数拆分成两条语句进行宏定义,看似没有问题,但是如果出现以下的情况时候:

if(condition) 
    help(x);
else
    ……

help(x)会被自动展开为两条语句,此处if后面有没有接着大括号{}(可见统一标准编程风格习惯还是很重要的),就会出错!

所以将上面的宏定义函数定义成:

#define help(x) do{\
    // 执行语句1; \
    // 执行语句2; \
} while(0)

就可以完美解决刚才出现的问题,算是一个常用的小技巧吧!

 

 

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沉迷单车的追风少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值