c++primer 第十七章笔记 01tuple类型

c++primer 第十七章笔记 01tuple类型,第1张

tuple类型
	//tuple看作一个[快速而随意]的数据结构
	//三个成员都设置为0
	//tuple<size_t, size_t, size_t>threeD;
	//必须使用直接初始化,tuple的构造函数是explicit8789890-=
	tuple < string, vector<double>, int, list<int> >someVal("constants", { 3.14,2.719 }, 42, { 0,1,2,3,4 });
   //为什么可以
	tuple<size_t, size_t, size_t>threeD = { 1,2,3 };
	tuple<size_t, size_t, size_t>threed{ 1,2,3 };

	//类似make_pair ,用make_tuple来生成对象
	//item是一个tuple。


包含 const char* ,int,double auto item = make_tuple("0-99901423-X",3,20.00);

访问tuple的成员

	//类似make_pair ,用make_tuple来生成对象
	//item是一个tuple。


包含 const char* ,int,double auto item = make_tuple("0-99901423-X",3,20.00); //返回第一个成员 auto book = get<0>(item); cout <<book<< endl; //返回第二个成员 auto cnt = get<1>(item); cout << cnt << endl; //返回最后一个成员 auto price = get<2>(item)/cnt; cout << price << endl; //打折百分之20 get返回指定成员的引用,就是左值引用 get<2>(item) *= 0.8; cout << get<2>(item) << endl; //trans是item的类型 decltype确定trangs的类型 typedef decltype(item)trans; //返回trans类型对象中成员的数量 size_t sz = tuple_size<trans>::value; cout << sz << endl; //cnt的类型与item中第二个成员相同 cnt是一个int tuple_element<1, trans>::type CNT = get<1>(item); cout <<CNT << endl;

关系和相等运算符
类型不同或成员数量不同都不能使用关系和相等运算符

	tuple<string, string>duo("1", "2");
	tuple<size_t, size_t>twoD(1, 2);
	//bool b = (duo == twoD);//报错 ,不能比较 size_t 和string

	tuple<size_t, size_t, size_t>thREED(1, 2, 3);
	//bool b = (twoD < thREED); //报错 成员数量不同

	tuple<size_t, size_t>origin(0, 0);
	 bool b = (origin < twoD); //正确 ,b为true
	 cout << b << endl;

练习17.1.1节练习
练习17.1 17.2

	 tuple<int, int, int>a{ 10,20,30 };
	 cout << get<0>(a) << endl;
	 cout << get<1>(a) << endl;
	 cout << get<2>(a) << endl;

	 tuple<string, vector<string>, pair<string, int>>B{ "1",{"2"},{"3",4} };
	 cout << get<0>(B) << endl;
	 auto v= get<1>(B);
	 vector<string>::iterator X= v.begin();
	 cout << *X << endl;


	 auto PAIR = get<2>(B);
	 cout << PAIR.first << endl;
	 cout << PAIR.second << endl;

练习17.3

typedef vector<string>::size_type line_no;
typedef tuple<string, shared_ptr<set<line_no>>, shared_ptr<vector<string>>>QueryResult;

ostream& print(ostream& os, const QueryResult& qr);

class TextQuery
{
public:
	TextQuery(ifstream&);
	QueryResult query(const string&)const;

private:
	shared_ptr<vector<string>>file;
	map<string, shared_ptr<set<line_no>>>wm;

};

TextQuery::TextQuery(ifstream& infile) :file(make_shared<vector<string>>())
{
	string line;
	string word;
	line_no index = 0;

	while (getline(infile,line))
	{
		file->push_back(line);
		++index;
		istringstream strline(line);
		while (strline >> word)
		{
			if (ispunct(word[0]))
			{
				word.erase(0, 1);
			}
			if (ispunct(word[word.size() - 1]))
			{
				word.pop_back();
			}
			if (ispunct(word[word.size() - 1]))
			{
				word.pop_back();
			}
			if (!wm[word])
			{
				wm[word] = make_shared < set<line_no>>();
			}
			wm[word]->insert(index);
		}
	}
}
QueryResult TextQuery::query(const string& s)const
{
	static shared_ptr < set <line_no>>nodata(make_shared<set<line_no>>());

	auto loc = wm.find(s);
	if (loc == wm.end())
	{
		return QueryResult(s, nodata, file);
	}
	else
	{

		return QueryResult(s, loc->second, file);
	}
}

inline string make_plural(size_t ctr, const string& word, const string& ending)
{
	return (ctr > 1) ? word + ending : word;
}


ostream& print(ostream& os, const QueryResult& qr)
{
	os << get<0>(qr) << " occurs " << get<1>(qr)->size() << " "
		<< make_plural(get<1>(qr)->size(), "time", "s") << endl;

	for (auto num : *(get<1>(qr)))
		os << "\t(line " << num << ") " << (*get<2>(qr))[num - 1] << endl;

	return os;
}

如果查询的结果是临时使用,tuple就很轻松。



建议还是定义QueryResult类 ,可以有其他种 *** 作,比如多加几种查询方式

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/563124.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-02
下一篇 2022-04-02

发表评论

登录后才能评论

评论列表(0条)

保存