- iostream
#include<iostream>//IO交互操作集
#include<iostream>//IO交互操作集
#include<sstream>//字符串交互操作集
#include<fstream>//文件交互操作集
#include<vector>
using namespace std;
int main(int argc, char const *argv[])
{
ofstream out1, out2;
out1 = out2;//错误:不能对流对象赋值
ofstream print(ofstream);//错误:不能初始化ofstream参数
out2 = print(out2);//错误:不能拷贝流对象
string word;
//确认流对象的状态最好方法就是将其作为条件语句的一部分
while(cin >> word){
cout << "input stream is working."<< endl;
}//当输入的流不是string类型的,就会结束循环语句
auto OldState = cin.rdstate();//记住cin的当前状态
cin.clear();//使cin有效
process_input(cin);//使用cin
cin.setstate(old_state);//使cin重置为原来状态
return 0;
//s.bad()若流s的badbit置位,则返回true
//s.good()若流s处于有效状态.则返回true
//s.clear()将流的状态设置为有效,返回void
//s.rdstate()返回流s的当前状态
//badbit用来指出流已经崩溃,表示不可恢复的读写错误
//eofbit用于指出流到达了文件末尾
//goodbit用于指出流未处于出错状态,值为0
}
2.string
#include<iostream>//IO交互操作集
#include<sstream>//字符串交互操作集
#include<fstream>//文件交互操作集
#include<vector>
using namespace std;
struct PersonInfo
{
string name;
vector<string> phones;
};
//string s(10,'?')重复初始化
//s.empty()s为空返回true,否则返回false
//s1+s2返回s1和s2连接后的结果,等价于strcpy(s1[],s2[])
//s.size()返回字符串的字符个数,不包括截止符,等价于strlen(char[])
//getline(cin,s)从cin中读取一行赋给s,返回cin
//s[n],返回第n个字符的引用
//isupper(c)和islower(c)返回bool类型,判断字符c是否为大写字母或小写字母
//isspace(c),判断c是否为空格字符
//toupper(c)和tolower(c)分别返回字母c的大写或小写形式
int main(int argc, char const *argv[])
{
string line,word;
vector<PersonInfo> people;
while (getline(cin,line))
{
PersonInfo info;
istringstream record(line);
//让line和record关联,也就是让line成为输入的字符串类型变量
//record相当于cin,istringstream
record >> info.name;
while (record >> word)
{
info.phones.push_back(word);
}
people.push_back(info);
}
return 0;
}