数据结构算法与应用c++语言描述 原书第二版 答案(更新中
2024-08-29 22:45:47
目录
第一章 C++回顾
函数与参数
1.交换两个整数的不正确代码。
//test_1
void swap(int x,int y)
{
int temp=x;
x=y;
y=temp;
}
void swap2(int& x,int& y)
{
int temp=x;
x=y;
y=temp;
}
void test_1()
{
int x=3,y=5;
swap(x,y);//error C2668: “swap”: 对重载函数的调用不明确.将void swap(int& x,int& y)改成void swap2(int& x,int& y)
cout<<x<<y<<endl;//35
int& a=x,b=y;//这里b是int。传值参数。int& a=3,&b=y;//这里b是int&。引用参数
cout<<a<<b<<endl;//35
swap2(a,b);
cout<<x<<y<<endl; //55,只有a改变了。
}
异常
10.抛出并捕捉整型异常。
int abc(int a,int b,int c)
{
if(a<0&&b<0&&c<0)
throw 1;
else if(a==0&&b==0&&c==0)
throw 2;
return a+b*c;
}
void test_10()
{
try
{
cout<< abc(2,0,2)<<endl;
cout<< abc(-2,0,-2)<<endl;
cout<< abc(0,0,0)<<endl;
}
catch(exception& e)
{
cout<<"aa "<<endl;
}
catch(int e)
{
if (e==2)
{
cout<<"e==2 "<<endl;
}
if (e==1)
{
cout<<"e==1 "<<endl;
}
}
catch(...)
{
cout<<"..."<<endl;
}
}
输出
如果把catch(..)放在最前面会报错。error C2311: “int”: 在行 41 上被“...”捕获
因为这是捕获所有的,所以一般放最后。
catch(...)
{
cout<<"..."<<endl;
system("pause");
return 1;
}
catch(int e)
{
if (e==2)
{
cout<<"e==2 "<<endl;
}
if (e==1)
{
cout<<"e==1 "<<endl;
}
system("pause");
return 1;
}
最新文章
- 用sublime写出的第一个网页
- 素数筛 uva 543
- 想抛就抛:Application_Error中统一处理ajax请求执行中抛出的异常
- Java NIO 概述
- Oracle Database 11g express edition
- Windows 2003 FastCgi安装环境
- 使用 Android Studio 跑新浪微博SDK Demo遇到的问题及解决
- ASP.NET树形控件TreeView的递归绑定
- POJ 1182 (经典食物链 /并查集扩展)
- 再回首,Java温故知新(十一):Java反射
- TreeView绑定无限层级关系类
- 8.4 sikuli 集成进eclipse 报错:Unsupported major.minor version 51.0
- 把Ubuntu用户目录下的目录名改成英文
- LinkQueue(链队列)
- 【Matlab&;Mathematica】对三维空间上的点进行椭圆拟合
- SonarQube 集成 GitLabCI
- SQL获取当前时间月份为两位数
- TreeSet多字段排序
- Weblogic环境(JSP)文件下载问题(下载的文件与原文件大小不一致问题)
- C# 调节图片亮度