Thread: C++ string and scanf

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    C++ string and scanf

    how to scan c++ strings using scanf
    Code:
    scanf("%s",str)
    doesn't work

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You don't. Why do you want to?

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    can i not do that...
    i should not use cin because it s too slow..

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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.

    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

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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?

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    can i not use any others... gets.... or something

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dpp View Post
    can i not use any others... gets.... or something
    There's getline.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    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?
    s tabstop... its not a real problem to ordinary users working at their computer...
    but in contests where a server and running time come into play it is

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    yeah...The first suggestion from the judge when i get TLE is avoid using cin 's

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    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;

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dpp
    Infact i got through wat i wanted but in extra time....I want to optimise 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().

    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.

    Quote Originally Posted by dpp
    fine can i not do this in map to store c strings
    You can, but you would need to provide a comparator if you want a key ordering other than by pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can but you need to supply a comparison function. This reference has an example of mapping const char*'s.

    unless by the rules of the competition the input is guaranteed to fit into str
    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.
    Last edited by anon; 06-12-2009 at 10:18 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scanf String
    By Demipimp in forum C Programming
    Replies: 9
    Last Post: 12-05-2008, 11:12 AM
  2. Scanf doesn't take in entire string
    By DragonX2n in forum C Programming
    Replies: 5
    Last Post: 01-31-2006, 09:30 PM
  3. is scanf as safe as fgets when used to receive string?
    By Antigloss in forum C Programming
    Replies: 4
    Last Post: 08-31-2005, 05:18 PM
  4. clearing buffer after reading string w/ scanf()
    By fisheromen1031 in forum C Programming
    Replies: 11
    Last Post: 08-01-2005, 09:33 AM
  5. String manipulation and scanf vs fgets
    By Nit in forum C Programming
    Replies: 9
    Last Post: 03-20-2002, 12:44 PM