method max_size() for map-object (STL) returns -1

Discussion in 'C++' started by alexey_m, Dec 22, 2005.

  1. alexey_m

    alexey_m Guest

    Hi!
    Code:

    std::map<int,MyClass*> m;
    int mapsize=m.max_size();

    So, mapsize is -1.
    Can anyone tell me why?
    Compile with g++ 3.2.2 under Linux. Under Windows with MS Compiler
    everything is OK (not -1).
    PS: for std::vector max_size() returns correct value.
    Thanks.
     
    alexey_m, Dec 22, 2005
    #1
    1. Advertising

  2. alexey_m

    Heinz Ozwirk Guest

    "alexey_m" <> schrieb im Newsbeitrag
    news:...
    > Hi!
    > Code:
    >
    > std::map<int,MyClass*> m;
    > int mapsize=m.max_size();
    >
    > So, mapsize is -1.
    > Can anyone tell me why?


    max_size does not return -1, it returns an unsigned value, which cannot be
    negative. But it can be outside the range of an int. Try

    std::map<int, MyClass*>::size_type size= m.max_size();

    > Compile with g++ 3.2.2 under Linux. Under Windows with MS Compiler
    > everything is OK (not -1).


    Incidently the MS compiler returns a value in the range of an int.

    > PS: for std::vector max_size() returns correct value.


    std::map<>::max-size also returns the correct value, but your usage is not
    correct.

    HTH
    Heinz
     
    Heinz Ozwirk, Dec 22, 2005
    #2
    1. Advertising

  3. alexey_m wrote:
    > Hi!
    > Code:
    >
    > std::map<int,MyClass*> m;
    > int mapsize=m.max_size();
    >
    > So, mapsize is -1.
    > Can anyone tell me why?
    > Compile with g++ 3.2.2 under Linux. Under Windows with MS Compiler
    > everything is OK (not -1).
    > PS: for std::vector max_size() returns correct value.
    > Thanks.
    >


    Try this:

    <code>
    #include <iostream>
    #include <map>

    using namespace std;

    struct MyClass {};
    int main()
    {
    map<int, MyClass*> m;
    int n = m.max_size();
    cout << "m.max_size(): " << m.max_size() << '\n';
    cout << "int n=max_size(): " << n << '\n';
    return 0;
    }
    </code>

    Nothing more to say, I guess...


    Stefan
    --
    Stefan Naewe
    naewe.s_AT_atlas_DOT_de
     
    =?ISO-8859-1?Q?Stefan_N=E4we?=, Dec 22, 2005
    #3
    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. Kevin Goodsell

    Exceeding container::max_size()?

    Kevin Goodsell, Apr 3, 2004, in forum: C++
    Replies:
    1
    Views:
    392
    Victor Bazarov
    Apr 4, 2004
  2. t

    max_size()

    t, Oct 2, 2007, in forum: C++
    Replies:
    4
    Views:
    428
    James Kanze
    Oct 3, 2007
  3. Sarath
    Replies:
    2
    Views:
    640
    Knockr
    Aug 19, 2009
  4. Juha Nieminen
    Replies:
    0
    Views:
    336
    Juha Nieminen
    Feb 10, 2011
  5. Great Deals
    Replies:
    1
    Views:
    244
    laura fairhead
    Oct 1, 2003
Loading...

Share This Page