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;
        }
    }
}
相关推荐
云泽80814 小时前
从零搭建 WSL2 + Ubuntu C/C++ 开发环境:原理、实践与底层架构解析
linux·c++·windows·ubuntu·wsl2
不会就选b20 小时前
算法日常・每日刷题--<贪心>14
算法
初願致夕霞20 小时前
C++11:类型推导与完美转发复盘笔记
c++
小猪写代码20 小时前
C++中什么是类,什么是对象
c++
mmmmath_31 天前
LeetCode.541.反转字符串II
数据结构·算法·leetcode
Navigator_Z1 天前
LeetCode //MySQL - 1251. Average Selling Price
c语言·算法·leetcode
君顾11 天前
上海24小时自助健身房系统开发实战指南:从架构设计到落地部署
java·开发语言·健身房
醇氧1 天前
MySQL 8.0 系统表损坏与引擎转换故障排查实战
数据结构·算法
大熊背1 天前
《Color constancy by characterization of illumination chromaticity》之色度色域最大化算法(二)
算法·白平衡·色度·色温