1.自定义类型与内置类型的相互转化
在做IO类型的OJ的时候,有多个接收样例的时候,在接收字符串的时候我们有下面的写法。
while(cin>>str);
cin>>str的返回值调用的是string类的operator>>;返回值是一个iostream,按道理说一个iostream无法作为一个逻辑判断。所以我们推测cin应该发生了类型的转化,转换为了一个bool类型的值。
1.1operator 类型
在C++中可以对类型进行重载,从而达到自定义类型与内置类型的相互转化。
由于转化的过程中发生的是隐士类型转化,生成了临时变量,临时变量具有常性,所以需要使用const修饰。
下面程序实现了Date类的operator bool和operator int函数
输出:
The year >= 2015
the year+month+day=:2035
2.C++文件IO
C++实现了一个庞大的IO库,其中以ios为基类;
类名 | 功能 |
---|---|
ifstream | 创建一个只输入用的对象 |
ofstream | 创建一个只输出用的对象 |
fstream | 既可以输入也可以输出 |
使用文件流对象的成员函数打开一个磁盘文件,使得文件流对象和磁盘文件之间建立联系
比如我们读取当前主程序io.cpp中的内容
int main()
{
ifstream ifs("io.cpp");
char ch;
while (ifs)
{
ch = ifs.get();
cout << ch;
}
cout << endl;
return 0;
}
将io.cpp写到一个名为io.txt的文件中
int main()
{
ifstream ifs("io.cpp");
ofstream ofs("io.txt");
char ch;
while (ifs)
{
ch = ifs.get();
cout << ch;
ofs << ch;
}
cout << endl;
return 0;
}
注意到,在读取文件内容的时候,我们多读取了一个字符,该字符是文件的标志EOF;我们可以使用eof接口取消对EOF的读取
int main()
{
ifstream ifs("io.cpp");
ofstream ofs("io.txt");
char ch;
ch = ifs.get();
while (!ifs.eof())
{
cout << ch;
ofs << ch;
ch = ifs.get();
}
cout << endl;
return 0;
}
3.C++文件IO的二进制读取和文本读取
struct ServerInfo
{
char _address[256];
int _port;
Date _d;
};
struct ConfigManager
{
public:
ConfigManager(const char* filename)
:_filename(filename)
{}
//二进制写
void writebin(const ServerInfo& info)
{
//以二进制写的方式打开文件
ofstream ofs(_filename, ios_base::out | ios_base::binary);
ofs.write((const char*)&info, sizeof(info));
}
void readbin(ServerInfo& info)
{
//以二进制读的方式打开文件
ifstream ifs(_filename, ios_base::in | ios_base::binary);
ifs.read((char*)&info, sizeof(info));
}
// 文本读写
void writetext(const ServerInfo& info)
{
ofstream ofs(_filename);
ofs << info._address << endl;
ofs << info._port << endl;
ofs << info._d << endl;
}
void readtext(ServerInfo& info)
{
ifstream ifs(_filename);
ifs >> info._address;
ifs >> info._port;
ifs >> info._d;
}
private:
string _filename;
};
主程序
int main()
{
ConfigManager mg("test.txt");
ServerInfo info = { "https://www.bilibili.com/",22,{2022,10,12} };
mg.writetext(info);
ServerInfo rinfo;
mg.readtext(rinfo);
cout << rinfo._address << " " << rinfo._port << " " << rinfo._d << endl;
return 0;
}
输出:https://www.bilibili.com/ 22 2022 10 12
4.stringstream 类
将数值类型数据格式化为字符串
int main()
{
int a = 123456789;
stringstream s;
s << a;
string ss;
s >> ss;
cout << "a转换为字符串:" << ss << endl;
//将原来流中的字符清理掉
s.str(" ");
double d = 12.34;
s << d;
s >> ss;
cout << "d转换为字符串:" <<d<< endl;
return 0;
}
输出:
a转换为字符串:123456789
d转换为字符串:12.34
字符串拼接
int main()
{
stringstream sstream;
sstream << "this is" << " " << "first string,";
sstream << "this is second string";
cout << sstream.str() << endl;
sstream.str("");
sstream << "this is thrid string";
cout << sstream.str() << endl;
return 0;
}
输出
this is first string,this is second string
this is thrid string
序列化与反序列化
模拟微信的信息发送
struct chatinfo
{
string name; //用户姓名
int id; //用户ID
Date date; //发送日期
string mgs; //发送内容
};
int main()
{
chatinfo info = { "张三",1111111,{2022,10,12},"你在干嘛?" };
//转换为字符串
ostringstream oss;
oss << info.name << " " << info.id << " " << info.date << " " << info.mgs;
string s;
s = oss.str();
cout << s << endl;
chatinfo chinfo;
istringstream iss(s);
iss >> chinfo.name >> chinfo.id >> chinfo.date >> chinfo.mgs;
cout << "-----------------------------------------" << endl;
cout << chinfo.name << " " << "(" << chinfo.id << ")" << "[" << chinfo.date << "]" << endl;
cout << chinfo.mgs << endl;
return 0;
}