zoukankan      html  css  js  c++  java
  • 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的拷贝。 

  • 相关阅读:
    第二周作业
    第一次作业
    第0次作业
    第一次的作业
    第0次作业
    第三次作业
    %f使用时的注意事项
    关于c++停止工作
    第二次作业
    第一次作业
  • 原文地址:https://www.cnblogs.com/sanerer/p/7074110.html
Copyright © 2011-2022 走看看