`
yunchow
  • 浏览: 317661 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

c++链表 异常 内部类 输出格式控制

    博客分类:
  • C++
阅读更多
C(控制台)    F(文件)
---------------------------------------------
* I
	 cin			ifstream fin(文件名)

				fin.close()
	cin>>			fin>>
	cin.get(/*char&*/)	fin.get(/*char&*/)
	getline(cin/fin,str,'/*结束符*/')
	read(内存地址,字节数)//此时注意长度
	read()不会自动加'\0'
	clear()恢复错误状态,ignore()清缓冲
	peek()  putback()把读出的字符放回缓冲区

----------------------------------------------
* O
	cout			ofstream fout(文件名)
	cerr/clog		fout.close()
	<<
	put()
	write(内存地址,字节数)把内存中的数写到文件中
	string str;
	fin.read((char*)&str,sizeof(str));//结果不一定是想要的.
	如果要读的是字符就用字符数组
fin.gcount()//最近读到多少个字符
fin.eof()//判断是否读到了文件末尾

控制台与文件的输入输出是完全一样的.
EOF end of file  代表-1
================================================
* 输出格式控制

只对格式化输出有效 <<
指定输出宽度:cout.width(int),一次性的,只对下一个输出有效
cout.fill('&')//在空白地方填充指定字符,不是一次性的
//例子
#include <iostream>
using namespace std;

class wf{
        int w;
        char ch;
public :
        wf(int w, char c ):w(w),ch(c){}
        friend ostream& operator<<(ostream& os, const wf& o){
                os.width(o.w);
                os.fill(o.ch);
                return os;
        }
};

int main()
{
        cout << wf(10,'*') << 123 << endl;
        cout << 123 << endl;
}


设置输出精度
cout.precision(8);
cout.setf()//控制输出格式

添加控制标志
cout.setf(ios::left|ios::hex)

常见输出控制标志:
ios:right,ios:dec(十进制),ios:oct(八进制),ios:hex,ios::showbase(带

前缀,如ox),ios::showpoint(总带小数点),ios::scientific(科学计数法)
ios::showpos(positive带符号),ios::uppercase(大写,把十六进制和科学计


数输出A~F时变大写)

cout.precision(2);
cout.setf(ios::fixed);//合起来用,设置小数后几位.

cout.unsetf(标志)//清除标志


#include <iostream>
using namespace std;

int main()
{
        cout.width(10);
        cout.fill('*');
        cout.setf(ios::left);
        cout << 123 << endl;
        cout.setf(ios::showpos);
        cout << 123 << endl;
        cout.setf(ios::showpoint);
        cout << 123.0 << endl;
        cout.setf(ios::scientific|ios::uppercase);
        cout << 1234.0 << endl;
        cout.precision(2);
        cout.unsetf(ios::scientific);
        cout.setf(ios::fixed);
        cout << 123.0 << endl;
        cout.unsetf(ios::showpos);
        cout.unsetf(ios::dec|ios::oct);
        cout.setf(ios::hex);
        cout.setf(ios::showbase);
        cout << 123 << endl;
        cout << "hello,world" << endl;
}


输出控制符
flush:清空缓冲
endl:insert a '\n' ,and clear out buffer
oct : set the output as oct
dec :十进制输出
hex: 十六进制输出
cout.setf(ios::left);//cout<<left与其等价
setw(宽度) ----> width
setfill(填充字符) ---> fill
setprecision(精度) ---> precision


注意:要用以上格式要包含头文件<iomanip>

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
        cout << setw(10) << setfill('~') << left << 123 << endl;
        cout << hex << showbase << uppercase << 123 << endl;
}

===============================================
ofstream fout(文件名,方式/*默认ios::out*/);
默认会清除文件中原内容
打开方式:ios::app(追加内容)
ios::binary:open file in binary mode;原样读写
ios::ate:start reading or writing at end of file
ios::in:ipen for reading
ios::trunc:truncate file to zero length if it exitsts
ios::out:open for writing

seekg(位置int);//读定位
seekp(位置int);//写定位
tellp()
tellg()//返回当前位置

//既可读也可写
fstream fio(文件名,ios::in|ios::out);
一般提倡读写分开.
+ 保存对象
A a(23);
fout.write((char*)&a,sizeof(a));
有一点如果要保存string对象时要先将其转换成c_str风格再转.
因为string里是一个指针,它的长度是4,如果保存的话会是一个地址,是毫无意

义的.
用法:fout.write((char*)strngObj.c_str(),strlen(strngObj));
-------------------------------
* 内部类

  在一个作用域范围内定义的类叫做内部类.我们可以在一个函数里定义一个类

,也可以在一个类里定义一个内部类.
#include <iostream>
using namespace std;
class A /*全局类*/ {
public:
	class B /*(成员)内部类*/ { };
	//int b;//成员变量
};

class B /*可以叫B*/ { };

class D {
public :
	class B { };
};

int main()
{
	class C { };//(局部)内部类
	//int c;//局部变量
	C objc;
	A obja;
	A::B objab;//内部类的访问方法.
	B objb;
	D::B objdb;
}

内部类的作用:隐藏名字,避免冲突.可以通过其中统一的内部类名来统一各个类

的操作方法.其中最著名的就是迭代器.其实迭代器是类的内部类.
在c++里内部类与外部类毫不相干.
-------------------------------------------------
* 异常

如果不用异常程序各处会出现错误处理代码,使程序显的混乱,而异常处理将会

把错误集中处理,让程序结构清晰起来.
异常是不常见,但无法避免,可预料并作准备.
try {
	if( 有异常情况 )
		throw 数据;//在c++里什么都可以抛
}//监视是否有数据抛出.
catch/* 扑获异常 */( 类型 变量名 ) {

	/* 对异常处理code */
}//catch是用来集中处理错误信息的
catch( 类型 变量名 ) {

	/* 对异常处理code */
}//可以有若干个catch,对不同的异常进行处理.

c++的异常处理就像小两口打架,想抛什么就抛什么.
//例子,异常处理
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char* argv[])
{
     try {
	ifstream fin(argv[1]);
	if(!fin)
		throw 100;
	char buf[1000];
	fin.read(buf,1000);
	if(!fin)
		throw 100.0;
	cout.write( buf, 1000);
	fin.close();
	
     }catch(double e) {
	cout << "double:" << e << endl;
     }catch(long e) {
	cout << "long:" << e << endl;
     }catch(...)/*接收所有的异常*/ {
	cout << "Exception Error!" << endl;
     }
     cout << "What a good day!" << endl;
		
}


exception类有一个成员函数what()用来返回描述异常的字符串e.what();
在一个函数里如果有没有被捕获的异常会被抛到调用它的地方.
最终会传到main函数里面.所以我们在main里写上下面语句块:
try { } catch(...) { } 这样就能处理所有的异常.
抛出子类型对象异常,可以用父类对象来进行捕获.当一起进行捕获,但要注意先

后顺序,一般是先catch子类再catch父类对象.
catch( 类型 变量 ) {
throw;
}//处理不了,重新抛出该异常.一般不这么做,偶尔做一做
其中的类型可以是自定义类型,同样可以向外抛.
====================================================

* 重要数据结构与算法

  + 链表
结构中也可以写构造函数,结构与类的区别最主要的区别就是结构中默认的成员

是public型的,而类中成员默认是private型的.其他的大致相同.
基本类型()的值是0
T(),T是基本类型时值是0,T是类时T()会是一个无名对象.
当然编译器有可能会优化程序.

//链表示例
#include <iostream>
using namespace std;
typedef int T;

int main()
{
        struct Node {
                T data;
                Node* next;
                Node(const T& d) : data(d),next(NULL){ }
        };
        Node* header = NULL;
        cout << "Please input some integers : " << endl;
        do{
                T data;
                cin >> data;
                //cout << (int)data << ' ';
                Node* p = new Node(data);
                p->next = header;
                header = p;
        }while(cin.get()!='\n');
        Node* p = header;
        while(p){
                cout << p->data << ' ';
                p = p->next;
        }
        cout << endl;
	p = header;
        while(p){
                Node* q = p->next;
                delete p;
                p = q;
        }
}


//链表类示例程序
//功能:支持增,删,改,查
#include <iostream>
using namespace std;
typedef int T;

class List {
        struct Node {
                T data;
                Node* next;
                Node( const T& t=T()) : data(t),next(NULL){ }
        }; //end of struct Node

        Node* head ;

public:
        List() : head(NULL){ }
        void insertFirst( const T& t ){
                Node* p = new Node(t);
                p->next = head;
                head = p;
        }
        void travel(){
                Node* p = head;
                while(p){
                        cout << p->data << ' ' ;
                        p = p->next;
                }
                cout << endl;
        }
        void free(){
                while(head){
                        Node* q = head->next;
                        delete head;
                        head = q;
                }
        }
        int size(){
                Node* p = head;
                int cnt=0;
                while(p){
                        p = p->next;
                        cnt ++;
                }
                return cnt;
        }
        ~List(){
                free();
                cout << "~List() called" << endl;
        }
        int find( const T& t ){
                Node* p = head;
                int cnt = 0;
                while(p){
                        if(p->data==t) return cnt;
                        p = p->next;
                        cnt++;
                }
                return -1;
        }
        T getHead(){
                if(!head) throw "Exception: No head";
                return head->data;
        }
        T getTail(){
                if(!head) throw "Exception: No tail";
                Node* p = head;
                while(p->next)
                        p = p->next; 
                return p->data;
        }
        T get( int index ){
                Node* p = getPointer(index);
                return p->data;
        }
        bool erase( int index ){
                if( 0== index ){
                        Node* p = head;
                        head = head->next;
                        delete p;
                        return true;
                }
                else{
                        Node* pre = getPointer(index-1);
                        Node* cur = pre->next;
                        pre->next = cur->next;
                        delete cur;
                        return true;
                }
                return false;
        }
        bool update(const T& old, const T& nw){
                int index=find(old);
                if(index<0)
                        return false;
                Node* p = getPointer(index);
                p->data = nw;
                return true;
        }
private :
        Node* getPointer(int index){
                if(index<0 || index>=size()){
                        throw "Exception:index out of bound" ;
                }
                Node* p = head;
                for( int i=0; i<index; i++)
                        p = p->next;
                return p;
        }
};//end of class List
int main()
{
        List list;
        list.insertFirst(1);
        list.insertFirst(2);
        list.insertFirst(3);
        list.insertFirst(4);
        list.insertFirst(5);
        list.travel();
        cout << "list.size():" << list.size() << endl;
        cout << "list.find(2):" << list.find(2) << endl;
        cout << "list.find(9):" << list.find(9) << endl;
        cout << "getHead:" << list.getHead() << endl;
        cout << "getTail:" << list.getTail() << endl;
        cout << "get(3):" << list.get(3) << endl;
        try{
                cout << "get(9):" << list.get(9) << endl;
        }
        catch(...){
                cout << "Exception" << endl;
        }
        //cout << "get(5):" << list.get(5) << endl;//ERROR
        cout << "------------------" << endl;
        list.erase(0);
        list.travel();
        try{
                list.erase(2);
        }
        catch(...){cout << "Exception" << endl;}
        list.travel();
        list.update(1,100);
        list.travel();
}

0
0
分享到:
评论

相关推荐

    谭浩强C语言程序设计,C++程序设计,严蔚敏数据结构,高一凡数据结构算法分析与实现.rar

    *5.6 C++处理字符串的方法——字符串类与字符串变量 5.6.1 字符串变量的定义和引用 5.6.2 字符串变量的运算 5.6.3 字符串数组 5.6.4 字符串运算举例 习题 第6章 指针 6.1 指针的概念 6.2 变量与指针 6.2.1 定义...

    基于C++实现的产品入库管理操作系统

    文件操作:利用C++的文件输入输出流进行数据的持久化存储。 GUI设计:可能需要用到Qt或其他C++图形界面库来设计用户交互界面。 数据结构与算法:运用合适的数据结构(如链表、树、哈希表)来高效地存储和检索数据。 ...

    新手学习C++入门资料

    C++中用来做控制态输入输出的iostream类库替代了标准C中的stdio函数库。 C++中的try/catch/throw异常处理机制取代了标准C中的setjmp()和longjmp()函数。 二、关键字和变量 C++相对与C增加了一些关键字,如下: ...

    C++和面向对象数值计算

    4.6.2 浮点数格式化输出 4.6.3 输出宽度 4.6.4 文件的输入输出 4.6.5 字符的输入输出 4.6.6 字符串流 4.7 非线性方程的迭代解法 4.7.1 二分法 4.7.2 牛顿法 4.8 练习 第5章 类 5.1 类的...

    C++ STL 开发技术导引(第6章)

    3.4 流的格式控制 41 3.5 本章小结 45 第二篇 C++ STL泛化技术基础 第4章 C++ STL泛型库概述 48 4.1 C++ STL的发展历程 48 4.2 C++ STL的各种实现版本 49 4.2.1 HP STL 49 4.2.2 SGI STL 50 4.2.3 ...

    谭浩强C语言程序设计,C++程序设计,严蔚敏数据结构,高一凡数据结构算法分析与实现.rar )

    *5.6 C++处理字符串的方法——字符串类与字符串变量 5.6.1 字符串变量的定义和引用 5.6.2 字符串变量的运算 5.6.3 字符串数组 5.6.4 字符串运算举例 习题 第6章 指针 6.1 指针的概念 6.2 变量与指针 6.2.1 定义...

    C++MFC教程

    Visual C++MFC入门教程 目录 +-- 第一章 VC入门 |------ 1.1 如何学好VC |------ 1.2 理解Windows消息机制 |------ 1.3 利用Visual C++/MFC开发Windows程序的优势 |------ 1.4 利用MFC进行开发的通用方法介绍 |----...

    C++ STL开发技术导引(第5章)

    3.4 流的格式控制 41 3.5 本章小结 45 第二篇 C++ STL泛化技术基础 第4章 C++ STL泛型库概述 48 4.1 C++ STL的发展历程 48 4.2 C++ STL的各种实现版本 49 4.2.1 HP STL 49 4.2.2 SGI STL 50 4.2.3 ...

    C++ STL开发技术导引(第3章)

    3.4 流的格式控制 41 3.5 本章小结 45 第二篇 C++ STL泛化技术基础 第4章 C++ STL泛型库概述 48 4.1 C++ STL的发展历程 48 4.2 C++ STL的各种实现版本 49 4.2.1 HP STL 49 4.2.2 SGI STL 50 4.2.3 ...

    传智播客扫地僧视频讲义源码

    15_复数类_所有函数都写在类的内部 16_复数类_所有函数都写在类的外部_上 17_复数类_所有函数都写在类的外部_下 18_复数类_所有函数都写在类的外部(h和cpp分开) 19_类模板中的static关键字 20_案例_数组模板类_需求...

    最新名企标准通用C++面试题,

    如果类的成员函数是一个callback函数, 必须宣告它为"static",才能把C++ 编译器加诸于函数的一个隐藏参数this去掉。 11.C++里面是不是所有的动作都是main()引起的?如果不是,请举例。 不是的,C++里面有些...

    C语言解析教程(原书第4版)(美) 凯利.pdf

    13.10 c++的异常 13.11 面向对象编程的优点 13.12 总结 13.13 练习 第14章 从c到java 14.1 输出 14.2 变量和类型 14.3 类和抽象数据类型 14.4 重载 14.5 类的创建和销毁 14.6 面向对象编程和继承 14.7 多态和重写...

    JAVA面试题最全集

    一个“.java”原文件中是否可以包括多个类(不是内部类)? 53.掌握内部类和接口的概念 54.StringTokenizer类的使用 55.数据结构,如何遍历List中的元素? 如果要按照键值保存或者访问数据,使用什么数据结构? ...

    java 面试题 总结

    Static Nested Class是被声明为静态(static)的内部类,它可以不依赖于外部类实例被实例化。而通常的内部类需要在外部类实例化后才能实例化。 22、JSP中动态INCLUDE与静态INCLUDE的区别? 动态INCLUDE用jsp:...

Global site tag (gtag.js) - Google Analytics