Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > method max_size() for map-object (STL) returns -1

Reply
Thread Tools

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

 
 
alexey_m
Guest
Posts: n/a
 
      12-22-2005
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.

 
Reply With Quote
 
 
 
 
Heinz Ozwirk
Guest
Posts: n/a
 
      12-22-2005
"alexey_m" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) lkaboutprogramming.com...
> 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


 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Stefan_N=E4we?=
Guest
Posts: n/a
 
      12-22-2005
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
 
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
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
Sorted Returns List and Reversed Returns Iterator ++imanshu Python 7 08-23-2008 04:25 AM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
createImage sometime returns null and sometime returns non-null. vizlab Java 3 10-17-2007 11:21 AM
block returns and hash element returns Trans Ruby 2 11-06-2005 12:15 PM



Advertisments