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

合集《C++ Primer第5版》 课后练习答案 - 丸子球球 - 博客园 (cnblogs.com)

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

posted @ 2020-11-06 08:55  丸子球球  阅读(242)  评论(0编辑  收藏  举报