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;
        }
    }
}
相关推荐
虾..11 分钟前
C++ 哈希
开发语言·c++·哈希算法
循环过三天14 分钟前
3.1、Python-列表
python·算法
liu****20 分钟前
14.日志封装和线程池封装
linux·开发语言·c++
青青草原羊村懒大王21 分钟前
python基础知识三
开发语言·python
将编程培养成爱好26 分钟前
C++ 设计模式《统计辅助功能》
开发语言·c++·设计模式·访问者模式
fie888940 分钟前
基于循环谱分析的盲源分离信号处理MATLAB
开发语言·matlab·信号处理
kgduu41 分钟前
go-ethereum之rpc
开发语言·rpc·golang
dragoooon341 小时前
[优选算法专题六.模拟 ——NO.40~41 外观数列、数青蛙]
数据结构·算法·leetcode
yong99901 小时前
MATLAB倍频转换效率分析与最佳匹配角模拟
开发语言·前端·matlab
已黑化的小白1 小时前
Rust 的所有权系统,是一场对“共享即混乱”的编程革命
开发语言·后端·rust