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避免修改参数

                                                                                                      

上一篇:BUU-WEB-Ping Ping Ping


下一篇:shell脚本中将 IFS (Internal Field Separator 内部字段分隔符)替换为换行符