728x90
std::map 과 같지만 일반적으로 속도가 더 빠른 것으로 알려져 있는 unordered map 과 소스를 ptr 로 썼을 때의
안정성을 강화시키는 ptr_ 시리즈를 합해 ptr_unordered_map 이라는 컨테이너가 있습니다. 별도의 clear 를 호출
하지 않아도 메모리의 누수가 발생하지 않습니다. boost::shared_ptr 과의 상성이 좋다고 추천하고 있으니 같이
써 보도록 하지요
예제
#include "stdafx.h"
#include <boost/shared_ptr.hpp>
#include <boost/timer.hpp>
#include <boost/ptr_container/ptr_unordered_map.hpp>
#include <iostream>
class CTest
{
float m_fLifeSec;
boost::timer m_Timer;
public:
bool m_bDeleteMe;
void Run()
{
if(m_Timer.elapsed() > m_fLifeSec)
m_bDeleteMe = true;
}
CTest(float f) : m_fLifeSec(f)
{
m_bDeleteMe = false;
m_Timer.restart();
};
~CTest() {};
};
int _tmain(int argc, _TCHAR* argv[])
{
boost::ptr_unordered_map< int, boost::shared_ptr<CTest> > List;
for(int i=0; i<2; i++)
{
boost::shared_ptr<CTest> pTemp(new CTest(3));
List[i] = pTemp;
}
while(List.size() > 0)
{
for(auto it=List.begin(); it!=List.end();)
{
auto pTmp = it->second->get();
if(pTmp != nullptr)
pTmp->Run();
if(pTmp->m_bDeleteMe)
{
it = List.erase(it);
continue;
}
std::cout << "size " << List.size() << std::endl;
++it;
}
}
int n;
std::cin >> n;
}
728x90
'프로그래밍 > boost' 카테고리의 다른 글
| boost::atomic & lockfree (0) | 2013.06.14 |
|---|---|
| boost 라이브러리 설치 (0) | 2013.06.12 |
| boost::program_options (0) | 2012.11.16 |
| boost::function / boost::tuple 의 활용 (0) | 2012.11.07 |
| boost::tuple / tuples / tuples::tuple (0) | 2012.11.06 |
댓글