[완료] stdio (printf, scanf)와 iostream(cin, cout)을 혼용하는게 문제가 있을까요 ?

parkon의 이미지

몇천줄 정도의 간단한 C++ 코딩을 하고 있는데요,
이런 저런 다양한 형식의 출력문들이 많네요.
"%e12.3", "%f9.3" 이런 식의 고정 필드(?)로 자료를 출력하는 경우도 많고요.
이런 걸 보면 ,printf를 쓰는게 좋아 보이긴 한데
또 때론 cin, cout의 >>, <<를 쓰는게 간편할 때도 많구요.

물론 어느 한쪽으로 정해도 다 잘 할 수는 있겠지만
코딩 시간과 노력을 줄이기 위해,
여긴 이것 써고, 저긴 저거 써고 이렇게 하면
혹시 문제의 소지가 있을까요 ?

마구잡이로 섞어 사용하는게 문제가 있다면,
궁여지책으로, 입력은 cin >> 요 녀석으로 하고
출력은 printf("...") 요 녀석으로 하는건 어떨까요 ?

간단하고 별것 아니지만 오래 전부터 궁금해 하던 내용이여서
여기에 질문올립니다.

chadr의 이미지

혼용해서 쓰셔도 전혀 문제 없습니다.

printf로 할수 있는것은 전부 cout으로도 할수 있고 역으로도 가능합니다.

setprecision, setw, showbase, hex등을 이용하면 가능하며 자세한건 아래를 참조하세요.

http://www.cplusplus.com/reference/iostream/manipulators/

-------------------------------------------------------------------------------
It's better to appear stupid and ask question than to be silent and remain stupid.

empty2fill의 이미지

syncing iostream with stdio
http://stackoverflow.com/questions/1595355/syncing-iostream-with-stdio

디폴트로 sync_with_stdio 설정이 true로 되어 있기 때문에

stdio와 iostream을 섞어 써도 괜찮다고 하네요.

단, 성능에 불이익이 있다고 하네요. 어느 정도인지 모르겠지만.

——
———
Life is a tragedy when seen in close-up, but a comedy in long-shot. - Chaplin, Charlie -

parkon의 이미지

댓글 감사드립니다.
제 경우 입출력 루틴에서 성능은 별로 안 중요하기 때문에 일단 안심이 되는군요.
그리고 윗분이 말씀해 주신 대로 setw 등을 쓰면 제가 원하는 일을 할 수 있다는 건 알지만
코드가 길어지고 또 제 귀차니즘 때문에... ^^,
암튼 한가지로 통일해서 쓰는게 (이왕이면 cin, cout으로 쓰는게)
장기적으론 이익이 될 거라는 데는 동의합니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.