做网站需要简介,作一个网站要多少钱,wordpress 代码执行,网站建设规范文件操作
#includeiostream
#includestring
#includefstream
using namespace std;
//文本操作
//程序运行时产生的数据都属于临时数据#xff0c;程序一旦运行结束都会被释放
//通过文件可以数据持久化
//c中对文件操作包含头文件fstream
/…文件操作
#includeiostream
#includestring
#includefstream
using namespace std;
//文本操作
//程序运行时产生的数据都属于临时数据程序一旦运行结束都会被释放
//通过文件可以数据持久化
//c中对文件操作包含头文件fstream
//文件类型分为两种
//1.文本文件 -文件以文本的ASCLL码形式存储在计算机中
//2.二进制文件 - 文件以文本的二进制形式存储在计算机中用户一般不能直接读懂它们
//操作文件的三大类
//1.ofstream写操作
//2.ifstream读操作
//3.fstream: 读写操作
//写文件
//1.包含头文件
//#includefstream
//2.创建流对象
//ofstream ofs;
//3.打开文件
//ofs.open(文件路径,打开方式);
//文件打开方式
//ios::in 读文件方式打开
//ios::out 写文件方式打开
//ios::ate 初始位置文件尾
//ios::app 追加文件方式打开
//ios::trunc 如果文件存在先删除再创建
//ios::binary 二进制方式
//文件打开方式可以配合使用利用|操作符
//4.写数据
//ofs写入的数据;
//5.关闭文件
//ofs.close();
void test(){//1.包含头文件//2.创建流对象 ofstream ofs;//3.指定打开方式ofs.open(text.txt,ios::out);//4.写内容ofs姓名张三endl;ofs性别男endl;ofs年龄25endl; //5.关闭文件 ofs.close();
}
//读文件
//1.包含头文件
//#includefstream
//2.创建流对象
//ifstream ifs;
//3.打开文件并判断文件是否打开
//ifs.open(文件路径,打开方式);
//4.读数据
//四种方式读取
//5.关闭文件
//ifs.close()
void test1(){//1、包含头文件//2、创建流对象ifstream ifs; //3、打开文件并判断文件是否打开ifs.open(text.txt,ios::in);if(!ifs.is_open()){cout文件打开失败endl;return;}//4、读数据//第一种 char buf[1023] {0};while(ifs buf) {coutbufendl;}
// //第二种
// char buf[1024] {0};
// while(ifs.getline(buf,sizeof(buf))) {
// coutbufendl;
// }
// //第三种
// string buf;
// while(getline(ifs,buf)){
// coutbufendl;
// } //第四种
// char c;
// while((c ifs.get()) ! EOF){ //EOF end of file
// coutc;
// } //5、关闭文件 ifs.close();
}
//二进制文件
//以二进制的方式对文件进行读写操作
//打开方式要指定为ios::binary
//二进制文件写文件
//二进制方式写文件主要利用流对象调用成员函数write
//函数原型ostreamwrite(const char *buffer,int len);
//参数解释字符指针buffer指向内存中一段存储空间。len是读写的字节数
class Person{public:char m_Name[64];int m_Age;
};
void test2(){//1.包含头文件//2.创建流对象ofstream ofs; //3.打开文件ofs.open(person.txt,ios::out|ios::binary);//创建流对象和打开文件一起
// ofstream ofs(person.txt,ios::out|ios::binary);//4.写文件Person p {张三,18} ;ofs.write((const char *)p, sizeof(p));//5.关闭文件 ofs.close();}
//二进制文件读文件
//函数原型istreamread(char *buffer,int len);
class Person1{
public: char m_Name[64];int m_Age;
};
void test3(){//1.包含头文件//2.创建流对象ifstream ifs; //3.打开文件 判断文件是否打开成功 ifs.open(person.txt,ios::in|ios::binary);if(! ifs.is_open()){cout文件打开失败endl; return;}//创建流对象和打开文件一起
// ifstream ofs(person.txt,ios::out|ios::binary);//4.读文件Person1 p;ifs.read((char *)p, sizeof(Person1));cout姓名p.m_Name 年龄p.m_Ageendl; //5.关闭文件 ifs.close();
}
int main(){//文本文件写 test();//文本文件读 test1();//二进制文件写 test2();//二进制文件读test3();
}
模板
#includeiostream
#includestring
using namespace std;//1.2函数模板语句
//c另一种编程思想称为泛型编程主要利用的技术就是模板
//c提供两种模板机制函数模板和类模板
//1.2.1函数模板作用
//建立一个通用函数其函数返回值类型和形参类型可以不具体制定用一个虚拟的类型来代表
//语法 templatetypename T
// 函数声明或定义
//template --- 声明创建模板
//typename --- 表明其后面的符号是一种数据类型可以用class代替
//T --- 通用的数据类型名称可以替换通常为大写字母 //函数模板
templatetypename T void mySwap(T a,T b)
{T temp a;a b;b temp;
}
两个整型交换函数
//void swapInt(int a,int b)
//{
// int temp a;
// a b;
// b temp;
//}
交换两个浮点型函数
//void swapDouble(double a,double b)
//{
// double temp a;
// a b;
// b temp;
// } void test01(){cout************test01**********endl***********函数模板*********endl; int a 10;int b 20;
// swapInt(a,b);
// couta a b bendl;
//
// double c 1.1;
// double d 2.2;
// swapDouble(c,d);
// coutc c d dendl;//函数模板两种使用方法 //1、自动类型推导mySwap(a,b);couta a b bendl;//2、显示指定类型mySwapint(a,b); couta a b bendl;}//1.2.2 函数模板注意事项//注意事项//1、自动类型推导必须导出一致的数据类型T才可以使用//2、模板必须要确定出T的数据类型才可以使用//template class T//void func()
// {
// coutfunc 调用endl;
// }
//调用func()是错误的必须明确func的数据类型 //1.2.3 函数模板案例
//案例描述
//利用函数模板封装一个排序的函数。可以对不同类型数组进行排序
//排序规则从大到小排序算法为选择排序
//分别利用char数组和int数组进行测试 //排序算法
template typename T
void mySort(T arr[], int len)
{for(int i 0;i len;i){int max i;for(int j i1; jlen; j){if(arr[max]arr[j]) // 此处是max 不是 i需要实时更新max的值 {max j;}}if(max!i){mySwap(arr[i],arr[max]);}}} // 6,432,432,32,32
// i 0; max 0 j 1 32
// i 1; max 1 j 2 432
// i 2; max 2 j 3 432
// i 3; max 3 j 4 max 4 2
// i 4; max 4 j 5 max 4 1//打印 template typename Tvoid show(T arr[],int len){for(int i 0; i len;i){coutarr[i] ; }coutendl; }void test02()
{cout************test02**********endl********函数模板测试排序*******endl;char charArr[] fdacbeg;int intArr[] {6,432,432,32,32};int len sizeof(charArr)/sizeof(charArr[0]);int size sizeof(intArr)/sizeof(intArr[0]);show(charArr,len);mySort(charArr,len); show(charArr,len); show(intArr,size);mySort(intArr,size); show(intArr,size);
}
//1.2.4 普通函数与函数模板的区别
//普通函数与函数模板区别
//普通函数调用时可以发生自动类型转换(隐式类型转换)
//函数模板调用时如果利用自动类型推导不会发生隐式类型转换
//int myAadd(int a, int b)
//{
// return a b;
// }
// myAadd(10,c);//不会报错隐式类型转换转为ACI码相加
//templatetypename T
//T myAdd02(T a, T b)
//{
// return a b;
//}
// myAadd02(10,c);//会报错隐式类型不能转换
//1.2.5普通函数与函数模板的调用规则
//调用规则如下
//1.如果函数模板和普通函数都可以实现优先调用普通函数
//2.可通过空模板参数列表来强调调用函数模板
//3.函数模板也可以发生重载
//4.如果函数模板可以产生更好的匹配优先调用函数模板 //void myPrint(int a,int b)
//{
// cout调用的模板endl;
// }
//
//templateclass T
//void myPrint(T a,T b)
//{
// cout调用的模板endl;
//}
//templateclass T
//void myPrint(T a,T b , T c)
//{
// cout调用的重载的模板endl;
//
//void test01()
//{
// int a 10;
// int b 20;
//
// myPrint(a,b);//优先调用普通函数
//通过空模板参数列表强制调用函数模板
// myprint(a,b);
// myprint(a,b,c); 函数模板也可以发生重载 //如果函数模板产生更好的匹配优先调用函数模板
// char c1 a;
// char c2 b;
// myprint(c1,c2); // 调用的是模板
//}
//1.2.6模板的局限性
//模板的通用并不是万能的
//例如
//如果传入的 a 和 b 是一个数组就无法实现了
//templateclass T
//void f(T a,T b)
//{
// a b;
//}
//再例如
//templateclass T
//void f(T a, T b)
//{
// if(a b){
//
// }
//}
//在上述代码中如果T的数据类型传入的是像Person这样的自定义数据类型也无法正常运行
class person{
public:person(string name,int age){this-myname name;this-myage age;}string myname;int myage;
};
templateclass T
bool equals (T a,T b)
{if(a b){return true ;}else{return false;}
}
//利用具体化person的版本实现代码具体化优先调用
templatebool equals(person p1,person p2){if(p1.myname p2.myname p1.myage p2.myage){return true;}else{return false;}
} void test03()
{cout************test03**********endl********模板的局限性*******endl;int a 10;int b 10;int ret equals (a,b);if(ret){couta等于bendl; }else{couta不等于bendl;}
}void test04()
{cout************test04**********endl********模板的局限性*******endl;person p1(tom,10);person p2(tom,10);bool ret equals(p1,p2);if(ret){coutp1 等于 p2endl; }else{coutp1 不等于 p2endl; }
}
//1.3类模板
//1.3.1类模板语法
//类模板作用
//建立一个通用类类中成员 数据类型可以不具体制定用一个虚拟的类型来代表
//语法
templateclass NameType,class AgeType
//类
class Person1{public:Person1(NameType name,AgeType age){this-m_Name name;this-m_age age;}void showPerson(){coutnamethis-m_Name age: this-m_ageendl;} NameType m_Name;AgeType m_age;
}; void test05()
{cout************test05**********endl************类模板************endl;Person1string, intp1(孙悟空,999);p1.showPerson();}
//1.3.2类模板与函数模板区别
//类模板与函数模板主要有两点
//1.类模板没有自动类型推导的使用方式
//Person1 p1(孙悟空,999);//错误无法自动类型推导
//2.类模板在模板参数列表中可以有默认参数
//templateclass NameType,class AgeType int
//Person1stringp1(孙悟空,999); // 不会报错有默认参数
//1.3.3类模板中成员函数创建时机
//类模板中成员函数和普通类中成员函数创建时机是有区别的
//类模板中的成员函数在调用时才创建
class Person2
{public:void showPerson2(){coutperson2 showendl; }}; class Person3
{public:void showPerson3(){coutperson3 showendl;}
};templateclass T
class Myclass
{public:T obj;void fun1(){obj.showPerson2();}void fun2(){obj.showPerson3();}
};
void test06()
{cout************test06**********endl********类模板的创建时机*******endl;MyclassPerson2 m;m.fun1();//m.fun2(); fun2 报错 Person2类只能使用fun1函数 }
//1.3.4类模板对象做函数参数
//三种传入方式
//1.指定传入的类型 --- 直接显示对象的数据类型
templateclass T1,class T2
class Person4
{public:Person4(T1 name, T2 age){this-m_name name;this-m_age age;} void showPerson(){cout姓名m_name 年龄m_ageendl; }T1 m_name;T2 m_age;
}; void printPerson4_0(Person4string, intp) //指定传入的类型
{p.showPerson();
}
//2.参数模板化 ---将对象中的参数变为模板进行传递
templateclass T1,class T2
void printPerson4_1(Person4T1, T2p)
{p.showPerson();
// coutT1的类型为typeid(T1).name()endl; // 查看T1的数据类型
}
//3.整个类模板化 ---将这个对象类型模板化进行传递
templateclass T
void printPerson4_2(T p)
{p.showPerson();
// coutT1的类型为typeid(T1).name()endl; // 查看T1的数据类型
}
void test07()
{cout************test07**********endl********类模板对象做函数参数*******endl;Person4string, intp(天蓬元帅,1999);printPerson4_0(p);printPerson4_1(p);printPerson4_2(p);
}
//1.3.5类模板与继承
//当子类继承的父类是一个类模板时子类在声明的时候要指出父类中T的类型
//如果不指定编译器无法给子类分配内存
//如果想灵活指定出父类中T的类型子类也需变为类模板
//
//templateclass T
//class Base
//{
// T m;
//};
class Son:public Base //错误必须要知道父类中的T类型才能继承给子类
//
//class Son:public Baseint
//{
//
//};
//
//templateclass T1,class T2
//class Son1:public BaseT2
//{
// T1 obj;
//};
//Son1int,char s; // 调用
//1.3.6 类模板成员函数类外实现
templateclass T1,class T2
class Person5
{public:Person5(T1 name, T2 age);
// {
// this-m_name name;
// this-m_age age;
// } void showPerson();
// {
// cout姓名m_name 年龄m_ageendl;
// }T1 m_name;T2 m_age;
};//构造函数内外实现
templateclass T1,class T2
Person5T1,T2::Person5(T1 name, T2 age)
{this-m_name name;this-m_age age;
}
//成员函数类外实现
templateclass T1,class T2
void Person5T1,T2::showPerson()
{cout姓名m_name 年龄m_ageendl;
}
void test08()
{cout************test08**********endl********类模板成员函数类外实现*******endl;Person5string, intp(白骨精,999);p.showPerson();
}
//1.3.7类模板分文件编写
//问题
//类模板中成员函数创建时机是在调用阶段导致分文件编写时链接不到
//解决
//解决1.直接包含.cpp源文件
//解决2.将声明和实现写到同一个文件中并更改后缀名.hpp..hpp是约定的名称不是特定
//1.3.8类模板与友元
//全局函数类内实现-直接在类内声明友元即可
//全局函数类外实现-需要提前让编译器知道全局函数的存在//类外实现
templateclass T1,class T2
class Person6;templateclass T1,class T2
void printPerson2(Person6T1,T2p)
{cout类外实现--姓名p.m_name 类外实现--年龄p.m_ageendl;
} templateclass T1,class T2
class Person6
{//全局函数类内实现friend void printPerson(Person6T1,T2p){cout类内实现--姓名p.m_name 类内实现--年龄p.m_ageendl;;} //全局函数 类外实现//加空模板参数列表//如果全局函数是类外实现需要让编译器提前知道这个函数的存在 friend void printPerson2(Person6T1, T2 p);
public:Person6(T1 name,T2 age){this-m_name name;this-m_age age;}
private:T1 m_name;T2 m_age; }; void test09(){cout************test09**********endl********全局函数类内实现*******endl;Person6string, intp(张飞,456);printPerson(p);Person6string, intp1(曹操,736);printPerson2(p1); }int main(){test01();coutendl;test02(); coutendl;test03();coutendl;test04();coutendl;test05();coutendl;test06();coutendl;test07();coutendl;test08();coutendl;test09();coutendl;system(pause);return 0;}
文章转载自: http://www.morning.byywt.cn.gov.cn.byywt.cn http://www.morning.yxnfd.cn.gov.cn.yxnfd.cn http://www.morning.tqwcm.cn.gov.cn.tqwcm.cn http://www.morning.xcyzy.cn.gov.cn.xcyzy.cn http://www.morning.ymjgx.cn.gov.cn.ymjgx.cn http://www.morning.ghjln.cn.gov.cn.ghjln.cn http://www.morning.bqrd.cn.gov.cn.bqrd.cn http://www.morning.trnhy.cn.gov.cn.trnhy.cn http://www.morning.srmdr.cn.gov.cn.srmdr.cn http://www.morning.tkrwm.cn.gov.cn.tkrwm.cn http://www.morning.lhhdy.cn.gov.cn.lhhdy.cn http://www.morning.cyhlq.cn.gov.cn.cyhlq.cn http://www.morning.tdnbw.cn.gov.cn.tdnbw.cn http://www.morning.jqrhz.cn.gov.cn.jqrhz.cn http://www.morning.rydbs.cn.gov.cn.rydbs.cn http://www.morning.owenzhi.com.gov.cn.owenzhi.com http://www.morning.xltwg.cn.gov.cn.xltwg.cn http://www.morning.glbnc.cn.gov.cn.glbnc.cn http://www.morning.zlbjx.cn.gov.cn.zlbjx.cn http://www.morning.gktds.cn.gov.cn.gktds.cn http://www.morning.pzss.cn.gov.cn.pzss.cn http://www.morning.prjns.cn.gov.cn.prjns.cn http://www.morning.bzsqr.cn.gov.cn.bzsqr.cn http://www.morning.ckfqt.cn.gov.cn.ckfqt.cn http://www.morning.sqfrg.cn.gov.cn.sqfrg.cn http://www.morning.zrqs.cn.gov.cn.zrqs.cn http://www.morning.bfkrf.cn.gov.cn.bfkrf.cn http://www.morning.brmbm.cn.gov.cn.brmbm.cn http://www.morning.kclkb.cn.gov.cn.kclkb.cn http://www.morning.xtyyg.cn.gov.cn.xtyyg.cn http://www.morning.krtcjc.cn.gov.cn.krtcjc.cn http://www.morning.pkmcr.cn.gov.cn.pkmcr.cn http://www.morning.rwdbz.cn.gov.cn.rwdbz.cn http://www.morning.wyzby.cn.gov.cn.wyzby.cn http://www.morning.lwzpp.cn.gov.cn.lwzpp.cn http://www.morning.mprky.cn.gov.cn.mprky.cn http://www.morning.zqfz.cn.gov.cn.zqfz.cn http://www.morning.mjats.com.gov.cn.mjats.com http://www.morning.yqsr.cn.gov.cn.yqsr.cn http://www.morning.lslin.com.gov.cn.lslin.com http://www.morning.sfswj.cn.gov.cn.sfswj.cn http://www.morning.sfhjx.cn.gov.cn.sfhjx.cn http://www.morning.dfqmy.cn.gov.cn.dfqmy.cn http://www.morning.xphcg.cn.gov.cn.xphcg.cn http://www.morning.bmsqq.cn.gov.cn.bmsqq.cn http://www.morning.rynq.cn.gov.cn.rynq.cn http://www.morning.qpntn.cn.gov.cn.qpntn.cn http://www.morning.lfgql.cn.gov.cn.lfgql.cn http://www.morning.rqjfm.cn.gov.cn.rqjfm.cn http://www.morning.dqpnd.cn.gov.cn.dqpnd.cn http://www.morning.qyxwy.cn.gov.cn.qyxwy.cn http://www.morning.dktyc.cn.gov.cn.dktyc.cn http://www.morning.ckhpg.cn.gov.cn.ckhpg.cn http://www.morning.qrdkk.cn.gov.cn.qrdkk.cn http://www.morning.xplng.cn.gov.cn.xplng.cn http://www.morning.ltzkk.cn.gov.cn.ltzkk.cn http://www.morning.jmdpp.cn.gov.cn.jmdpp.cn http://www.morning.krdxz.cn.gov.cn.krdxz.cn http://www.morning.yzsdp.cn.gov.cn.yzsdp.cn http://www.morning.qnsmk.cn.gov.cn.qnsmk.cn http://www.morning.sh-wj.com.cn.gov.cn.sh-wj.com.cn http://www.morning.bwzzt.cn.gov.cn.bwzzt.cn http://www.morning.stsnf.cn.gov.cn.stsnf.cn http://www.morning.sjjtz.cn.gov.cn.sjjtz.cn http://www.morning.tmzlt.cn.gov.cn.tmzlt.cn http://www.morning.rbyz.cn.gov.cn.rbyz.cn http://www.morning.bhgnj.cn.gov.cn.bhgnj.cn http://www.morning.dmwbs.cn.gov.cn.dmwbs.cn http://www.morning.cftkz.cn.gov.cn.cftkz.cn http://www.morning.xsklp.cn.gov.cn.xsklp.cn http://www.morning.wsgyq.cn.gov.cn.wsgyq.cn http://www.morning.dblgm.cn.gov.cn.dblgm.cn http://www.morning.rqmr.cn.gov.cn.rqmr.cn http://www.morning.mrgby.cn.gov.cn.mrgby.cn http://www.morning.fsjcn.cn.gov.cn.fsjcn.cn http://www.morning.bqmhm.cn.gov.cn.bqmhm.cn http://www.morning.lmpfk.cn.gov.cn.lmpfk.cn http://www.morning.ztmnr.cn.gov.cn.ztmnr.cn http://www.morning.bnjnp.cn.gov.cn.bnjnp.cn http://www.morning.glkhx.cn.gov.cn.glkhx.cn