• C++ Primer第5版 第八章课后练习答案


    练习8.1

    istream& read(istream& is) {
        string word;
        while (is >> word) {
            cout << word << " ";
        }
        cout << endl;
        is.clear();
        return is;
    }

    练习8.2

    int main(int argc, char* argv[])
    {
        read(cin);
    }

    练习8.3

    EOF或者输入错误字符

    练习8.4

    void read(string &str,vector<string>& v) {
        std::ifstream ifs(str);
        if (ifs) {
            string s;
            while (getline(ifs, s)) {
                v.push_back(s);
            }
        }
    }
    
    int main(int argc, char* argv[])
    {
        vector<string> v;
        string str = const_cast<const char*>(argv[1]);
        read(str, v);
    }

    练习8.5

    void read(string &str,vector<string>& v) {
        std::ifstream ifs(str);
        if (ifs) {
            string s;
            while (ifs >> s) {
                v.push_back(s);
            }
        }
    }
    
    int main(int argc, char* argv[])
    {
        vector<string> v;
        string str = const_cast<const char*>(argv[1]);
        read(str, v);
    }

    练习8.6

    int main(int argc, char* argv[])
    {
        Sales_data total;
        std::ifstream ifs(argv[1]);
        std::ofstream ofs(argv[2]);
        if (read(ifs, total)) {
            Sales_data trans;
            while (read(ifs,trans))
            {
                if (total.isbn() == trans.isbn())
                    total.combine(trans);
                else
                {
                    print(ofs, total) << endl;
                    total = trans;
                }
            }
            print(ofs, total) << endl;
        }
        else
        {
            std::cerr << "No Data?!" << endl;
        }
    }

    练习8.7

    int main(int argc, char* argv[])
    {
        Sales_data total;
        std::ifstream ifs(argv[1]);
        std::ofstream ofs(argv[2],std::ofstream::app);
        if (read(ifs, total)) {
            Sales_data trans;
            while (read(ifs,trans))
            {
                if (total.isbn() == trans.isbn())
                    total.combine(trans);
                else
                {
                    print(ofs, total) << endl;
                    total = trans;
                }
            }
            print(ofs, total) << endl;
        }
        else
        {
            std::cerr << "No Data?!" << endl;
        }
    }

    练习8.8

    int main(int argc, char* argv[])
    {
        Sales_data total;
        std::ifstream ifs(argv[1]);
        std::ofstream ofs(argv[2],std::ofstream::app);
        if (read(ifs, total)) {
            Sales_data trans;
            while (read(ifs,trans))
            {
                if (total.isbn() == trans.isbn())
                    total.combine(trans);
                else
                {
                    print(ofs, total) << endl;
                    total = trans;
                }
            }
            print(ofs, total) << endl;
        }
        else
        {
            std::cerr << "No Data?!" << endl;
        }
    }

    练习8.9

    istream& read(std::istringstream& iss) {
        string word;
        while (iss >> word) {
            cout << word << " ";
        }
        cout << endl;
        iss.clear();
        return iss;
    }

    练习8.10

    int main(int argc, char* argv[])
    {
        std::ifstream ifs(argv[1]);
        std::istringstream iss;
        vector<string> v;
        string word;
        if (ifs) {
            v.emplace_back("");
            while (getline(ifs, v.back()))
            {
                v.emplace_back("");
            }
        }
        else
        {
            std::cerr << "cannot open this file: " << argv[1] << endl;
        }
        for (auto it : v) {
            iss.clear();
            iss.str(it);
            while (iss >> word) {
                cout << word << " ";
            }
        }
    }

    练习8.11

    把istringstream record(line) 换成record.clear();record.str(line);

    练习8.12

    因为我们需要能直接访问PersonInfo的成员变量,所以是聚合类,不能类内初始化

    练习8.13

    struct PersonInfo
    {
        string name;
        vector<string> phones;
    };
    
    std::vector<std::string> s_split(const std::string& in, const std::string& delim) {
        std::regex re{ delim };
        return std::vector<std::string> {
            std::sregex_token_iterator(in.begin(), in.end(), re, -1),
                std::sregex_token_iterator()
        };
    }
    
    int main(int argc, char* argv[])
    {
        string line, word;
        vector<PersonInfo> people;
        std::ifstream ifs(argv[1]);
        while (getline(ifs, line)) {
            vector<string> v = s_split(line, " ");
            PersonInfo info = { v.front(),vector<string>(v.begin() + 1,v.end()) };
            people.emplace_back(info);
        }
    }

    练习8.14

    使用引用避免生成临时变量并赋值,使用const避免修改参数

                                                                                                          

  • 相关阅读:
    人生中第一份值得纪念的工作
    ZOJ 3829 Known Notation(字符串处理 数学 牡丹江现场赛)
    java基础之内部类
    从计算的本质到编程语言
    【Cocos2dx】资源目录,播放背景音乐,导入外部库
    POJ 3723 Tree(树链剖分)
    hdu 1002 A + B Problem II(大正整数相加)
    时间格式字符串转化为date和时间戳
    深入浅出游戏算法(4)-unity3d算法(1)-球转动
    GeoServer手动发布本地Shapefile地图
  • 原文地址:https://www.cnblogs.com/GodZhuan/p/13923395.html
Copyright © 2020-2023  润新知