17.1节练习

练习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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值