C++捷径之七--结束

2024-01-07 21:18
文章标签 c++ 结束 之七 捷径

本文主要是介绍C++捷径之七--结束,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

// 异常处理 ,try,throw,catch
try{
  cout<<"Inside try block/n";
  throw
99 ;
  cout<<"This will not execute";
// throw 后,所以此句不执行
 }
 catch(int i){
  cout<<"Caught an exception--value is: "<<i<<endl;
// 得到 throw 扔出的值 99
 }
//throw 扔出的值 , catch 得到的值类型匹配
// 被扔出值后,程序就跳到 catch 语句进行处理。处理完后继续执行 catch 后面的语句。
class Myexception{
public:
 char str_what[80];
 Myexception() { *str_what=0; }
 Myexception(char *s) { strcpy(str_what,s); }
};// 定义一个类 Myexception
// 主函数中引用
 try{
  cout<<"Enter numerator and denominator: ";
  cin>>a>>b;
  if(!b)  throw Myexception("Cannot divide by zero!");
  else  cout<<"Quotient is: "<<a/b<<endl;
 }

 catch(Myexception e){
  cout<<e.str_what<<endl; }
// 由此处引用前面的扔出值
// 当有两个类,一个是另一个的派生类,此时,尽管扔出值可能是派生类,但是 catch 时定义的 // 基类也能接受,所以如果同时定义两个接受的类型,在前面的 catch 语句执行,将跳过后面的
catch(...){ }// 必须位于所有 catch 语句的后面。
// 另一种定义 throw 的类型确定
void Xhandler(int test) throw(int, char ,double)
{
 if(test==0)  throw test;
 if(test==1)  throw 'a';
 if(test==2)  throw 123.23;
}
// 用于判断是否分配空间成功
try{
 p=new int[32];//include <new>
 }
 catch(bad_alloc xa){
 cout<<"Allocation failure./n";
 return 1;
 }// 最后需要 delete []p; 释放空间
// 另一种判断方法
p=new(nothrow) int[32];
 if(!p){
 cout<<"Allocation failure./n";
 return 1;
 }
// 类中定义带有异常处理得 new
void *three_d::operator new(size_t size)
{
 void *p;
 
 cout<<"Allocating three_d object./n";
 p=malloc(size);
 if(!p){
  bad_alloc ba;
  throw ba;
 }
 return p;
}
//I/O 系统
ostream &operator<<( ostream &stream, three_d obj )
{
 stream << obj.x << ",";
 stream << obj.y << ",";
 stream << obj.z << "/n";
 return stream;
}// 重新释义 iostream 库中的的输出运算符 ostream<<
// 可以在类中定义友元
friend ostream &operator<<( ostream &stream, three_d obj);
istream &operator>>( istream &stream, three_d &obj)
{
 cout<<"Enter x,y,z values: ";
 stream >> obj.x >> obj.y >> obj.z;
 return stream;
}
// 重新释义 iostream 库中的的输出运算符 istream>>
// 明格式化 I/O
cout.setf( ios::showpos ); // 显示 +-
cout.setf( ios::scientific );
// 科学输入法
ios::fmtflags f;// 标准 IO 库中的 ios
cout.precision(2);
 cout.width(10);
 cout.fill('#');
#include <iomanip>
stream.setf(ios::right);
 stream<<setw(10)<<setfill('$');
// 写入一个文件 --12
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
 ofstream out("test "); 

//
创建 test 文件 . ofstream out("test .txt "); 创建的即为 test.txt 文本文件
 if(!out)
 {
  cout<<"cannot open file./n";
  return 1;
 }

 out<<10<<" "<<123.23<<endl;
 out<<"this is a short text file.";
