Compile Error... STL?

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
ulin
New poster
Posts: 15
Joined: Wed Jun 30, 2004 11:24 am
Location: POLAND (Plock)

Compile Error... STL?

Post 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

CDiMa
Experienced poster
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

Re: Compile Error... STL?

Post 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

ulin
New poster
Posts: 15
Joined: Wed Jun 30, 2004 11:24 am
Location: POLAND (Plock)

Re: Compile Error... STL?

Post 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

oldbam
New poster
Posts: 17
Joined: Tue Sep 14, 2004 9:30 am

Post by oldbam »

Try to write
[cpp]#include <cstdio>[/cpp]
instead of [cpp]include <stdio.h>[/cpp]
Life is beautifull !!!

Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post 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]

ulin
New poster
Posts: 15
Joined: Wed Jun 30, 2004 11:24 am
Location: POLAND (Plock)

Post 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!

Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post 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/

Post Reply

Return to “C++”