• string流


    使用 istreamstring 与文本行绑定之后读取元素。

    struct PersonInfo
    {
        string name;
        vector<string> phones;
    };

    int main(int argc, char const *argv[])
    {
        string line, word;
        vector<PersonInfo> people;
        istringstream record;
        while (getline(cin, line))
        {
            PersonInfo info;

            record.clear();                                      // 在将line拷贝到record中之前,应调用clear()函数来使流复位。保证下一次循环时,流record的正常使用。

            record.str(line);

            //    istringstream record(line)           如果在循环内部,则不需要复位,因为每次循环都会zion个销毁和重新创建istringstream流对象 record!
            record >> info.name;
            while (record >> word)
                info.phones.push_back(word);
            people.push_back(info);
        }
        return 0;
    }

    fstream 与 ostringstream 使用上的区别。

    ofstream out;                     // 输出文件流未与任何文件相关联。                                                              

    out.open ("file1") ;             // 打开指定文件,即与文件相关联                                                                  

    out.close();                        // 关闭out,以便将其用于其他文件。                                                              

    out.open("file2")                // 与另一个文件关联。 

      ostringstream  out_s;        // 输出字符串流不需要与string绑定。

      out_s<<"test string!";        // 向out_s的string对象写入字符(内存提供的默认的string对象,不需要额外绑定)。

      cout<<out_s.str();             //输出out_s的拷贝。 

  • 相关阅读:
    计划任务和压缩归档
    libevent 源码学习三 —— 基本使用场景和事件流程
    libevent 源码学习二 —— reactor 模式
    libevent 库源码学习
    手动配置固定IP参数vim vim
    软件相关
    写xhttpd服务器时 遇到segmentation fault
    c与c++中输出字符指针和字符串指针的问题
    char * argv[] ,string简析
    传入参数与传出参数
  • 原文地址:https://www.cnblogs.com/sanerer/p/7074110.html
Copyright © 2020-2023  润新知