Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > why is map::begin() returning const_iterator?

Reply
Thread Tools

why is map::begin() returning const_iterator?

 
 
Tim Partridge
Guest
Posts: n/a
 
      07-24-2003
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

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-24-2003

"Tim Partridge" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      07-24-2003
Tim Partridge wrote in
news(E-Mail Removed):

> 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:air< 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.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      07-24-2003
Buster Copley wrote in news:(E-Mail Removed):

>
> 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:perator -> ().second.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      07-24-2003
Rob Williscroft wrote in news:Xns93C2D264D45BEukcoREMOVEfreenetrtw@
195.129.110.131:

> Buster Copley wrote in news:(E-Mail Removed):
>
>>
>> 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:perator -> ().second.


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

ofcourse.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
Buster Copley
Guest
Posts: n/a
 
      07-24-2003
Rob Williscroft wrote:
> Rob Williscroft wrote in news:Xns93C2D264D45BEukcoREMOVEfreenetrtw@
> 195.129.110.131:
>
>
>>Buster Copley wrote in news:(E-Mail Removed):
>>
>>
>>>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:perator -> ().second.


I stand corrected.

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


But it's not a static function <g>.

map <...>::iterator iter = ...;
f (iter->second);

> Rob.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why is returning an int from a pointer crashing my app Milan Krejci C++ 4 08-28-2007 04:13 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
returning none when it should be returning a list? randomtalk@gmail.com Python 11 05-02-2006 10:26 AM
Why does this work? (returning automatic string) Phlip C++ 9 01-27-2004 05:14 PM



Advertisments