练习17.4:
#include <iostream>
#include <string>
#include <errno.h>
#include <unordered_set>
using namespace std;
#include <algorithm>
#include <numeric>
class Sales_date {
//将非成员重载函数设置为友元函数
friend Sales_date operator+(const Sales_date& h1, const Sales_date& h2);
friend bool operator==(const Sales_date& lhs, const Sales_date& rhs);
friend istream& operator >> (istream& is, Sales_date& item);
friend ostream& operator << (ostream& os, const Sales_date& item);
friend class std::hash<Sales_date>;
public:
//默认构造函数
Sales_date() :bookNo(std::string()) { }
Sales_date(const std::string& s, unsigned us, double p) :bookNo(s), units_sold(us), revenue(p* us) { }
Sales_date(const std::string& s) :bookNo(s) { }
//拷贝构造函数
Sales_date(const Sales_date& s) :bookNo(s.bookNo), units_sold(s.units_sold), revenue(s.revenue) { }
//拷贝赋值运算符
Sales_date& operator=(const Sales_date&);
//复合赋值运算符
Sales_date& operator+=(const Sales_date&);
//得到bookNo
std::string isbn()const { return bookNo; }
private:
//平均售价
double avg_price()const { return units_sold ? revenue / units_sold : 0; }
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
bool operator==(const Sales_date& lhs, const Sales_date& rhs)
{
if (lhs.bookNo == rhs.bookNo && lhs.units_sold == rhs.units_sold && lhs.revenue == rhs.revenue)
{
return true;
}
return false;
}
//拷贝赋值运算符
Sales_date& Sales_date::operator=(const Sales_date& s)
{
bookNo = s.bookNo;
units_sold = s.units_sold;
revenue = s.revenue;
return *this;
}
//复合赋值运算符
Sales_date& Sales_date::operator+=(const Sales_date& s)
{
if (this->bookNo == s.bookNo)
{
units_sold = units_sold + s.units_sold;
revenue = revenue + s.revenue;
return *this;
}
else
{
throw runtime_error("bookNo is different");
}
}
//重载的加法运算符
Sales_date operator+(const Sales_date& h1, const Sales_date& h2)
{
if (h1.bookNo == h2.bookNo)
{
Sales_date sTemp = h1;
sTemp.units_sold = sTemp.units_sold + h2.units_sold;
sTemp.revenue = sTemp.revenue + h2.revenue;
return sTemp;
}
else
{
//无法相加
//throw runtime_error("bookNo is different");
cout << "h1.bookNo != h2.bookNo" << endl;
}
}
//重载输入运算符
istream& operator >> (istream& is, Sales_date& item)
{
double price;
is >> item.bookNo >> item.units_sold >> price;
//检查输入是否出错
if (is)
{
item.revenue = item.units_sold * price;
}
else
{
//输入失败,对象被赋予默认的状态
item = Sales_date();
}
return is;
}
//重载输出运算符
ostream& operator << (ostream& os, const Sales_date& item)
{
os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price();
return os;
}
namespace std {
template<>
struct hash<Sales_date>
{
typedef size_t result_tytpe;
typedef Sales_date agument_type;
size_t operator()(const Sales_date& s)const;
};
size_t hash<Sales_date>::operator()(const Sales_date& s)const
{
return hash<string>()(s.bookNo) ^
hash<unsigned>()(s.units_sold) ^
hash<double>()(s.revenue);
}
}
bool compareIsbn(const Sales_date& lhs, const Sales_date& rhs)
{
return lhs.isbn() < rhs.isbn();
}
typedef tuple < vector<Sales_date>::size_type, vector<Sales_date>::const_iterator, vector<Sales_date>::const_iterator>matches;
vector<matches>findBook(const vector<vector<Sales_date>>& files, const string& book)
{
//初始化空vector
vector<matches>ret;
for (auto it = files.cbegin(); it != files.cend(); ++it)
{
auto found = equal_range(it->cbegin(), it->cend(), book, compareIsbn);
if (found.first != found.second)
{
ret.push_back(make_tuple(it - files.cbegin(), found.first, found.second));
}
}
return ret;
}
void reportResults(istream& in, ostream& os, const vector<vector<Sales_date>>& files)
{
string s;
while (in >> s)
{
auto trans = findBook(files, s);
if (trans.empty())
{
cout << s << " not found in any stores" << endl;
continue;;
}
for (const auto& store : trans)
{
os << "store " << get<0>(store) << " sales: " << accumulate(get<1>(store), get<2>(store), Sales_date(s)) << endl;
}
}
}
int main()
{
//三家书店
vector<Sales_date>v1;
vector<Sales_date>v2;
vector<Sales_date>v3;
Sales_date s1("C++", 2, 50);
Sales_date s2("C++", 7, 50);
Sales_date s3("C++", 6, 50);
Sales_date s4("C++", 10, 50);
Sales_date s5("Java", 5, 30);
Sales_date s6("Java", 10, 30);
Sales_date s7("Java", 9, 30);
Sales_date s8("HTML5", 20, 40);
Sales_date s9("HTML5", 3, 40);
v1.push_back(s1);
v1.push_back(s2);
v1.push_back(s3);
v1.push_back(s6);
v2.push_back(s1);
v2.push_back(s4);
v2.push_back(s6);
v2.push_back(s7);
v3.push_back(s2);
v3.push_back(s3);
v3.push_back(s8);
v3.push_back(s9);
vector<vector<Sales_date>>files = { v1,v2,v3 };
reportResults(cin, cout, files);
system("pause");
return 0;
}
结果:
练习17.5:
代码为:
#include <iostream>
#include <string>
#include <errno.h>
#include <unordered_set>
using namespace std;
#include <algorithm>
#include <numeric>
class Sales_date {
//将非成员重载函数设置为友元函数
friend Sales_date operator+(const Sales_date& h1, const Sales_date& h2);
friend bool operator==(const Sales_date& lhs, const Sales_date& rhs);
friend istream& operator >> (istream& is, Sales_date& item);
friend ostream& operator << (ostream& os, const Sales_date& item);
friend class std::hash<Sales_date>;
public:
//默认构造函数
Sales_date() :bookNo(std::string()) { }
Sales_date(const std::string& s, unsigned us, double p) :bookNo(s), units_sold(us), revenue(p* us) { }
Sales_date(const std::string& s) :bookNo(s) { }
//拷贝构造函数
Sales_date(const Sales_date& s) :bookNo(s.bookNo), units_sold(s.units_sold), revenue(s.revenue) { }
//拷贝赋值运算符
Sales_date& operator=(const Sales_date&);
//复合赋值运算符
Sales_date& operator+=(const Sales_date&);
//得到bookNo
std::string isbn()const { return bookNo; }
private:
//平均售价
double avg_price()const { return units_sold ? revenue / units_sold : 0; }
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
bool operator==(const Sales_date& lhs, const Sales_date& rhs)
{
if (lhs.bookNo == rhs.bookNo && lhs.units_sold == rhs.units_sold && lhs.revenue == rhs.revenue)
{
return true;
}
return false;
}
//拷贝赋值运算符
Sales_date& Sales_date::operator=(const Sales_date& s)
{
bookNo = s.bookNo;
units_sold = s.units_sold;
revenue = s.revenue;
return *this;
}
//复合赋值运算符
Sales_date& Sales_date::operator+=(const Sales_date& s)
{
if (this->bookNo == s.bookNo)
{
units_sold = units_sold + s.units_sold;
revenue = revenue + s.revenue;
return *this;
}
else
{
throw runtime_error("bookNo is different");
}
}
//重载的加法运算符
Sales_date operator+(const Sales_date& h1, const Sales_date& h2)
{
if (h1.bookNo == h2.bookNo)
{
Sales_date sTemp = h1;
sTemp.units_sold = sTemp.units_sold + h2.units_sold;
sTemp.revenue = sTemp.revenue + h2.revenue;
return sTemp;
}
else
{
//无法相加
//throw runtime_error("bookNo is different");
cout << "h1.bookNo != h2.bookNo" << endl;
}
}
//重载输入运算符
istream& operator >> (istream& is, Sales_date& item)
{
double price;
is >> item.bookNo >> item.units_sold >> price;
//检查输入是否出错
if (is)
{
item.revenue = item.units_sold * price;
}
else
{
//输入失败,对象被赋予默认的状态
item = Sales_date();
}
return is;
}
//重载输出运算符
ostream& operator << (ostream& os, const Sales_date& item)
{
os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price();
return os;
}
namespace std {
template<>
struct hash<Sales_date>
{
typedef size_t result_tytpe;
typedef Sales_date agument_type;
size_t operator()(const Sales_date& s)const;
};
size_t hash<Sales_date>::operator()(const Sales_date& s)const
{
return hash<string>()(s.bookNo) ^
hash<unsigned>()(s.units_sold) ^
hash<double>()(s.revenue);
}
}
bool compareIsbn(const Sales_date& lhs, const Sales_date& rhs)
{
return lhs.isbn() < rhs.isbn();
}
//typedef tuple < vector<Sales_date>::size_type, vector<Sales_date>::const_iterator, vector<Sales_date>::const_iterator>matches;
typedef pair< vector<Sales_date>::size_type, pair<vector<Sales_date>::const_iterator, vector<Sales_date>::const_iterator>>matches;
vector<matches>findBook(const vector<vector<Sales_date>>& files, const string& book)
{
//初始化空vector
vector<matches>ret;
for (auto it = files.cbegin(); it != files.cend(); ++it)
{
auto found = equal_range(it->cbegin(), it->cend(), book, compareIsbn);
if (found.first != found.second)
{
ret.push_back(make_pair(it - files.cbegin(), make_pair(found.first, found.second)));
}
}
return ret;
}
void reportResults(istream& in, ostream& os, const vector<vector<Sales_date>>& files)
{
string s;
while (in >> s)
{
auto trans = findBook(files, s);
if (trans.empty())
{
cout << s << " not found in any stores" << endl;
continue;;
}
for (const auto& store : trans)
{
os << "store " << store.first << " sales: " << accumulate(store.second.first, store.second.second, Sales_date(s)) << endl;
}
}
}
int main()
{
//三家书店
vector<Sales_date>v1;
vector<Sales_date>v2;
vector<Sales_date>v3;
Sales_date s1("C++", 2, 50);
Sales_date s2("C++", 7, 50);
Sales_date s3("C++", 6, 50);
Sales_date s4("C++", 10, 50);
Sales_date s5("Java", 5, 30);
Sales_date s6("Java", 10, 30);
Sales_date s7("Java", 9, 30);
Sales_date s8("HTML5", 20, 40);
Sales_date s9("HTML5", 3, 40);
v1.push_back(s1);
v1.push_back(s2);
v1.push_back(s3);
v1.push_back(s6);
v2.push_back(s1);
v2.push_back(s4);
v2.push_back(s6);
v2.push_back(s7);
v3.push_back(s2);
v3.push_back(s3);
v3.push_back(s8);
v3.push_back(s9);
vector<vector<Sales_date>>files = { v1,v2,v3 };
reportResults(cin, cout, files);
system("pause");
return 0;
}
结果:
练习17.6:
使用一个类来代替,修改代码如下:
class StoreBookFile
{
friend void reportResults(istream& in, ostream& os, const vector<vector<Sales_date>>& files);
public:
using line_no = std::vector<std::string>::size_type;
//构造函数
StoreBookFile(std::vector<Sales_date>::size_type a, vector<Sales_date>::const_iterator b, vector<Sales_date>::const_iterator c) :
store_index(a), book_it1(b), book_it2(c) { }
private:
std::vector<Sales_date>::size_type store_index;
vector<Sales_date>::const_iterator book_it1, book_it2;
};
bool compareIsbn(const Sales_date& lhs, const Sales_date& rhs)
{
return lhs.isbn() < rhs.isbn();
}
vector<StoreBookFile>findBook(const vector<vector<Sales_date>>& files, const string& book)
{
//初始化空vector
vector<StoreBookFile>ret;
for (auto it = files.cbegin(); it != files.cend(); ++it)
{
auto found = equal_range(it->cbegin(), it->cend(), book, compareIsbn);
if (found.first != found.second)
{
ret.push_back(StoreBookFile(it - files.cbegin(), found.first, found.second));
}
}
return ret;
}
void reportResults(istream& in, ostream& os, const vector<vector<Sales_date>>& files)
{
string s;
while (in >> s)
{
auto trans = findBook(files, s);
if (trans.empty())
{
cout << s << " not found in any stores" << endl;
continue;;
}
for (const auto& store : trans)
{
os << "store " << store.store_index << " sales: " << accumulate(store.book_it1, store.book_it2, Sales_date(s)) << endl;
}
}
}
结果:
练习17.7:
使用tuple和pair类似,都很简便,适合临时存储数据
如果要对数据进行一些操作,使用类版本更好
练习17.8:
会发生异常,这里使用默认构造函数,booNo被初始化为空字符串,累加时没有初始值,发生错误