C++Primer第五版 练习10.32解答

本文详细介绍了如何通过C++编程语言解决复杂数据结构和算法问题,包括利用STL库实现高效排序、查找和计算操作。通过实例解析,展示了如何运用vector容器、比较函数、find_if和accumulate等高级功能,解决特定业务场景中的数据处理需求。重点讨论了如何在实际项目中灵活应用这些工具,以提高代码效率和可维护性。


    练习10.32:重写1.6节(第21页)中的书店程序,使用一个vector保存交易记录,使用不同算法完成处理。使用sort10.3.1节(第345页)中的compareIsbn函数来排序交易记录,然后使用findaccumulate求和。


    说明:关于这道题,其实题目步骤说的很清楚,但在find的使用上,我遇到了问题,题目中的用的是find_if函数而非find,主要问题纠结于,当类型是Sales_item时,find(,,)第三个元素的条件该怎么写,思考无果的情况下,在find_if里用lambda表达式解决了问题,关于思考的过程,写了两版程序,以记录思考的过程,在网上搜这道题时,没能直接找到,因此,自己写了这篇博客,以共同学习,以及探寻更好的思路。


    本文程序中有一个Sales_data.txt,内容如下

Hero 1 2
Tom 2 3
Hero 2 3
Jack 2 3
Hero 3 4

这个没什么好说的,程序里有一个“Sales_item.h”头文件,安照Primer书上说明可以到指定网站下载到,本文最后会贴出来


先看第一版的程序

/*
*练习10.32 
*2015/9/9 
*问题描述:练习10.32:重写1.6节(第21页)中的书店程序,使用一个vector保存交易记录,使用不同算法完成处理。使用sort和10.3.1节(第345页)中的compareIsbn函数来排序交易记录,然后使用find和accumulate求和。 
*说明:这道题并不是想象中那么容易
*作者:Nick Feng
*邮箱:nickgreen23@163.com 
*/

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
#include "Sales_item.h"

using namespace std;

bool compareISBN(const Sales_item &lhs, const Sales_item &rhs)
{
	return lhs.isbn() < rhs.isbn();
}

bool f(const Sales_item &rhs)
{
	return rhs.isbn() == "Hero";
}
int main()
{   
    ifstream in("Sales_data.txt");                                  //从文本读取Sales_item数据
	istream_iterator<Sales_item> sale_in(in),eof;                   //定义一个输入流迭代器
	ostream_iterator<Sales_item> out_iter(cout, "\n");              //定义一个输出流迭代器,格式占一行
	vector<Sales_item> vec(sale_in,eof);                            //复制文本输入的内容,保存到vector
	copy(vec.begin(),vec.end(), out_iter);                          //这里打印的是没有排序的,即是文本中的内容 
	cout << endl;
	
	sort(vec.begin(),vec.end(),compareISBN);                          //进行排序
	copy(vec.begin(),vec.end(), out_iter);                             //这里打印的是排序之后的内容
	cout << endl;
	
	vector<Sales_item> sz_item;
    vector<Sales_item>::iterator it;
    for(it = vec.begin(); it != vec.end();++it)                        
    {
		//if(it == find_if(vec.begin(),vec.end(),[](const Sales_item &rhs){ return rhs.isbn() == "Hero";}) )
		if((*it).isbn() == "Hero")
			sz_item.push_back(*it);			                           //找到ISBN号为”Hero“的信息                                                                                  保存到sz_item 
    }
	
	copy(sz_item.begin(),sz_item.end(),out_iter);                           //打印”Hero“的信息 
	cout << endl;
	cout << accumulate(sz_item.begin(),sz_item.end(),Sales_item("Hero"));   //计算”Hero“的和 
	cout << endl; 
	
	
	

	return 0;	
} 

   关于这一版程序,没什么好说的,注释里基本都有,关于accumulate函数,第三个元素是一个初始计算值,这里是Sales_item("Hero")相当于 Hero 0 0


运行结果如下

Hero 1 2 2
Tom 2 6 3
Hero 2 6 3
Jack 2 6 3
Hero 3 12 4


Hero 1 2 2
Hero 2 6 3
Hero 3 12 4
Jack 2 6 3
Tom 2 6 3


Hero 1 2 2
Hero 2 6 3
Hero 3 12 4


Hero 6 20 3.33333

--------------------------------
Process exited after 0.1512 seconds with return value 0
请按任意键继续. . .


第二版程序

