参考:
[1]输出缓冲区
重点:
1.缓冲区问题
2.指针占几个字节
3.B* q(new B);//q不是对象,q是个指针.
4.p=q:只是调用赋值运算符,没调用拷贝构造函数,因为没有创建新的对象
5.const char* 字符串类型指针
正文:
//达内C++教程\03_标准C++编程_陈宗权_7day\标准C++编程_day07PM_格式化输出_异常 TEST1
//
#include<iostream>
using namespace std;
#include<iomanip>
#include<windows.h>
#include<stdio.h>
int main()
{
//// 只能控制一个字符的宽度
//std::cout.width(10);
//std::cout << 123 << "," << 123 << std::endl;
//// 如果设定小于实际的字符,按实际字符输出
//std::cout.width(2);
//std::cout << 123 << "," << hex << 123 << std::endl;
//// 也可以通过函数进行设定宽度
//std::cout << setw(10) << 123 << " , " << dec << 123 << std::endl;
// // 通过函数的方式设置几进制
//std::cout.setf(ios::hex, ios::basefield);
//// showbase 把几进制打印出来
//std::cout <<showbase<<123 << std::endl;
//// 取消几进制显示
//std::cout.unsetf(ios::hex);
//std::cout << 123 << std::endl;
//std::cout << 95.0 << std::endl;
//std::cout << showpoint << 95.0 << std::endl;
//std::cout << scientific << 95.0 << std::endl;
//// 精确到3位数!!!
// std::cout.precision(3);
//std::cout << 95.0 << std::endl;
//std::cout.unsetf(ios::scientific);
//std::cout << 95.0 << std::endl;
////精确到小数点后3位,需要配合std::cout.precision(3);
//std::cout << fixed << std::endl;
//std::cout << 95.0 << std::endl;
// // 函数的方式精确到小数点后2位
//std::cout << setprecision(2) << 8000.0 << std::endl;
//// showpos 显示正负号
//std::cout << showpos << 123 << " , " << 45.6 << std::endl;
//// 变成大写
//std::cout <<uppercase<<hex<<123<<" , "<<scientific<<95.0 << std::endl;
//std::cout.fill('%');
//std::cout << dec << setw(10) << 123 << std::endl;
//// left 往左靠齐;internal:往两边靠
//std::cout << setfill('%') << left << setw(10) << 123 << std::endl;
//std::cout << setfill('*')<<internal<<setw(10)<<123<< std::endl;
//std::cout <<right<<setw(10)<<"hello" << std::endl;
// unitbuf:不要缓冲,只能放下一个单元,这就是说每输入一个,马上就会刷新,显示在屏幕上并进行清除缓冲区
// 那现在没触发就显示是什么回事呢
char* pbuffer = new char[512];
setvbuf(stdout, pbuffer,0,100);
// 这样不会立刻被刷新,只会在缓冲区中,刷新的标志:
// 第一个是缓冲区满了(时间到了或者钱总量到了),
// 或者是强制要其刷新,比如使用了endl,flush等等(你老婆叫你去存钱),或者设置了unitbuf位。
//std::cout << "hello";
std::cout << unitbuf << "hello";
Sleep(1000);
//cerr: 这样则是无缓冲,立即执行I/O操作
std::cout << flush << "world";
//cerr << "world";
system("pause");
}
//达内C++教程\03_标准C++编程_陈宗权_7day\标准C++编程_day07PM_格式化输出_异常 TEST1
//#include <iostream>
//#include <exception>
//using namespace std;
//class MyType {};
//
//class MyException: public exception {
//public:
// const char* what() const throw() { return "我的芙蓉花"; }
//
//};
//
////throw(double,int ,const char*,MyType,MyException) 抛出可能异常的类型声明,throw(),不没有任何异常
//// 异常对象,即使是个临时变量,也会保存到他处理之后为止,所以你在catch()可以用引用
//void f() throw(double,int ,const char*,MyType,MyException){
// std::cout << "请输入1个1~5的数";
// int n;
// cin >> n;
// if (n == 1) throw MyException();
// if (n == 2) throw MyType();
// if (n == 3) throw "hello";
// if (n == 4) throw 123;
// if (n == 5) throw 123.5;
// std::cout <<"====the end of f()====" << std::endl;
//}
//int main(){
//
// try {
// f();
// }
// //捕获列表,一遇到throw,直接跳入相应的catch,没有捕获,会导致程序终止
// catch (const char* e) { std::cout <<"const char* "<<e<< std::endl; }
// catch (const double e) { std::cout << "double " << e << std::endl; }
// catch (const MyType e) { std::cout << "MyType "<< std::endl; }
// catch (const MyException& e) { std::cout << "MyException " <<e.what()<< std::endl; }
// catch (const exception e) { std::cout << "exception " << e.what() << std::endl; }
// //任意类型的异常,
// catch (...) { std::cout << "其他异常"<< std::endl; }
// std::cout << "====the end of main()====" << std::endl;
// system("pause");
//}
//达内C++教程\03_标准C++编程_陈宗权_7day\标准C++编程_day07PM_格式化输出_异常 TEST2
//#include <iostream>
//#include <exception>
//using namespace std;
//class MyType {};
//
//class MyException : public exception {
//public:
// const char* what() const throw() { return "我的芙蓉花"; }
//
//};
//
////throw(double,int ,const char*,MyType,MyException) 抛出可能异常的类型声明,throw(),不没有任何异常
//// 异常对象,即使是个临时变量,也会保存到他处理之后为止,所以你在catch()可以用引用
//void f() throw(double, int, const char*, MyType, MyException) {
// std::cout << "请输入1个1~5的数";
// int n;
// cin >> n;
// if (n == 1) throw MyException();
// if (n == 2) throw MyType();
// if (n == 3) throw "hello";
// if (n == 4) throw 123;
// if (n == 5) throw 123.5;
// std::cout << "====the end of f()====" << std::endl;
//}
//int main() {
//
// try {
// f();
// }
// //捕获列表,一遇到throw,直接跳入相应的catch,没有捕获,会导致程序终止
// catch (const char* e) { std::cout << "const char* " << e << std::endl; }
// catch (const double e) { std::cout << "double " << e << std::endl; }
// catch (const MyType e) { std::cout << "MyType " << std::endl; }
// catch (const MyException& e) { std::cout << "MyException " << e.what() << std::endl; }
// catch (const exception e) { std::cout << "exception " << e.what() << std::endl; }
// //任意类型的异常,
// catch (...) { std::cout << "其他异常" << std::endl; }
// std::cout << "====the end of main()====" << std::endl;
// system("pause");
//}
////达内C++教程\03_标准C++编程_陈宗权_7day\标准C++编程_day07PM_格式化输出_异常 TEST3
//#include <iostream>
//#include <exception>
//#include <memory>
//using namespace std;
//
//class A {
//public:
// A() { std::cout <<"A()"<<this << std::endl; }
// ~A() { std::cout << "~A()" << this << std::endl; }
//};
//
//int main(){
// try {
// //A* p = new A;
// shared_ptr<A> p(new A);
// int n;
// cin >> n;
// if (n < 0) {
// throw 123;
// }
// std::cout << "n= " <<n<< std::endl;
// //delete p;
//
// }
// // 智能指针在抛出异常之前会自动调用析构函数进行释放
// catch (int e) { std::cout <<"exception: "<<e << std::endl; }
// system("pause");
//
//}
//达内C++教程\03_标准C++编程_陈宗权_7day\标准C++编程_day07PM_格式化输出_异常 TEST4
#include <iostream>
#include <exception>
#include <memory>
using namespace std;
class D {
public:
D() { std::cout << "D()" << this << std::endl; }
~D() { std::cout << "~D()" << this << std::endl; }
};
class C {
public:
shared_ptr<D> p;
C(int n)
{
//D* q = new D;
shared_ptr<D> q(new D);
if (n < 0) throw 888;
p = q;//只是赋值运算符函数p.operator=(q),并不会调用复制构造函数,因为没有调用构造函数,因为没有新建对象
//shared_ptr<D>p(q);建了一个对象,调用拷贝构造,这个没有疑问。
std::cout << "C()" << this << std::endl;
}
~C()
{
//delete p;
std::cout << "~C()" << this << std::endl;
}
};
int main() {
try {
//A* p = new A;
shared_ptr<C> p(new C(1));
int n;
cin >> n;
if (n < 0) {
throw 123;
}
std::cout << "n= " << n << std::endl;
//delete p;
}
// 智能指针在抛出异常之前会自动调用析构函数进行释放
catch (int e) { std::cout << "exception: " << e << std::endl; }
system("pause");
}
网友评论