std::map
別のイテレータのイテレータをstd::map
キータイプとして使用するを作成したいVisualStudio2008 C++03アプリケーションがあります。しかし、キータイプを使用してそのマップから要素を消去しようとすると、問題が発生します。
この例では、の要素MyList
が5分以上経過すると、タイマーが起動してマップから要素を削除し、そのエージングタイマーを破棄する必要があります。
typedef std::map< Foo, FooBar > MyList;
typedef std::map< MyList::iterator, boost::shared_ptr< Timer > > MyListAgeTimers;
class A
{
public:
void AddItem( Foo& f, FooBar& fb )
{
CriticalSection::Guard g( lock_ );
std::pair< MyList::iterator, bool > new_foo =
my_list_.insert( std::make_pair( f, fb ) );
if( new_foo.second )
{
timers_.insert( std::make_pair(
new_foo.first,
boost::make_shared< Timer >( FiveMinutes, boost::bind( &A::OnAgeTimer, this, new_foo.first ) ) ) );
}
};
void OnAgeTimer( MyList::iterator item )
{
CriticalSection::Guard g( lock_ );
// erase by key-type generates an error:
// functional(143) : error C2676: binary '<' : 'const std::_Tree<_Traits>::iterator' does not define this operator or a conversion to a type acceptable to the predefined operator
timers_.erase( item );
// erase by iterator. works okay.
my_list_.erase( item );
};
private:
MyList my_list_;
MyListAgeTimers timers_;
CriticalSection lock_;
};
あるマップから別のマップへのキータイプとしてイテレータを使用できませんか?または、これに特化したものを定義する必要がありますoperator<
か?
編集:
(私にとって)明らかなことはこれだけです:
namespace std {
inline bool operator <( const MyList::iterator& a, const MyList::iterator& b )
{
return a->first < b->first;
}
};
std::operator<
しかし、それが正しければ、 2つのイテレータを比較するときのデフォルトの動作ではないのはなぜですか?