//test 文件中写入 10,123.23,this is a short text file
 out.close();

  return 0;
}
int main()
{
 char ch;
 int i;
 float f;
 char str[80];
 ifstream in("test");
 if(!in)
 {
  cout<<"cannot open file./n";
  return 1;
 }
 in>>i;
 in>>f;
 in>>ch;
 in>>str;
// test 文件中读数据到 i,f,ch,str
  cout<<i<<" "<<f<<" "<<ch<<endl;
 cout<<str<<endl;

in.close();
 return 0;
}
//get() 出文件内容 --14
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
 char ch;
 
 argc=2; argv[1]="test";
 if(argc!=2)
 {
  cout<<"Usage: PR <filename>/n";
  return 1;
 }
 ifstream in(argv[1], ios::in | ios::binary);
 if(!in)
 {
  cout<<"cannot open file./n";
  return 1;
 }
 while(in)
 {
  in.get(ch);
  if(in)
   cout<<ch;
 }
in.close();
 return 0;
}
 //put() 入文件内容 --15
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
 char *p="hello there";
 ofstream out("test", ios::out|ios::binary);
 if(!out)
 {
  cout<<"cannot open file./n";
  return 1;
 }
 while(*p)
  out.put(*p++);
 return 0;
}
//read() write()--16
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
 int n[5]={1,2,3,4,5};
 register int i;
 ofstream out("test", ios::out|ios::binary);
 if(!out)
 {
  cout<<"cannot open file./n";
  return 1;
 }
 out.write( (char *)&n, sizeof n );
 out.close();
 for(i=0; i<5; i++)
  n[i]=0;
 ifstream in("test", ios::in|ios::binary );
 if(!in)
 {
  cout<<"cannot open file./n";
  return 1;
 }
 in.read( (char *)&n, sizeof n );
 for(i=0; i<5; i++)
  cout<<n[i]<<" ";
 in.close();
 return 0;
}

 

这篇关于C++捷径之七--结束的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/581255

相关文章

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

如何利用Java获取当天的开始和结束时间

《如何利用Java获取当天的开始和结束时间》:本文主要介绍如何使用Java8的LocalDate和LocalDateTime类获取指定日期的开始和结束时间,展示了如何通过这些类进行日期和时间的处... 目录前言1. Java日期时间API概述2. 获取当天的开始和结束时间代码解析运行结果3. 总结前言在J

c++中std::placeholders的使用方法

《c++中std::placeholders的使用方法》std::placeholders是C++标准库中的一个工具,用于在函数对象绑定时创建占位符,本文就来详细的介绍一下,具有一定的参考价值,感兴... 目录1. 基本概念2. 使用场景3. 示例示例 1:部分参数绑定示例 2:参数重排序4. 注意事项5.

使用C++将处理后的信号保存为PNG和TIFF格式

《使用C++将处理后的信号保存为PNG和TIFF格式》在信号处理领域,我们常常需要将处理结果以图像的形式保存下来,方便后续分析和展示,C++提供了多种库来处理图像数据,本文将介绍如何使用stb_ima... 目录1. PNG格式保存使用stb_imagephp_write库1.1 安装和包含库1.2 代码解

C++实现封装的顺序表的操作与实践

《C++实现封装的顺序表的操作与实践》在程序设计中,顺序表是一种常见的线性数据结构,通常用于存储具有固定顺序的元素,与链表不同,顺序表中的元素是连续存储的,因此访问速度较快,但插入和删除操作的效率可能... 目录一、顺序表的基本概念二、顺序表类的设计1. 顺序表类的成员变量2. 构造函数和析构函数三、顺序表

使用C++实现单链表的操作与实践

《使用C++实现单链表的操作与实践》在程序设计中,链表是一种常见的数据结构,特别是在动态数据管理、频繁插入和删除元素的场景中,链表相比于数组,具有更高的灵活性和高效性,尤其是在需要频繁修改数据结构的应... 目录一、单链表的基本概念二、单链表类的设计1. 节点的定义2. 链表的类定义三、单链表的操作实现四、

使用C/C++调用libcurl调试消息的方式

《使用C/C++调用libcurl调试消息的方式》在使用C/C++调用libcurl进行HTTP请求时,有时我们需要查看请求的/应答消息的内容(包括请求头和请求体)以方便调试,libcurl提供了多种... 目录1. libcurl 调试工具简介2. 输出请求消息使用 CURLOPT_VERBOSE使用 C