why is map::begin() returning const_iterator?

T

Tim Partridge

I want to use a map as a container storing foos and ints. I want to be
able to create pointers to the foos after they're in the container. How
can I do this? My following attempt fails:

#include <map>

class foo {
public:
bool operator<( const foo &f ) const { return true; }
};

int main() {
map< foo, int > m;
foo f;

m.insert( pair< foo, int >( f, 1 ) );
foo *fp = &(m.begin()->first); // this line gives an error

return 0;
}

In g++ 2.95.2, the following error is given:
main.cc:13: initialization to `foo *' from `const foo *' discards
qualifiers

So why is m.begin() returning a const_iterator instead of just a regular
iterator?

Thanks,
Tim Partridge
 
J

John Harrison

Tim Partridge said:
I want to use a map as a container storing foos and ints. I want to be
able to create pointers to the foos after they're in the container. How
can I do this? My following attempt fails:

#include <map>

class foo {
public:
bool operator<( const foo &f ) const { return true; }
};

int main() {
map< foo, int > m;
foo f;

m.insert( pair< foo, int >( f, 1 ) );
foo *fp = &(m.begin()->first); // this line gives an error

return 0;
}

In g++ 2.95.2, the following error is given:
main.cc:13: initialization to `foo *' from `const foo *' discards
qualifiers

So why is m.begin() returning a const_iterator instead of just a regular
iterator?

Thanks,
Tim Partridge

Its not, the keys of any map (foo in your case) are constant, you cannot
change them.

Just change 'foo *fp;' to 'const foo* fp;' like the error message suggests.

john
 
R

Rob Williscroft

Tim Partridge wrote in
I want to use a map as a container storing foos and ints. I want to be
able to create pointers to the foos after they're in the container.
How can I do this? My following attempt fails:

#include <map>

class foo {
public:
bool operator<( const foo &f ) const { return true; }
};

int main() {
map< foo, int > m;
foo f;

m.insert( pair< foo, int >( f, 1 ) );
foo *fp = &(m.begin()->first); // this line gives an error

return 0;
}

In g++ 2.95.2, the following error is given:
main.cc:13: initialization to `foo *' from `const foo *' discards
qualifiers

So why is m.begin() returning a const_iterator instead of just a
regular iterator?

It isn't, it's returning an iterator, your problem is that a
std::map< KeyType, ValueType > actually contains a
std::pair< KeyType const, ValueType >.

IOW m.begin()->first is a "foo const" and hence your problem.

This is by design, to stop you from changing the map's keys. If
you do do this (via reinterpret_cast<>) you will break the map,
searches (find and operator []) and inserts will not work as
designed.

HTH

Rob.
 
R

Rob Williscroft

Buster Copley wrote in
Maps don't have mutable iterators. For a map, iterator is the same type
as const_iterator. This is because assigning to a position in a map
doesn't make any sense. You want the position to be dependent on the
ordering of the elements, so you can't change elements, only remove and
add them.

This is true of std::set<>, however with std::map<> you can assign to
map<>::iterator::eek:perator -> ().second.

Rob.
 
R

Rob Williscroft

Rob Williscroft wrote in 195.129.110.131:
Buster Copley wrote in

This is true of std::set<>, however with std::map<> you can assign to
map<>::iterator::eek:perator -> ().second.

map<>::iterator::eek:perator -> () -> second.

ofcourse.

Rob.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top