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

#undef _ASSERTE

#define _ASSERTE assert

#endif

 

#include

#include

 

 

class SubscriptionInfoMap

{

public:

 

 

typedef std::mapstd::set*> SUBSCRIPTIONSMAP;

 

SubscriptionInfoMap();

virtual ~SubscriptionInfoMap();

 

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

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

const std::set* 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& 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* pItemSet = static_cast*>((*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* pItemSet = 0;

 

if ( it != _subscriptions.end() )

{

pItemSet = (*it).second;

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

}

else

{

pItemSet = new std::set;

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

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

}

 

}

 

const

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

const

{

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

std::set* 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& SubscriptionInfoMapIterator::getItemSet()

const

{

return *((*_mapIt).second);

}

 

 

 

 

 

 

你可能感兴趣的:(C/C++,string,iterator,class,delete,c)