现在的位置: 首页 > 综合 > 正文

multiset

2018年02月15日 ⁄ 综合 ⁄ 共 992字 ⁄ 字号 评论关闭
#include <string>
#include <set>
#include <iostream>

struct TableItem
{
	int id;
	std::string idText;
	std::string positionText;
	std::string shearText;
	std::string momentText;
};

struct CompareIDText
{
	bool operator()(const TableItem * t1, const TableItem * t2) const;
};

bool CompareIDText::operator()(const TableItem * t1, const TableItem * t2) const
{
	//非零即真 
	int res = strcmp(t1->idText.c_str(), t2->idText.c_str());
	if (res < 0)
	{
		return true;
	}
	return false;
}

void main()
{
	const int arraySize = 5;
	TableItem items[arraySize];
	items[0].idText="1";
	items[0].positionText="10";
	items[1].idText="2";
	items[1].positionText="20";
	items[2].idText="3";
	items[2].positionText="30";
	items[3].idText="1";
	items[3].positionText="40";
	items[4].idText="2";
	items[4].positionText="50";

	typedef std::multiset<TableItem *, CompareIDText> Tables;
	Tables tables;
	for(int i=0; i<arraySize; ++i)
	{
		tables.insert(&items[i]);
	}

	Tables::iterator ite = tables.begin();
	Tables::iterator iteEnd = tables.end();
	TableItem * tItem;
	for(int j=1 ;ite!=iteEnd; ++ite, ++j)
	{
		tItem = *ite;
		std::cout << tItem->idText << std::endl;
	}
	
}

输出结果

1
1
2
2
3

抱歉!评论已关闭.