By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,414 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Dec 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"alexey_m" <no*****@no.where> schrieb im Newsbeitrag
news:e8******************************@localhost.ta 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
Dec 22 '05 #2

P: n/a
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
Dec 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.