17.1节练习
来源:互联网 发布:tracert linux 编辑:程序博客网 时间:2024/05/21 11:37
练习17.1 定义一个保存三个int值得tuple,并将其成员分别初始化为10、20和30。
#include <iostream>#include <tuple>#include <algorithm>using namespace std;int main(){tuple<int, int, int> iple{ 10,20,30 };cout << get<0>(iple) << endl;}
练习17.2 定义一个tuple,保存一个string、一个vector<string>和一个pari<string,int>。
#include <iostream>#include <tuple>#include <utility>#include <string>#include <vector>#include <algorithm>using namespace std;int main(){string s = "durant";tuple<string, vector<string>, pair<string, int>> sple{ s,{"paul","pierce"},{"curi",30} };cout << get<0>(sple) << endl;cout << get<1>(sple)[0] << " " << get<1>(sple)[1] << endl;cout << get<2>(sple).first << " " << get<2>(sple).second << endl;}
练习17.3 重写12.3节(第430页)中的TextQuery程序,使用tuple代替QueryResult类。你认为哪种设计更好?为什么?
tuple类更方便。
练习17.4 编写并测试你自己版本的findBook函数。
bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs){return lhs.isbn() == rhs.isbn();}typedef tuple<vector<Sales_data>::size_type,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->end(), book, compareIsbn);if (found.first != found.second) {ret.push_back(make_tuple(it - files.cbegin(), found.first, found.second));}}return ret;}
练习17.5 重写findBook,令其返回一个pair,包含一个索引和一个迭代器pair。
【转载】
typedef pair<vector<Sales_data>::size_type, pair<vector<Sales_data>::const_iterator, vector<Sales_data>::const_iterator>> matches; pair<size_t, pair<vector::const_iterator, vector::const_iterator>> findBook(const vector<vector<Sales_data>> &files, const string &book) { pair<matches> ret; // initially empty // for each store find the range of matching books, if any for (auto it = files.cbegin(); it != files.cend(); ++it) { // find the range of Sales_data that have the same ISBN auto found = equal_range(it->cbegin(), it->cend(), book, compareIsbn); if (found.first != found.second) // this store had sales // remember the index of this store and the matching range ret = make_pair(it - files.cbegin(), found); } return ret; // empty if no matches found }
练习17.6 重写findBook,不适用tuple或pair。
【转载】
template <typename T> class matches_result{ public: make_match(vector<T>::size_type i, vector<T>::const_iterator it1, vector<T>::const_iterator it2) : index(i), first(it1), second(it2){} vector<T>::size_type index; vector<T>::const_iterator first, second; }; matches_result<Sales_data> findBook(const vector<vector<Sales_data>> &files, const string &book) { matches_result<Sales_data> ret; // initially empty // for each store find the range of matching books, if any for (auto it = files.cbegin(); it != files.cend(); ++it) { // find the range of Sales_data that have the same ISBN auto found = equal_range(it->cbegin(), it->cend(), book, compareIsbn); if (found.first != found.second) // this store had sales // remember the index of this store and the matching range ret.make_match(it - files.cbegin(), found.first, found.second); } return ret; // empty if no matches found }
0 0
- 17.1节练习
- 第十七章 17.1.1节练习
- 第十七章 17.1.2节练习
- C++ Primer 练习答案 1.2节练习
- C++ Primer 练习答案 1.4节练习
- 第一章 1.1节练习
- 第一章 1.2节练习
- 第一章 1.3节练习
- 10.1节练习
- 10.2.1节练习
- 10.2.2节练习
- 10.2.3节练习
- 10.3.1节练习
- 10.3.2 节练习
- 10.3.3节练习
- 10.3.4节练习
- 10.4.1节练习
- 10.4.2 节练习
- js可编辑表格
- 功能强大的html网站后台管理系统模板代码下载
- Android 禁止系统休眠,使屏幕不变暗,保持游戏中屏幕高亮
- HTTP基础:URL格式、 HTTP请求、响应、消息
- angularjs 保存数据 和 check
- 17.1节练习
- EJB知识点大全
- 数据结构 猫狗队列
- UnicodeDecodeError: ‘XXX’ codec can’t decode bytes in position 2-5: illegal multibyte sequence
- 职场职位缩写 PM,TM,PL,TL,SE,PG,CEO,CFO
- static全局变量与普通的全局变量有什么区别?static局部变量和普通局部变量有什么区别?static函数与普通函数有什么区别?
- iPhone - 读取工程包中的二进制文件
- JS刷新页面的几种方法
- 20160705_Java基础知识(访问权限)