Page 3 of 9
Posted: Thu Jun 17, 2004 10:06 pm
by Krzysztof Duleba
Because streams (and esp. iomanip) were implemented in a different way in old version of g++. The flags were integers, so cout << an_ios_flag simply prints (and printed) an integer. That's why setf function is necessary to set a flag.
This was changed so that flags have their own class now (std::ios_base). operator<<, overloaded for this type, calls setf.
Flags like std::ios::right (which is equal 128 in g++ 3.3), for compatibility, are still integers. However, std::right is a std::ios_base object and can be passed as argument to operator<<.
Posted: Thu Jun 17, 2004 10:16 pm
by PunyTroll
hm, actually it was std::cout << std::right which caused the error message in the first place.
Code: Select all
02603660_24.c: In function `int main()':
02603660_24.c:127: `::right' undeclared (first use here)
or did I misinterpret your response
Hagen.
Posted: Thu Jun 17, 2004 11:13 pm
by Krzysztof Duleba
Read again. Error message that you quoted comes from OJ, which works on g++ 2.95. The old version. It has no std::right. It has no ability to use cout << a_flag neither, for the reasons that I already mentioned.
Posted: Thu Jun 17, 2004 11:33 pm
by PunyTroll
hm, reading again, I still can't see it but that doesn't matter now. I see what you mean and additionally I finally found the information which version of gcc OJ uses. Thx in any case
Hagen.
Posted: Sat Jul 10, 2004 10:48 am
by cytmike
I used STL hash_map
But dunno why
I still got TLE...
can anybody help me?
[cpp]#include <hash_set.h>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
int p;
cin>>p;
char h[32];
cin.getline(h,32);
cin.getline(h,32);
for (int y=0;y<p;y++)
{
if (y)
cout<<endl;
hash_multiset <char*> sky;
cin.getline(h,32);
while (strlen(h))
{
sky.insert(h);
cin.getline(h,32);
cout<<h<<endl;
cout<<sky.count(h)<<endl;
}
double i=sky.size()/100.0;
string s="";
cout<<i<<endl;
copy(sky.begin(), sky.end(), ostream_iterator<char*>(cout, " "));
for (hash_multiset <char*>::iterator l=sky.begin();l!=sky.end();l++)
{
if (*l!=s)
cout<<(*l)<<' '<<setprecision(4)<<setiosflags(ios::fixed)<<sky.count(*l)/i<<endl;
s=*l;
}
}
return 0;
} [/cpp]
Posted: Thu Jul 15, 2004 5:18 am
by minskcity
First I used STL map - TLE.
Then I implemented my own hash, which made my program ran in linear time - TLE.
I changed my code to process only first 3 inputs - WA in 8.5 sec...
Then I sent:
[cpp]string s;
while(getline(cin, s));[/cpp] - TLE !!!!!????
I guess this is input problem, not algorithmic problem...

Posted: Thu Jul 15, 2004 6:06 am
by cytmike
Yea, that's the input prob
I did this before too.
It seems that we can only use C-styled character array instead of C++ strings as input.
However, we can't use this for STL map

Posted: Thu Jul 15, 2004 4:00 pm
by Larry
Where t is the name of the map, and a is the name of the char array.
t[ string( a ) ] = whatever;
(Though, of course, it takes time..)
I used STL's map and got AC in about 5 secs..
Posted: Thu Jul 15, 2004 5:38 pm
by minskcity
Larry wrote:Where t is the name of the map, and a is the name of the char array.
t[ string( a ) ] = whatever;
(Though, of course, it takes time..)
I used STL's map and got AC in about 5 secs..
Who did you read char arrays? gets()?
Posted: Thu Jul 15, 2004 5:54 pm
by cytmike
minskcity wrote:Larry wrote:Where t is the name of the map, and a is the name of the char array.
t[ string( a ) ] = whatever;
(Though, of course, it takes time..)
I used STL's map and got AC in about 5 secs..
Who did you read char arrays? gets()?
yea
i used sth like this:
char y[size];
cin>>y;
set<char *> t;
t[y]=...;
but when i call t.count(y) it is always 0
my code is shown above
the hash set throw away all data
Posted: Thu Jul 15, 2004 6:34 pm
by minskcity
char y[size];
cin>>y;
set<char *> t;
t[y]=...;
1) STL "set" is not hashset, optional second parameter in the template is comparator.
2) STL "set" does not look to support []. STL "map" does.
3) cin >> y will only read until space.
4) set < char * > might create a set of pointers, not strings.
---- According to Larry, it's possible to get AC by using STL map ( not hashed ) by reading char* and converting them into strings when accessing the map.
I was asking Larry which function he was using to read char arrays fast enough...
Posted: Thu Jul 15, 2004 7:39 pm
by cytmike
I used this:
[cpp]hash_multiset <char*> sky;
cin.getline(h,32);
while (strlen(h))
{
sky.insert(h);
cin.getline(h,32);
}[/cpp]
a set of pointers? why? what should i do then? convert c-styled char array to c++ string first?
Posted: Thu Jul 15, 2004 7:57 pm
by Larry
I don't have the code in front of me, but I almost never use cin, (since I am still mostly a C programmer)
I do something like this:
Code: Select all
char a[MAX_L];
map<string, int> t;
while ( gets( a ) ) {
t[ string( a ) ]++;
}
Of course, you have to clear and stuff, but that's how I handle it..
Posted: Thu Jul 15, 2004 8:20 pm
by minskcity
Larry wrote:I don't have the code in front of me, but I almost never use cin, (since I am still mostly a C programmer)
I do something like this:
Code: Select all
char a[MAX_L];
map<string, int> t;
while ( gets( a ) ) {
t[ string( a ) ]++;
}
Of course, you have to clear and stuff, but that's how I handle it..
THANKS A LOT!!!!
The only change I made to my program was getline->gets...
That corresponded to 10+sec->1.77sec, AC, 7th in ranking... (looks like my hashing works)
PS: You might be a mostly a C programmer, but I bet you are using STL map whenever is possible.

Posted: Thu Jul 15, 2004 9:31 pm
by Larry
I am indeed a C programmer.. but I was learning the STL at some point.. no point in reinventing the wheels, eh?