达内2013C++教程笔记(其实为其数据结果与算法的一部分,但内容更像是C++的)

1. istringstream,ostringstream类

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

class Person
{
	private:
		string name;
		int age;
	public:
		friend ostream& operator<<( ostream& os, const Person &p ) // 函数2 
		{
			return os << p.name << ' ' << p.age << endl;
		}
		
		friend istream& operator>>( istream& in, Person &p ) //函数1 
		{
			return in >> p.name >> p.age;
		}
};

int main()
{
	string s1 = "hyp 22";
	Person a;
	istringstream in(s1); // 字符串输入流对象 
	in >> a;  // 会调用函数1,因为istringstream为istream的子类 
	cout << a;
	 
	ostringstream out;  // 字符串输出流对象
	out << a << endl;  //调用函数2,out为ostream的子类
	string s2 = out.str(); // 取得字符串 
	cout << s2 << endl;
	 
	system("pause");
	return 0;
} 


2. 文件IO:#include<fstream>
ifstream fin;文件输入流对象
ofstream fout;文件输出流对象
fstream fio;文件IO对象
都是istream/ostream类的子类,cin/cout能做的事情,文件fin/fout都可以做,文件
输入/输出流对象需要跟一个文件关联


3. 最大的无符号整数可以这样表示:unsigned int num = ~0u;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值