0427 嵌入式学习笔记 (31)模板

#include<iostream>
using namespace std;

template <class T>
class Queue
{
	public:
		Queue(int size)
			{
			 		len = 0;
					size = 30;
					dp = new T[size];
					start = 0;
					end = -1;
			}
			void push(T dat);//入队操作
			T pop();//出队操作
			void travel();//遍历操作
			int getlength();
			~Queue();
	private:
		int len;
		T *dp;
		int size;
		int start;
		int end;
};

template <class T>
void Queue<T>::push(T dat)
{
	if(len == size)
	{
		printf("queue full!\n");
		return;
	}
	end++;
	end = end % size;
	dp[end] = dat;
	len++;
}

template <class T>
T Queue<T>::pop()
{
	if(len == 0)
	{
		printf("queue empty!\n");
		exit(1);
	}
	T temp = dp[start++];
	start = start % size;
	len--;
	return temp;
}

template<class T>//模板T
void Queue<T>::travel()//遍历函数
{
	int length = len;
	cout<<"================"<<endl;
	printf("size-%d\n",len);
	while(length != 0)
	{
		printf("%d",dp[start++]);
		start = start % size;
		length--;
	}
	printf("\n===============\n");
}

template<class T>
int Queue<T>::getlength()
{
	return len;
}

template<class T>
Queue<T>::~Queue()
{
	delete []dp;
}
int main()
{
	Queue<int> queue(13);
	for(int i = 0;i < 13;i++)
	{
		queue.push(i+1);
	}

    int count = 0;
    while(queue.getlength() !=1)
    {
        count++;
        int temp = queue.pop();
        if(count % 3 ==0)
        {
            cout<<temp<<"is killed!"<<endl;
        }
        else
        {
            queue.push(temp);
        }
    }
    cout<<queue.pop()<<"is alive!"<<endl;

	return 0;
}

在这里插入图片描述

#include<iostream>
using namespace std;

int divide(int a,int b)
{
    char c = 'a';
    if(b == 0);
    throw c;
    return a / b;
}

void myunexpected()
{
    printf("error exception!\n");
    throw 0;
}

int main()
{
    set_unexpected(myunexpected);
    try{
        cout<<divide(7,9)<<endl;
        cout<<divide(9,0)<<endl;
        cout<<divide(63,9)<<endl;
        }
        catch(char a)
        {
            cout<<a<<" is divided by zero"<<endl;
        }
    return 0;
}

在这里插入图片描述

try
{
//有可能出现异常的语句
}

catch
{
//处理异常的语句
}

throw

本质:log日志

exception

#include<iostream>
using namespace std;

class myexception
{
	public:
		myexception(const char *msg)
		{
				cout<<"error constructor!"<<endl;
				strcpy(this->msg,msg);
		}
		const char * getmessage()const
		{
			return msg;
		}
	private:
		char msg[100];
};

class demo
{
	public:
		demo()
		{
			cout<<"constructor!"<<endl;
		}
		~demo()
		{
			cout<<"disconstructor!"<<endl;
		}
};

void fun()
{
	demo d;
	throw myexception("throw fun exception!");
}
int main()
{
	try
	{
		fun();
	}
	catch(myexception &e)
	{
		printf("the exception is %s\n",e.getmessage());
	}
	cout<<"main end!"<<endl;
	
	return 0;
}

异常处理的好处:在抛出异常之后,会将之前创造的类的对象进行释放

创建自定义的异常释放
栈的解旋

logic_error:逻辑错误
runtime_error:运行时错误
bad_alloc:错误的内存分配

length_error:最大长度错误
out_of_range:超出有效范围
invalid_argument:参数不合适

常用cin cout输出流函数

#include<iostream>
#include<unistd.h>
using namespace std;


int main()
{
    int a = 23;
    cout.setf(ios::showbase);
    cout << "dec " << a << endl;
    cout.unsetf(ios::dec); //取消十进制

    cout.setf(ios::hex);//十六进制输出
    cout << "hex: " << a << endl;
    cout.unsetf(ios::hex);

    cout.setf(ios::oct);
    cout << "oct: " << a << endl;
    cout.unsetf(ios::oct);

    const char *ptr = "china";
    cout.width(10);
    cout << ptr << endl;

    cout.width(10);
    cout.fill('=');
    cout << ptr << endl;

    double pi = 22.0/7.0;
    cout << pi << endl;
    cout.setf(ios::scientific);//以科学计数法输出
    cout << pi << endl;
    cout.unsetf(ios::scientific);

    cout.precision(20);//以小数点后20位输出
    cout << pi << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值