boost multi index

Programing/boost 2012.07.10 15:04 |

 

흥배님 블로그 참고 하여 작성

 http://jacking.tistory.com/

 

 

 

// testboost.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/key_extractors.hpp>
#include <boost/tuple/tuple.hpp>


class Character
{
public:
 Character( const int nCharID, const std::wstring strName, const int nlevel ) : m_nCharID(nCharID), m_strName(strName),
  m_nLevel( nLevel )
 {
  m_nExp = 0;
  m_nMoney = 0;
  m_nLevel = 0;
 }
 
private:

 int m_nCharID;
 std::wstring m_strName;
 int m_nExp;
 int m_nMoney;

 int m_nLevel;

public:
 const int CharID() const { return m_nCharID; }
 const int Level () const { return m_nLevel; }
};

typedef boost::multi_index::const_mem_fun <Character, const int , &Character::Level > IDX_LEVEL;
//typedef boost::multi_index::member <Character, int , &Character::m_nCharID > IDX_CHARID;


typedef boost::multi_index::const_mem_fun <Character, const int , &Character::CharID > IDX_CHARID2;


typedef struct indices : public boost::multi_index::indexed_by
 <
  boost::multi_index::hashed_unique <IDX_CHARID2>
  , boost::multi_index::hashed_non_unique <IDX_LEVEL>
 >
{
 enum INDEX
 {
  IDX_UNIQUE_CHARID,
  IDX_NON_UNIQUE_LEVEL,
  IDX_END
 };

}INDICES;


typedef boost::multi_index_container <Character, indices > Container;


int main()
{
 Container CharacterSet;

 CharacterSet.insert( Character(1, "test1", 21));
 CharacterSet.insert( Character(2, "test2", 31));
 CharacterSet.insert( Character(3, "test3", 39));
 CharacterSet.insert( Character(4, "test4", 35));
 CharacterSet.insert( Character(5, "test5", 21));
 CharacterSet.insert( Character(6, "test6", 12));
 CharacterSet.insert( Character(7, "test7", 10));
 CharacterSet.insert( Character(8, "test8", 41));
 

 std::cout << "모든 캐릭터 출력" << std::endl;
 std::for_each( CharacterSet.begin(), CharacterSet.end(), [](const Character& Char) {
  std::cout << "CharID : " << Char.m_nID << ", Level : " << Char.m_nLevel << ", Exp : " << Char.m_nExp << std::endl;
     } );
 std::cout << std::endl;


 /// modify
 std::cout << "캐릭터 ID 3번을 33번으로 그리고 레벨도 333으로 변경" << std::endl;

 Container::nth_index<indices::IDX_CHARID2>::type &Index1 = CharacterSet.get<indices::IDX_CHARID2>();
 Container::nth_index_iterator<indices::IDX_CHARID2>::type iter = Index1.find(3);

 if (Index1.end() != iter)
 {
  std::cout << "CharID : " << (*iter).m_nID << std::endl;
  Index1.modify( iter, [](Character& Char) { Char.m_nID = 33; Char.m_nLevel = 333; } );
 }
 std::cout << std::endl;


 std::for_each( CharacterSet.begin(), CharacterSet.end(), [](const Character& Char) {
  std::cout << "CharID : " << Char.m_nID << ", Level : " << Char.m_nLevel << ", Exp : " << Char.m_nExp << std::endl;
     } );
 std::cout << std::endl << std::endl;

 


 /// modify rollback
 std::cout << "캐릭터 ID 4번을 6번으로 그리고 레벨도 666으로 변경" << std::endl;

 iter = Index1.find(4);

 if (Index1.end() != iter)
 {
  std::cout << "CharID : " << (*iter).m_nID << "번을 6번, 레벨은 666으로 변경" << std::endl;
  int nCharID = (*iter).m_nID;
  int nLevel = (*iter).m_nLevel;
  Index1.modify( iter, [](Character& Char) { Char.m_nID = 6; Char.m_nLevel = 666; }, [nCharID, nLevel](Character& Char) { Char.m_nID = nCharID; Char.m_nLevel = nLevel; } );
 }
 std::cout << std::endl;


 std::for_each( CharacterSet.begin(), CharacterSet.end(), [](const Character& Char) {
  std::cout << "CharID : " << Char.m_nID << ", Level : " << Char.m_nLevel << ", Exp : " << Char.m_nExp << std::endl;
     } );
 std::cout << std::endl << std::endl;

 

 /// modify key
 std::cout << "캐릭터 ID 33번을 333333번으로 변경" << std::endl;

 iter = Index1.find(33);

 if (Index1.end() != iter)
 {
  std::cout << "CharID : " << (*iter).m_nID << std::endl;
  Index1.modify_key( iter, [](int& nCharID) { nCharID = 333333; } );
 }
 std::cout << std::endl;


 std::for_each( CharacterSet.begin(), CharacterSet.end(), [](const Character& Char) {
  std::cout << "CharID : " << Char.m_nID << ", Level : " << Char.m_nLevel << ", Exp : " << Char.m_nExp << std::endl;
     } );
 std::cout << std::endl;

 return 0;
}

 

 

'Programing > boost' 카테고리의 다른 글

boost multi index  (0) 2012.07.10
Posted by 패스맨

댓글을 달아 주세요