std::tie在struct中的用法

#include <iostream>
#include <string>
#include <map>
#include <tuple>

using namespace std;

struct Test
{
	int id;
	std::string name;
	std::map<int, std::string> address;
	float wage;
	
	auto tie() const {return std::tie(id, name, address, wage);}
};

inline bool operator==(const Test& lhs, const Test& rhs)
{
	return lhs.tie() == rhs.tie();
}

int main() 
{
	std::map<int, std::string> address{{11, "shanghai"}};
	Test t1{11, "zhangshan", address, 33333.34};
	Test t2{11, "zhangshan", address, 33333.34};
	Test t3{11, "zhangshan", address, 33333.35};
	
	if (t1 == t2)
	{
		std::cout << "t1 == t2" << std::endl;
	}
	else
	{
		std::cout << "t1 != t2" << std::endl;
	}
	
	if (t1 == t3)
	{
		std::cout << "t1 == t3" << std::endl;
	}
	else
	{
		std::cout << "t1 != t3" << std::endl;
	}
	
	return 0;
}

運行結果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章