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;
        }
    }
}
相关推荐
liulilittle30 分钟前
LinkedList 链表数据结构实现 (OPENPPP2)
开发语言·数据结构·c++·链表
无聊的小坏坏1 小时前
三种方法详解最长回文子串问题
c++·算法·回文串
长路 ㅤ   1 小时前
Java后端技术博客汇总文档
分布式·算法·技术分享·编程学习·java后端
山河木马1 小时前
前端学习C++之:.h(.hpp)与.cpp文件
前端·javascript·c++
2401_891957311 小时前
list的一些特性(C++)
开发语言·c++
秋说1 小时前
【PTA数据结构 | C语言版】两枚硬币
c语言·数据结构·算法
二十雨辰1 小时前
[尚庭公寓]07-Knife快速入门
java·开发语言·spring
Python大数据分析@1 小时前
Origin、MATLAB、Python 用于科研作图,哪个最好?
开发语言·python·matlab
qq_513970441 小时前
力扣 hot100 Day37
算法·leetcode
编程零零七2 小时前
Python巩固训练——第一天练习题
开发语言·python·python基础·python学习·python练习题