C++ Primer(第5版) 练习 17.5
练习 17.5 重写findBook,令其返回一个pair,包含一个索引和一个迭代器pair。
环境:Linux Ubuntu(云服务器)
工具:vim
代码块
cpp
vector<vector<Sales_data>> files;
typedef pair<vector<Sales_data>::size_type, pair<std::vector<Sales_data>::const_iterator, vector<Sales_data>::const_iterator>> matches;
vector<matches> findBook(const vector<vector<Sales_data>> &files, const string &book){
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_data>> &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_data(s))<<endl;
}
}
}