C++ //练习 17.5 重写findBook,令其返回一个pair,包含一个索引和一个迭代器pair。

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;
        }
    }
}
相关推荐
Jasmine_llq4 分钟前
《P4391 [BalticOI 2009] Radio Transmission 无线传输 题解》
算法·字符串·substr
虾球xz24 分钟前
游戏引擎学习第278天:将实体存储移入世界区块
数据库·c++·学习·游戏引擎
水水沝淼㵘31 分钟前
嵌入式开发学习日志(数据结构--单链表)Day20
c语言·开发语言·数据结构·学习·算法
举一个梨子zz32 分钟前
Java—— 可变参数、集合工具类、集合嵌套、不可变集合
java·开发语言·intellij-idea·需求分析
算法给的安全感33 分钟前
bfs-最小步数问题
java·算法·宽度优先
iangyu36 分钟前
【windows server脚本每天从网络盘复制到本地】
开发语言·windows·php
程序员拂雨43 分钟前
Python知识框架
开发语言·python
灏瀚星空1 小时前
地磁-惯性-视觉融合制导系统设计:现代空战导航的抗干扰解决方案
图像处理·人工智能·python·深度学习·算法·机器学习·信息与通信
泽02021 小时前
C++类和对象之相关特性
java·开发语言·c++
敲键盘的小夜猫1 小时前
深入理解Python逻辑判断、循环与推导式(附实战案例)
开发语言·python