#include "stdafx.h"
#include <iostream>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
using namespace boost;
using namespace boost::multi_index;
using namespace std;
class CShuihuBawcock
{
public:
CShuihuBawcock(int id_, std::string name_, std::string nickename)
:m_id(id_)
,m_name(name_)
, m_nickname(nickename)
{
}
public:
int m_id;
string m_name;
string m_nickname;
};
typedef multi_index_container <
CShuihuBawcock,
indexed_by<
ordered_unique<member<CShuihuBawcock, int, &CShuihuBawcock::m_id> >,
ordered_non_unique<member<CShuihuBawcock, string, &CShuihuBawcock::m_name> >,
ordered_non_unique<member<CShuihuBawcock, string, &CShuihuBawcock::m_nickname> >
>
> EmployeeContainer;
int _tmain(int argc, _TCHAR* argv[])
{
EmployeeContainer con;
con.insert(CShuihuBawcock(0, "宋江", "天魁星"));
con.insert(CShuihuBawcock(1, "吴用", "天机星"));
con.insert(CShuihuBawcock(2, "卢俊义", "天罡星"));
typedef EmployeeContainer::nth_index<0>::type IdIndex;
IdIndex::iterator it = con.get<0>().find(5);
if (it != con.get<0>().end())
{
cout << it->m_name << endl;
}
getchar();
return 0;
}先不多说 贴一段小代码 有时间再整理下:
本文介绍了一个使用C++ Boost库中的MultiIndexContainer来实现具有多种索引方式的容器示例。该示例创建了一个能根据ID、姓名及昵称进行查找的雇员信息容器。

1万+

被折叠的 条评论
为什么被折叠?



