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 小时前
干支编码的确定性实现:节气切分 + 1000 条溯源断语(附 JS 源码)
开发语言·javascript·ecmascript
roman_日积跬步-终至千里5 小时前
【算法五】从斐波那契到动态规划:自顶向下、自底向上与状态压缩
算法·动态规划
一晌小贪欢5 小时前
Python办公17:暴力拆分——按页数或指定范围提取 PDF 核心页面
java·开发语言·python·pdf·excel·数据可视化·python办公
孫治AllenSun5 小时前
【LangChain4J-06】Prompt 工程与模板化开发
开发语言·python·prompt
wuyk5555 小时前
9.冒泡排序:像鱼缸泡泡一样的简单排序算法
开发语言·stm32·单片机·算法·排序算法
路多辛5 小时前
为什么用 Go 写 AI Agent,covo-agent 的选型思考
开发语言·golang·agent·ai编程
渡我白衣6 小时前
深入理解 Transformer:Transformer 究竟是什么?
java·linux·开发语言·c++·人工智能·深度学习·transformer
无限码力7 小时前
8.26华为OD机试真题 新系统【字符串回文判断】
算法·华为od·华为od机考·华为od机试·华为od上机考试真题·华为od最新机试真题题解·华为od机试真题题库
玖玥拾9 小时前
LeetCode 125 验证回文串
算法·leetcode
「QT(C++)开发工程师」14 小时前
C++ 11 常用for循环
开发语言·c++