Page 1 of 1

Compile Error... STL?

Posted: Fri Sep 17, 2004 10:43 am
by ulin
Why this code get Compile Error?
Please help...

Code: Select all

#include <stdio.h>
#include <string>
#include <hash_map>

using namespace std;

const int maxn = 11;

int n,test; 
hash_map<string,int> person;
char names[maxn][15];
char name[15];



int main()
{
	
	int x,y;
	
	int ile,cash;

	while (scanf ("%d",&n)==1)
	{
		person.clear();
		for (x=0; x<n; ++x)
		{
			scanf ("%s",&names[x]);
			person[names[x]]=0;
		}


		for (x=0; x<n; ++x)
		{
			scanf ("%s%d%d",&name,&cash,&ile);
			if (ile) person[name]-=cash-cash%ile;
			else     person[name]+=cash;

			for (y=0; y<ile; ++y)
			{
				scanf ("%s",&name); person[name]+=cash/ile;
			}
		}

		if (test++) printf ("\n");
		for (x=0; x<n; ++x)
		{
			printf ("%s %d\n",names[x],person[names[x]]);
		}

	}

	return 0;
}
Best Regards

Re: Compile Error... STL?

Posted: Fri Sep 17, 2004 12:21 pm
by CDiMa
ulin wrote:Why this code get Compile Error?
Please help...

Code: Select all

#include <stdio.h>
#include <string>
#include <hash_map>
[...]
Best Regards
OJ uses an old version of gcc... try this:
[cpp]
#include <hash_map.h>
[/cpp]

Ciao!!!

Claudio

Re: Compile Error... STL?

Posted: Fri Sep 17, 2004 2:38 pm
by ulin
CDiMa wrote: OJ uses an old version of gcc... try this:
[cpp]
#include <hash_map.h>
[/cpp]
It doesn't work.. It's still CE
I don't know how to use STL without CE
Please help...

Best Regards

Posted: Fri Sep 17, 2004 6:44 pm
by oldbam
Try to write
[cpp]#include <cstdio>[/cpp]
instead of [cpp]include <stdio.h>[/cpp]

Posted: Fri Sep 17, 2004 7:58 pm
by Krzysztof Duleba
The problem is that libstdc++ doesn't supply hash function for strings. You have to write it yourself, which is preety easy since there is hash function for const char *. For instance you can try this code:
[cpp]template<> struct hash< std::string > {
size_t operator()( const std::string& x ) const{
return hash< const char* >()( x.c_str() );
}
};[/cpp]

Posted: Sat Sep 18, 2004 1:57 pm
by ulin
Krzysztof Duleba wrote:The problem is that libstdc++ doesn't supply hash function for strings. You have to write it yourself, which is preety easy since there is hash function for const char *. For instance you can try this code:
[cpp]template<> struct hash< std::string > {
size_t operator()( const std::string& x ) const{
return hash< const char* >()( x.c_str() );
}
};[/cpp]

Thanks!!!
It works! I got AC

Could you explian me what is the function you wrote?
I've been trying use STL only for few weeks.

Maybe there are some webs where I could learn to use STL? or books?
I'm looking for sth for beginers.

Best Regards!

Posted: Sat Sep 18, 2004 10:11 pm
by Krzysztof Duleba
There is this great book "The C++ Standard Library. A Tutorial and Reference" by N. Josuttis, which was published in Poland by Helion last year.

Online references that might come in handy (but you have to know what you're looking for):
http://www.sgi.com/tech/stl/
http://www.dinkumware.com/