using a std::bitset in an iterator

S

Sean Farrow

Hi:
I have an iterator defined as follows:
std::map<std::bitset<6>, int>::iterator DotsIterator, SignsIterator;

I get errors that std::bitset does not declare the < operator. Does this
mean I can not use std::bitset in an iterator. Any help apreciated.

Cheers

Sean.
 
J

Juha Nieminen

Sean said:
Hi:
I have an iterator defined as follows:
std::map<std::bitset<6>, int>::iterator DotsIterator, SignsIterator;

I get errors that std::bitset does not declare the < operator. Does this
mean I can not use std::bitset in an iterator. Any help apreciated.

You have to specify a comparison operator for the map to use. For
example like this:

typedef std::bitset<6> KeyType;

struct BitSetComp
{
bool operator()(const KeyType& lhs, const KeyType& rhs)
{
return lhs.to_ulong() < rhs.to_ulong();
}
};

int main()
{
typedef std::map<KeyType, int, BitSetComp> BitSetMap;
BitSetMap m;
BitSetMap::iterator iter = m.begin();
}
 
T

Triple-DES

  You have to specify a comparison operator for the map to use. For
example like this:

typedef std::bitset<6> KeyType;

struct BitSetComp
{
    bool operator()(const KeyType& lhs, const KeyType& rhs)
    {
        return lhs.to_ulong() < rhs.to_ulong();
    }

};

nit: the operator must be const, otherwise the predicate can't be used
with map
int main()
{
    typedef std::map<KeyType, int, BitSetComp> BitSetMap;
    BitSetMap m;
    BitSetMap::iterator iter = m.begin();
}

DP
 
S

Sean Farrow

Hi:
When executing the following line:
DotsIterator =DotsList.find(dots);

I get the following error:

C2679: binary '=' : no operator found which takes a right-hand operand of
type 'std::_Tree<_Traits>::iterator' (or there . I
is no acceptable conversion)

DotsIterator is defined as:

std::map<std::bitset<6>, int>::iterator DotsIterator;

How can I solve this issue. I'm using visual c++ 2005.

Cheers

Sean.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top