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;
        }
    }
}
相关推荐
天天爱吃肉82189 分钟前
跟着创意天才周杰伦学新能源汽车研发测试!3年从工程师到领域专家的成长秘籍!
数据库·python·算法·分类·汽车
zh_xuan10 分钟前
kotlin lazy委托异常时执行流程
开发语言·kotlin
alphaTao20 分钟前
LeetCode 每日一题 2026/2/2-2026/2/8
算法·leetcode
甄心爱学习23 分钟前
【leetcode】判断平衡二叉树
python·算法·leetcode
阿猿收手吧!27 分钟前
【C++】string_view:高效字符串处理指南
开发语言·c++
颜酱33 分钟前
从二叉树到衍生结构:5种高频树结构原理+解析
javascript·后端·算法
不知名XL1 小时前
day50 单调栈
数据结构·算法·leetcode
玄同7651 小时前
我的 Trae Skill 实践|使用 UV 工具一键搭建 Python 项目开发环境
开发语言·人工智能·python·langchain·uv·trae·vibe coding
Word码1 小时前
[C++语法] 继承 (用法详解)
java·jvm·c++
@––––––1 小时前
力扣hot100—系列2-多维动态规划
算法·leetcode·动态规划