c++ primer标准库(1) 学习记录

  1. 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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值