why is map::begin() returning const_iterator?

Discussion in 'C++' started by Tim Partridge, Jul 24, 2003.

  1. 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
    Tim Partridge, Jul 24, 2003
    #1
    1. Advertising

  2. "Tim Partridge" <> wrote in message
    news:p...
    > 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
    John Harrison, Jul 24, 2003
    #2
    1. Advertising

  3. Tim Partridge wrote in
    news:p:

    > 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.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Jul 24, 2003
    #3
  4. Buster Copley wrote in news::

    >
    > 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.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Jul 24, 2003
    #4
  5. Rob Williscroft wrote in news:Xns93C2D264D45BEukcoREMOVEfreenetrtw@
    195.129.110.131:

    > Buster Copley wrote in news::
    >
    >>
    >> 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.


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

    ofcourse.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Jul 24, 2003
    #5
  6. Rob Williscroft wrote:
    > Rob Williscroft wrote in news:Xns93C2D264D45BEukcoREMOVEfreenetrtw@
    > 195.129.110.131:
    >
    >
    >>Buster Copley wrote in news::
    >>
    >>
    >>>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.


    I stand corrected.

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


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

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

    > Rob.
    Buster Copley, Jul 24, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. john smith

    const_iterator

    john smith, Aug 5, 2003, in forum: C++
    Replies:
    5
    Views:
    6,641
    Uwe Schnitker
    Aug 5, 2003
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    855
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,756
    Smokey Grindel
    Dec 2, 2006
  4. Alf P. Steinbach

    Erase map item via a const_iterator

    Alf P. Steinbach, Jun 4, 2009, in forum: C++
    Replies:
    2
    Views:
    411
    James Kanze
    Jun 4, 2009
  5. ittium
    Replies:
    4
    Views:
    306
    ittium
    Dec 8, 2011
Loading...

Share This Page