• 【cpp】chap 8


    1.输出“输入的内容”

    // basic file operations
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    std::istream& func(std::istream &is)
    {
        std::string buf;
        while (is >> buf)
            std::cout << "output = " << buf << std::endl;
        is.clear();
        return is;
    }
    
    int main() {
        std::istream &is=cin;
        func(is);
    }

     2.istringstream

    #include <iostream>
    #include <sstream>
    using std::istream;
    
    istream& func(istream &is)
    {
        std::string buf;
        while (is >> buf)
            std::cout << buf << std::endl;
        is.clear();
        return is;
    }
    
    int main()
    {
        std::istringstream iss("hello");
        func(iss);
    
        system("pause");
        return 0;
    }

    3.ReadFileToVec——将每一行储存成为vector元素

    #include <fstream>
    #include <string>
    #include <vector>
    #include <iostream>
    
    using std::vector; using std::string; using std::ifstream; using std::cout; using std::endl;
    
    void ReadFileToVec(const string& fileName, vector<string>& vec)
    {
        ifstream ifs(fileName);
        if (ifs)
        {
            string buf;
            while (std::getline(ifs, buf))
                vec.push_back(buf);
        }
    }
    
    int main()
    {
        vector<string> vec;
        ReadFileToVec("cww.txt", vec);
        for (const auto &str : vec)
            cout << str << endl;
        system("pause");
        return 0;
    }

    将每个单词存储到vector元素:

    #include <fstream>
    #include <string>
    #include <vector>
    #include <iostream>
    
    using std::vector; using std::string; using std::ifstream; using std::cout; using std::endl;
    
    void ReadFileToVec(const string& fileName, vector<string>& vec)
    {
        ifstream ifs(fileName);
        if (ifs)
        {
            string buf;
            while (ifs >> buf)
                vec.push_back(buf);
        }
    }
    
    int main()
    {
        vector<string> vec;
        ReadFileToVec("cww.txt", vec);
        for (const auto &str : vec)
            cout << str << endl;
        system("pause");
        return 0;
    }

    4.除非有必须使用其他容器的理由,否则就用Vector或List。

    5.

  • 相关阅读:
    [CQOI2015]选数
    利用匈牙利算法&HopcroftKarp算法解决二分图中的最大二分匹配问题 例poj 1469 COURSES
    玩家死亡,屏幕灰白效果实现
    Bool值相加
    (转)D3D性能优化
    Flash AS3视频教程
    Accurately Profiling Direct3D API Calls (Direct3D 9)
    菲涅尔反射
    sscanf时用到郁闷问题
    如何精确测量程序运行时间(转)
  • 原文地址:https://www.cnblogs.com/liez/p/6881067.html
Copyright © 2020-2023  润新知