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
原创粉丝点击