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

A

alexey_m

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.
 
H

Heinz Ozwirk

alexey_m said:
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 said:
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
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

alexey_m said:
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
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top