Set, Map 的使用

 

 

 

 

//

//|---------------------------------------------------------------

//| Copyright (C) 2001 Reuters, --

//| 1400 Kensington Road, Oak Brook, IL. 60523 --

//| All rights reserved. Duplication or distribution prohibited --

//|---------------------------------------------------------------

//

//

// SubscriptionInfoMap.h: storage class for subscriptions in service

// name and item name pair

//

 

#if !defined(SUBSCIPTION_INFO_MAP_INCLUDED_)

#define SUBSCIPTION_INFO_MAP_INCLUDED_

 

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

 

#ifndef WIN32

#include <cassert>

#undef _ASSERTE

#define _ASSERTE assert

#endif

 

#include <set>

#include <map>

 

 

class SubscriptionInfoMap

{

public:

 

 

typedef std::map<std::string,std::set<std::string>*> SUBSCRIPTIONSMAP;

 

SubscriptionInfoMap();

virtual ~SubscriptionInfoMap();

 

void addSubscriptionBySubject(const std::string& subject);

void addSubscriptionByServiceItem(const std::string& serviceName,const std::string& itemName);

const std::set<std::string>* getSubListByServiceName(const std::string& serviceName) const;

bool hasService(const std::string& serviceName) const;

 

private:

friend class SubscriptionInfoMapIterator;

 

SUBSCRIPTIONSMAP _subscriptions;

bool _isSubjectList;

};

 

 

class SubscriptionInfoMapIterator

{

public:

explicit SubscriptionInfoMapIterator(const SubscriptionInfoMap& infoMap);

~SubscriptionInfoMapIterator();

 

void start() ;

void forth() ;

bool off() const;

const std::string& getServiceName() const;

const std::set<std::string>& getItemSet() const;

 

private:

const SubscriptionInfoMap& _infoMap;

SubscriptionInfoMap::SUBSCRIPTIONSMAP::const_iterator _mapIt;

};

 

#endif // !defined(SUBSCIPTION_INFO_MAP_INCLUDED_)

 

 

 

 

// SubscriptionInfoMap.cpp: implementation of the SubscriptionInfoMap class.

//

//

 

 

#pragma warning( disable : 4786)

#pragma warning( disable : 4503)

 

 

#include "StdAfx.h"

#include "SubscriptionInfoMap.h"

 

//

// Construction/Destruction

//

 

SubscriptionInfoMap::SubscriptionInfoMap()

{

 

}

 

SubscriptionInfoMap::~SubscriptionInfoMap()

{

if (_subscriptions.size())

{

for( SUBSCRIPTIONSMAP::iterator it = _subscriptions.begin();

it != _subscriptions.end();

it = _subscriptions.begin() )

{

std::set<std::string>* pItemSet = static_cast<std::set<std::string>*>((*it).second);

_subscriptions.erase( it );

pItemSet->clear();

delete pItemSet;

}

}

 

}

 

void SubscriptionInfoMap::addSubscriptionBySubject(const std::string& subject)

{

std::string serviceName;

 

int pos = subject.find_first_of(".",0);

if (pos == std::string::npos)

return;

else

serviceName = subject.substr(0,pos);

 

addSubscriptionByServiceItem(serviceName,subject);

}

 

void SubscriptionInfoMap::addSubscriptionByServiceItem(const std::string& serviceName,const std::string& itemName)

{

 

SUBSCRIPTIONSMAP::const_iterator it = _subscriptions.find(serviceName);

std::set<std::string>* pItemSet = 0;

 

if ( it != _subscriptions.end() )

{

pItemSet = (*it).second;

pItemSet->insert(itemName.c_str());

}

else

{

pItemSet = new std::set<std::string>;

pItemSet->insert(itemName.c_str());

_subscriptions.insert(SUBSCRIPTIONSMAP::value_type(serviceName.c_str(),pItemSet));

}

 

}

 

const

std::set<std::string>* SubscriptionInfoMap::getSubListByServiceName(const std::string& serviceName)

const

{

SUBSCRIPTIONSMAP::const_iterator it = _subscriptions.find(serviceName);

std::set<std::string>* pItemSet = 0;

 

if ( it != _subscriptions.end() )

return (*it).second;

else

return 0;

}

 

bool

SubscriptionInfoMap::hasService(const std::string& serviceName)

const

{

SUBSCRIPTIONSMAP::const_iterator it = _subscriptions.find(serviceName);

return it == _subscriptions.end()? false : true;

}

 

SubscriptionInfoMapIterator::SubscriptionInfoMapIterator(const SubscriptionInfoMap& infoMap)

:_infoMap(infoMap)

{

_mapIt = _infoMap._subscriptions.end();

}

 

SubscriptionInfoMapIterator::~SubscriptionInfoMapIterator()

{

 

}

 

void SubscriptionInfoMapIterator::start()

{

if (!_infoMap._subscriptions.empty())

_mapIt = _infoMap._subscriptions.begin();

}

 

void SubscriptionInfoMapIterator::forth()

{

_mapIt++;

}

 

bool

SubscriptionInfoMapIterator::off()

const

{

return (_mapIt == _infoMap._subscriptions.end())?true:false;

}

 

const

std::string& SubscriptionInfoMapIterator::getServiceName()

const

{

return (*_mapIt).first;

}

 

const

std::set<std::string>& SubscriptionInfoMapIterator::getItemSet()

const

{

return *((*_mapIt).second);

}

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值