how to scan c++ strings using scanf
doesn't workCode:scanf("%s",str)
how to scan c++ strings using scanf
doesn't workCode:scanf("%s",str)
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.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.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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...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?
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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?
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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().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.Originally Posted by dpp
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.unless by the rules of the competition the input is guaranteed to fit into str
Last edited by anon; 06-12-2009 at 10:18 AM.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.