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++ vector的常见用法超详细讲解

《C++vector的常见用法超详细讲解》:本文主要介绍C++vector的常见用法,包括C++中vector容器的定义、初始化方法、访问元素、常用函数及其时间复杂度,通过代码介绍的非常详细,... 目录1、vector的定义2、vector常用初始化方法1、使编程用花括号直接赋值2、使用圆括号赋值3、ve

如何高效移除C++关联容器中的元素

《如何高效移除C++关联容器中的元素》关联容器和顺序容器有着很大不同,关联容器中的元素是按照关键字来保存和访问的,而顺序容器中的元素是按它们在容器中的位置来顺序保存和访问的,本文介绍了如何高效移除C+... 目录一、简介二、移除给定位置的元素三、移除与特定键值等价的元素四、移除满足特android定条件的元

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(

C++变换迭代器使用方法小结

《C++变换迭代器使用方法小结》本文主要介绍了C++变换迭代器使用方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1、源码2、代码解析代码解析:transform_iterator1. transform_iterat

详解C++中类的大小决定因数

《详解C++中类的大小决定因数》类的大小受多个因素影响,主要包括成员变量、对齐方式、继承关系、虚函数表等,下面就来介绍一下,具有一定的参考价值,感兴趣的可以了解一下... 目录1. 非静态数据成员示例:2. 数据对齐(Padding)示例:3. 虚函数(vtable 指针)示例:4. 继承普通继承虚继承5.

C++中std::distance使用方法示例

《C++中std::distance使用方法示例》std::distance是C++标准库中的一个函数,用于计算两个迭代器之间的距离,本文主要介绍了C++中std::distance使用方法示例,具... 目录语法使用方式解释示例输出:其他说明:总结std::distance&n编程bsp;是 C++ 标准

C++ 中的 if-constexpr语法和作用

《C++中的if-constexpr语法和作用》if-constexpr语法是C++17引入的新语法特性,也被称为常量if表达式或静态if(staticif),:本文主要介绍C++中的if-c... 目录1 if-constexpr 语法1.1 基本语法1.2 扩展说明1.2.1 条件表达式1.2.2 fa