how to scan c++ strings using scanf
doesn't workCode:scanf("%s",str)
Printable View
how to scan c++ strings using scanf
doesn't workCode:scanf("%s",str)
You don't. Why do you want to?
can i not do that...
i should not use cin because it s too slow..
Well, you could search for a better standard library implementation, where cin is not slow. Better yet, you could investigate if cin is actually slow in the first place. That is, if it is slowing your program down in a significant way.Quote:
i should not use cin because it s too slow..
But the C scanf is really C. It cannot deal with user-defined types. You'd have to scanf into a character buffer and then copy that into a std::string.
cin is too slow? What are you talking about? (That is, what are you doing that you think this is the problem?) And why do you think scanf would be faster, given that it uses the same input device?
can i not use any others... gets.... or something
s tabstop... its not a real problem to ordinary users working at their computer...Quote:
cin is too slow? What are you talking about? And why do you think scanf would be faster, given that it uses the same input device?
but in contests where a server and running time come into play it is :)
So you have profiled the program and have determined that the overhead of cin over pure disk I/O is a bottleneck for your application?
yeah...The first suggestion from the judge when i get TLE is avoid using cin 's :)
That's a judge's suggestion. Probably a programmer who has a lot of experience. Still, just a programmer. Have you profiled?
Yes... But as a programmer I have followed the teh suggestions many a times and succeeded...So the suggestion was absolutely right....
:)
Infact i got through wat i wanted but in extra time....I want to optimise it
:)
fine can i not do this in map to store c strings
Code:multimap<char*, int> mymap;
multimap<char*, int>::iterator it;
Well, due to the possibility of buffer overflow you certainly should not be using scanf("%s", str) where str is some array of char unless by the rules of the competition the input is guaranteed to fit into str... in which case perhaps you should just use gets().Quote:
Originally Posted by dpp
You could also try things like reading character by character via getchar() and then using std::string::push_back(), but I have no idea if that would be an improvement or a regression. You'll have to test, but that does without saying.
You can, but you would need to provide a comparator if you want a key ordering other than by pointer.Quote:
Originally Posted by dpp
You can but you need to supply a comparison function. This reference has an example of mapping const char*'s.
I think he's talking about Sphere Online competition, which makes this suggestion, and their input is always well-described (strings have maximum length etc). Also, since they compile and run your code, you can't choose the <iostream> implementation.Quote:
unless by the rules of the competition the input is guaranteed to fit into str