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;
        }
    }
}
相关推荐
老歌老听老掉牙5 分钟前
使用 OpenCASCADE 提取布尔运算后平面图形的外轮廓
c++·平面·opencascade
微笑尅乐17 分钟前
三种思路彻底掌握 BST 判断(递归与迭代全解析)——力扣98.验证二叉搜索树
算法·leetcode·职场和发展
闻缺陷则喜何志丹22 分钟前
【动态规划】数位DP的原理、模板(封装类)
c++·算法·动态规划·原理·模板·数位dp
豆沙沙包?39 分钟前
2025年--Lc194-516. 最长回文子序列(动态规划在字符串的应用,需要二刷)--Java版
java·算法·动态规划
胖咕噜的稞达鸭40 分钟前
二叉树搜索树插入,查找,删除,Key/Value二叉搜索树场景应用+源码实现
c语言·数据结构·c++·算法·gitee
_extraordinary_40 分钟前
Java Spring配置
java·开发语言·spring
showmethetime41 分钟前
基于相空间重构的混沌时间序列预测MATLAB实现
算法
进击的大海贼1 小时前
QT-C++ 自定义加工统计通用模块
开发语言·c++·qt
Rhys..1 小时前
JS - npm init
开发语言·javascript·npm
地平线开发者1 小时前
大模型 | VLM 初识及在自动驾驶场景中的应用
算法·自动驾驶