How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Difference between std::cin and scanf applied to string Ask Question. Asked 3 years, 7 months ago. Active 3 years, 7 months ago. Viewed times. Improve this question. Artyom Gevorgyan Artyom Gevorgyan 95 1 1 silver badge 7 7 bronze badges.
What's the problem with using the code that works? Add a comment. Active Oldest Votes. Generally: don't use scanf with std::string. So if you want to read a whole line, you would be better off with: std::string s; std::getline std::cin, s ;. Improve this answer. Carl Carl 2, 1 1 gold badge 13 13 silver badges 20 20 bronze badges. Sign up or log in Sign up using Google.
Sign up using Facebook. The only visible difference is that scanf has to explicitly declare the input type, whereas cin has the redirection operation overloaded using templates. This does not seem like a good enough reason for a performance hit of 5x. With synchronization turned off, using cin and scanf together will result in an undefined mess.
This is probably because scanf interprets the format arguments at runtime and makes use of variable number of arguments, whereas cin does this at compile time.
Now wondering, how fast can it be done? This is fast!!! This article is contributed by Ayush Govil. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute geeksforgeeks.
See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Skip to content. Change Language. Related Articles. Save Article. Improve Article.
0コメント