/*
*练习10.32 
*2015/9/9 
*问题描述:练习10.32:重写1.6节(第21页)中的书店程序,使用一个vector保存交易记录,使用不同算法完成处理。使用sort和10.3.1节(第345页)中的compareIsbn函数来排序交易记录,然后使用find和accumulate求和。 
*说明:这道题并不是想象中那么容易
*作者:Nick Feng
*邮箱:nickgreen23@163.com 
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
#include "Sales_item.h"

using namespace std;

bool compareISBN(const Sales_item &lhs, const Sales_item &rhs)  //参考345页 
{
	return lhs.isbn() < rhs.isbn();
}


int main()
{   
    ifstream in("Sales_data.txt");                             //从文本读取Sales_item数据 
	istream_iterator<Sales_item> sale_in(in),eof;              //定义一个输入流迭代器 
	ostream_iterator<Sales_item> out_iter(cout, "\n");         //定义一个输出流迭代器,格式占一行 
	vector<Sales_item> vec(sale_in,eof);                       //复制文本输入的内容,保存到vector 
	copy(vec.begin(),vec.end(), out_iter);                    //这里打印的是没有排序的,即是文本中的内容 
	cout << endl;
	
	sort(vec.begin(),vec.end(),compareISBN);                   //进行排序 
	copy(vec.begin(),vec.end(), out_iter);                     //这里打印的是排序之后的内容 
	cout << endl;
	
	auto wc =  find_if(vec.begin(),vec.end(),[](const Sales_item &rhs){ return rhs.isbn() == "Jack";}); //这一步                                                                                            //很很重要,文中会详细说明 
	copy(vec.begin(),wc,out_iter);                                       //这里只打印ISBN号为Hero的信息
	cout << endl; 
	cout << accumulate(vec.begin(),wc,Sales_item("Hero"));                   //计算ISBN号为“Hero”的结果 
	cout << endl; 

	return 0;	
} 

这里 特别说明的是  auto wc =  find_if(vec.begin(),vec.end(),[](const Sales_item &rhs){ return rhs.isbn() == "Jack";});

这一步排完序后,迭代器位置在第一个ISBN号为“Jack”上,因此从vec.begin()到wc,全是“Hero”的信息,我想这也就是为什么要排序,就是方便找位置,当然题目做到这里,我没按要求去用find去做,因为真心不会,所以用了find_if


最后附上 Sales_item.h源码

/*
 * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
 * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
 * copyright and warranty notices given in that book:
 * 
 * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
 * 
 * 
 * "The authors and publisher have taken care in the preparation of this book,
 * but make no expressed or implied warranty of any kind and assume no
 * responsibility for errors or omissions. No liability is assumed for
 * incidental or consequential damages in connection with or arising out of the
 * use of the information or programs contained herein."
 * 
 * Permission is granted for this code to be used for educational purposes in
 * association with the book, given proper citation if and when posted or
 * reproduced.Any commercial use of this code requires the explicit written
 * permission of the publisher, Addison-Wesley Professional, a division of
 * Pearson Education, Inc. Send your request for permission, stating clearly
 * what code you would like to use, and in what specific way, to the following
 * address: 
 * 
 *     Pearson Education, Inc.
 *     Rights and Permissions Department
 *     One Lake Street
 *     Upper Saddle River, NJ  07458
 *     Fax: (201) 236-3290
*/ 

/* This file defines the Sales_item class used in chapter 1.
 * The code used in this file will be explained in
 * Chapter 7 (Classes) and Chapter 14 (Overloaded Operators)
 * Readers shouldn't try to understand the code in this file
 * until they have read those chapters.
*/

#ifndef SALESITEM_H
// we're here only if SALESITEM_H has not yet been defined 
#define SALESITEM_H

// Definition of Sales_item class and related functions goes here
#include <iostream>
#include <string>

class Sales_item {
// these declarations are explained section 7.2.1, p. 270 
// and in chapter 14, pages 557, 558, 561
friend std::istream& operator>>(std::istream&, Sales_item&);
friend std::ostream& operator<<(std::ostream&, const Sales_item&);
friend bool operator<(const Sales_item&, const Sales_item&);
friend bool 
operator==(const Sales_item&, const Sales_item&);
public:
    // constructors are explained in section 7.1.4, pages 262 - 265
    // default constructor needed to initialize members of built-in type
    Sales_item(): units_sold(0), revenue(0.0) { }
    Sales_item(const std::string &book): 
                  bookNo(book), units_sold(0), revenue(0.0) { }
    Sales_item(std::istream &is) { is >> *this; }
public:
    // operations on Sales_item objects
    // member binary operator: left-hand operand bound to implicit this pointer
    Sales_item& operator+=(const Sales_item&);
    
    // operations on Sales_item objects
    std::string isbn() const { return bookNo; }
    double avg_price() const;
// private members as before
private:
    std::string bookNo;      // implicitly initialized to the empty string
    unsigned units_sold;
    double revenue;
};

// used in chapter 10
inline
bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs) 
{ return lhs.isbn() == rhs.isbn(); }

// nonmember binary operator: must declare a parameter for each operand
Sales_item operator+(const Sales_item&, const Sales_item&);

inline bool 
operator==(const Sales_item &lhs, const Sales_item &rhs)
{
    // must be made a friend of Sales_item
    return lhs.units_sold == rhs.units_sold &&
           lhs.revenue == rhs.revenue &&
           lhs.isbn() == rhs.isbn();
}

inline bool 
operator!=(const Sales_item &lhs, const Sales_item &rhs)
{
    return !(lhs == rhs); // != defined in terms of operator==
}

// assumes that both objects refer to the same ISBN
Sales_item& Sales_item::operator+=(const Sales_item& rhs) 
{
    units_sold += rhs.units_sold; 
    revenue += rhs.revenue; 
    return *this;
}

// assumes that both objects refer to the same ISBN
Sales_item 
operator+(const Sales_item& lhs, const Sales_item& rhs) 
{
    Sales_item ret(lhs);  // copy (|lhs|) into a local object that we'll return
    ret += rhs;           // add in the contents of (|rhs|) 
    return ret;           // return (|ret|) by value
}

std::istream& 
operator>>(std::istream& in, Sales_item& s)
{
    double price;
    in >> s.bookNo >> s.units_sold >> price;
    // check that the inputs succeeded
    if (in)
        s.revenue = s.units_sold * price;
    else 
        s = Sales_item();  // input failed: reset object to default state
    return in;
}

std::ostream& 
operator<<(std::ostream& out, const Sales_item& s)
{
    out << s.isbn() << " " << s.units_sold << " "
        << s.revenue << " " << s.avg_price();
    return out;
}

double Sales_item::avg_price() const
{
    if (units_sold) 
        return revenue/units_sold; 
    else 
        return 0;
}
#endif




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值