-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_notes_2_input_and_output_library.html
More file actions
583 lines (450 loc) · 54.3 KB
/
cpp_notes_2_input_and_output_library.html
File metadata and controls
583 lines (450 loc) · 54.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
<!DOCTYPE html>
<html>
<head>
<title>
C++笔记2——标准输入输出库 | 雅乐网 </title>
<meta charset="UTF-8" />
<meta name="renderer" content="webkit">
<link rel="stylesheet" href="http://www.yalewoo.com/wp-content/themes/YLW3_lite/style.css" type="text/css" />
<link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="http://www.yalewoo.com/feed" />
<!-- All in One SEO Pack 2.3.12.1 by Michael Torbert of Semper Fi Web Design[-1,-1] -->
<meta name="description" content="一、iostream iostream库又叫做标准输入输出库(Standard Input / Output Streams Library),它包含两个基础类型:istream和ostream,分别表示输入流和输出流。其中定义了4个IO对象: cin是istream对象,称为标准输入(standard" />
<link rel="canonical" href="http://www.yalewoo.com/cpp_notes_2_input_and_output_library.html" />
<!-- /all in one seo pack -->
<link rel="alternate" type="application/rss+xml" title="雅乐网 » C++笔记2——标准输入输出库评论Feed" href="http://www.yalewoo.com/cpp_notes_2_input_and_output_library.html/feed" />
<link rel='stylesheet' id='crayon-css' href='http://www.yalewoo.com/wp-content/plugins/crayon-syntax-highlighter/css/min/crayon.min.css?ver=_2.7.2_beta' type='text/css' media='all' />
<link rel='stylesheet' id='crayon-theme-classic-css' href='http://www.yalewoo.com/wp-content/plugins/crayon-syntax-highlighter/themes/classic/classic.css?ver=_2.7.2_beta' type='text/css' media='all' />
<link rel='stylesheet' id='crayon-font-consolas-css' href='http://www.yalewoo.com/wp-content/plugins/crayon-syntax-highlighter/fonts/consolas.css?ver=_2.7.2_beta' type='text/css' media='all' />
<script type='text/javascript' src='https://lib.sinaapp.com/js/jquery/1.8.2/jquery.min.js'></script>
<script type='text/javascript'>
/* <![CDATA[ */
var CrayonSyntaxSettings = {"version":"_2.7.2_beta","is_admin":"0","ajaxurl":"http:\/\/www.yalewoo.com\/wp-admin\/admin-ajax.php","prefix":"crayon-","setting":"crayon-setting","selected":"crayon-setting-selected","changed":"crayon-setting-changed","special":"crayon-setting-special","orig_value":"data-orig-value","debug":""};
var CrayonSyntaxStrings = {"copy":"Press %s to Copy, %s to Paste","minimize":"Click To Expand Code"};
/* ]]> */
</script>
<script type='text/javascript' src='http://www.yalewoo.com/wp-content/plugins/crayon-syntax-highlighter/js/min/crayon.min.js?ver=_2.7.2_beta'></script>
<link rel='prev' title='C++笔记3——引用' href='http://www.yalewoo.com/cpp_notes_3_reference.html' />
<link rel='next' title='UILabel的宽高自适应计算' href='http://www.yalewoo.com/uilabel_auto_height_width.html' />
<link rel='shortlink' href='http://www.yalewoo.com/?p=2808' />
<link rel="stylesheet" href="http://www.yalewoo.com/wp-content/plugins/wp-content-index/style.css" type="text/css" media="all" />
<!-- Start Of Script Generated By WP-PostViews -->
<script type="text/javascript">
/* <![CDATA[ */
jQuery.ajax({type:'GET',url:'http://www.yalewoo.com/wp-admin/admin-ajax.php',data:'postviews_id=2808&action=postviews',cache:false});/* ]]> */
jQuery(document).ready(function() {
var ajax_data = {
action: "show_postview",
postviews_id: 2808
};
$.post("http://www.yalewoo.com/wp-admin/admin-ajax.php", ajax_data,
function(data) {
$('.meta-view').html(data);
});
return false;
});
</script>
<!-- End Of Script Generated By WP-PostViews -->
<script src="http://www.yalewoo.com/wp-content/themes/YLW3_lite/js/jquery.lazyload.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$("#secondary img").lazyload({
effect:"fadeIn"
});
});
$(function() {
$("img").lazyload({
effect:"fadeIn"
});
});
</script>
<!--[if lte IE 8]><script>document.write("<p style=\"color:red;font-size:40px;\">你正在使用 Internet Explorer 的过期版本(IE6、IE7、IE8)<br/>请<a href=\"#\" style=\"color:blue;\">升级您的浏览器</a>获得更好的浏览体验。</p>");</script><![endif]-->
</head><body>
<header id="topheader">
<hgroup>
<h1><a href = "http://www.yalewoo.com">雅乐网</a>
</h1>
<h2>计算机技术博客</h2>
</hgroup>
<div id="top_menu">
<div class="menu-%e6%9c%80%e9%a1%b6%e7%ab%af-container"><ul id="menu-%e6%9c%80%e9%a1%b6%e7%ab%af" class="menu"><li id="menu-item-663" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-663"><a href="http://www.yalewoo.com/about">关于本站</a></li>
<li id="menu-item-662" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-662"><a href="http://www.yalewoo.com/updates">雅乐网更新记录</a></li>
<li id="menu-item-661" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-661"><a target="_blank" href="http://www.yalewoo.com/old0/">老版网站</a></li>
</ul></div> <form method="get" id="searchform" action="http://www.yalewoo.com/">
<div>
<input type="text" value="" name="s" id="s" size="15" />
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form> </div>
</header>
<nav class="main_nav">
<div class="menu-%e4%b8%bb%e8%8f%9c%e5%8d%9520171106-container"><ul id="menu-%e4%b8%bb%e8%8f%9c%e5%8d%9520171106" class="menu"><li id="menu-item-3235" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-3235"><a href="http://www.yalewoo.com/">首页</a></li>
<li id="menu-item-3237" class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor menu-item-has-children menu-item-3237"><a href="http://www.yalewoo.com/programming">编程</a>
<ul class="sub-menu">
<li id="menu-item-3238" class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor current-menu-parent current-post-parent menu-item-3238"><a href="http://www.yalewoo.com/programming/c_cpp">C/C++</a></li>
<li id="menu-item-3243" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3243"><a href="http://www.yalewoo.com/programming/data_structure">数据结构</a></li>
<li id="menu-item-3244" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3244"><a href="http://www.yalewoo.com/programming/basic_algorithm">算法</a></li>
<li id="menu-item-3240" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3240"><a href="http://www.yalewoo.com/programming/online_judge">OJ刷题</a></li>
<li id="menu-item-3239" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3239"><a href="http://www.yalewoo.com/programming/linux">Linux</a></li>
<li id="menu-item-3241" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-3241"><a href="http://www.yalewoo.com/programming/web">Web</a>
<ul class="sub-menu">
<li id="menu-item-3242" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3242"><a href="http://www.yalewoo.com/programming/web/wordpress">wordpress</a></li>
</ul>
</li>
</ul>
</li>
<li id="menu-item-3245" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-3245"><a href="http://www.yalewoo.com/algorithm">算法</a>
<ul class="sub-menu">
<li id="menu-item-3248" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3248"><a href="http://www.yalewoo.com/algorithm/maths">数学</a></li>
<li id="menu-item-3246" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3246"><a href="http://www.yalewoo.com/algorithm/ml_notes">机器学习</a></li>
<li id="menu-item-3281" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3281"><a href="http://www.yalewoo.com/algorithm/deep_learning">深度学习</a></li>
<li id="menu-item-3247" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3247"><a href="http://www.yalewoo.com/algorithm/python">python</a></li>
<li id="menu-item-3253" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3253"><a href="http://www.yalewoo.com/algorithm/%e7%a4%be%e5%9b%a2%e6%a3%80%e6%b5%8b">社团检测</a></li>
</ul>
</li>
<li id="menu-item-3254" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-3254"><a href="http://www.yalewoo.com/tools">工具教程</a>
<ul class="sub-menu">
<li id="menu-item-3255" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3255"><a href="http://www.yalewoo.com/tools/git">Git/GitHub</a></li>
<li id="menu-item-3256" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3256"><a href="http://www.yalewoo.com/tools/sublime_text">Sublime Text</a></li>
<li id="menu-item-3257" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3257"><a href="http://www.yalewoo.com/tools/vs2013">VS2013</a></li>
<li id="menu-item-3259" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3259"><a href="http://www.yalewoo.com/tools/browser">浏览器</a></li>
<li id="menu-item-3258" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3258"><a href="http://www.yalewoo.com/tools/other_tools">其他工具</a></li>
</ul>
</li>
<li id="menu-item-3260" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-3260"><a href="http://www.yalewoo.com/excellent_softwares">软件推荐</a>
<ul class="sub-menu">
<li id="menu-item-3261" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3261"><a href="http://www.yalewoo.com/excellent_softwares/zip">压缩加密</a></li>
<li id="menu-item-3262" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3262"><a href="http://www.yalewoo.com/excellent_softwares/pictools">图片工具</a></li>
<li id="menu-item-3263" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3263"><a href="http://www.yalewoo.com/excellent_softwares/media_tools">多媒体</a></li>
<li id="menu-item-3264" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3264"><a href="http://www.yalewoo.com/excellent_softwares/safe_software">安全清理</a></li>
<li id="menu-item-3265" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3265"><a href="http://www.yalewoo.com/excellent_softwares/android">安卓</a></li>
<li id="menu-item-3266" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3266"><a href="http://www.yalewoo.com/excellent_softwares/utility">实用工具</a></li>
<li id="menu-item-3267" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3267"><a href="http://www.yalewoo.com/excellent_softwares/search_tools">搜索词典</a></li>
<li id="menu-item-3268" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3268"><a href="http://www.yalewoo.com/excellent_softwares/efficiency_tools">效率提升</a></li>
<li id="menu-item-3269" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3269"><a href="http://www.yalewoo.com/excellent_softwares/programming_tools">编程开发</a></li>
<li id="menu-item-3270" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3270"><a href="http://www.yalewoo.com/excellent_softwares/internet_software">网络软件</a></li>
<li id="menu-item-3271" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3271"><a href="http://www.yalewoo.com/excellent_softwares/edit_and_reading">阅读编辑</a></li>
</ul>
</li>
<li id="menu-item-3272" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-3272"><a href="http://www.yalewoo.com/it_resource">资源</a>
<ul class="sub-menu">
<li id="menu-item-3273" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3273"><a href="http://www.yalewoo.com/it_resource/good_websites">好网站</a></li>
<li id="menu-item-3274" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3274"><a href="http://www.yalewoo.com/it_resource/stuff">好资料</a></li>
<li id="menu-item-3275" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3275"><a href="http://www.yalewoo.com/it_resource/how">授人以渔</a></li>
<li id="menu-item-3276" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3276"><a href="http://www.yalewoo.com/it_resource/ebooks-share">电子书</a></li>
</ul>
</li>
<li id="menu-item-3277" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-3277"><a href="http://www.yalewoo.com/learning">我爱学习</a>
<ul class="sub-menu">
<li id="menu-item-3278" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3278"><a href="http://www.yalewoo.com/learning/popular_science">科普</a></li>
</ul>
</li>
<li id="menu-item-3280" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-3280"><a href="http://www.yalewoo.com/about">关于本站</a></li>
</ul></div></nav>
<script type="text/javascript" src="http://www.yalewoo.com/wp-content/themes/YLW3_lite/js/dianzan.js"></script>
<div id="mbxdh">
<div>
<a href="http://www.yalewoo.com/programming">编程</a> » <a href="http://www.yalewoo.com/programming/c_cpp">C/C++</a> » C++笔记2——标准输入输出库 </div>
</div>
<div id="container">
<section class="whole_article" id="article-2808">
<article class="post-2808 post type-post status-publish format-standard hentry category-c_cpp tag-c" id="entry">
<h2 id="article-title">
<span class = "title-meta-yuanchuang title-meta-ico"></span>
<a href="http://www.yalewoo.com/cpp_notes_2_input_and_output_library.html" title="C++笔记2——标准输入输出库">C++笔记2——标准输入输出库</a>
</h2>
<div class="post-meta">
<span class="meta-author meta-ico"><a href="http://www.yalewoo.com/author/yalewoo" title="由yalewoo发布" rel="author">yalewoo</a> </span>
<span class="meta-time meta-ico"> 最后修改于 2017-11-06</span>
发表于 2016-11-29
<span class="meta-view meta-ico">452</span>
<span class="meta-comment meta-ico"><a href="http://www.yalewoo.com/cpp_notes_2_input_and_output_library.html#respond">0</a></span>
<br><br>
<span class="meta-category meta-ico"> <a href="http://www.yalewoo.com/programming/c_cpp" rel="category tag">C/C++</a> </span>
<span class="meta-category meta-ico"> <a href="http://www.yalewoo.com/tag/c" rel="tag">c++</a> </span>
</div>
<div id="article-content">
<div id="content-index" class="content-index" style="margin:0 0 10px 10px;float:right;"><span class="content-index-toctoggle">[<a id="content-index-togglelink" href="javascript:content_index_toggleToc()">目录开关</a>]</span>
<script type="text/javascript" language="javascript">
window.content_index_showTocToggle=true;function content_index_toggleToc(){var tts="显示目录";var tth="隐藏目录";if(window.content_index_showTocToggle){window.content_index_showTocToggle=false;document.getElementById("content-index-contents").style.display="block";document.getElementById("content-index-togglelink").innerHTML=tth}else{window.content_index_showTocToggle=true;document.getElementById("content-index-contents").style.display="none";document.getElementById("content-index-togglelink").innerHTML=tts}}
</script>
<ul id="content-index-contents"><li class="content-index-level-1"><a href="http://www.yalewoo.com/cpp_notes_2_input_and_output_library.html#一、iostream" title="一、iostream"><em>1</em><span>一、iostream</span></a><ul class="children"><li class="content-index-level-2"><a href="http://www.yalewoo.com/cpp_notes_2_input_and_output_library.html#向流写入数据(输出)" title="向流写入数据(输出)"><em>1.1</em><span>向流写入数据(输出)</span></a></li><li class="content-index-level-2"><a href="http://www.yalewoo.com/cpp_notes_2_input_and_output_library.html#命名空间" title="命名空间"><em>1.2</em><span>命名空间</span></a></li><li class="content-index-level-2"><a href="http://www.yalewoo.com/cpp_notes_2_input_and_output_library.html#从流读取数据(输入)" title="从流读取数据(输入)"><em>1.3</em><span>从流读取数据(输入)</span></a></li><li class="content-index-level-2"><a href="http://www.yalewoo.com/cpp_notes_2_input_and_output_library.html#c中的输入输出函数" title="c中的输入输出函数"><em>1.4</em><span>c中的输入输出函数</span></a></li></ul></li></ul></div>
<h3 id="一、iostream">一、iostream</h3>
<p><a target="_blank" href="http://7d9rd6.com1.z0.glb.clouddn.com/wp-content/uploads/2016/11/scrn20161129195023.png" rel="attachment wp-att-2815"><img class="alignnone size-full wp-image-2815" data-original="http://7d9rd6.com1.z0.glb.clouddn.com/wp-content/uploads/2016/11/scrn20161129195023.png" src="http://www.yalewoo.com/wp-content/themes/YLW3_lite/img/loading.gif" alt="scrn20161129195023" width="396" height="109" /></a></p>
<noscript><img class="alignnone size-full wp-image-2815" src="http://7d9rd6.com1.z0.glb.clouddn.com/wp-content/uploads/2016/11/scrn20161129195023.png" alt="scrn20161129195023" width="396" height="109" /></a></p></noscript>
<p>iostream库又叫做标准输入输出库(Standard Input / Output Streams Library),它包含两个基础类型:istream和ostream,分别表示输入流和输出流。其中定义了4个IO对象:</p>
<p>cin是istream对象,称为标准输入(standard input)</p>
<p>cout是ostream对象,称为标准输出(standard output)</p>
<p>另外还有两个ostream对象:cerr(标准错误)和clog(用来输出运行时的日志信息)</p>
<p>一般系统把程序运行窗口和这些对象关联,读取cin时就会从窗口读取,向cout、cerr和clog写数据时,会写到窗口。</p>
<p>要使用iostream库,首先要包含该头文件</p>
<div id="crayon-5a77f1c62ea27685691437" class="crayon-syntax crayon-theme-classic crayon-font-consolas crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim wrap" style=" margin-top: 12px; margin-bottom: 12px; font-size: 20px !important; line-height: 30px !important;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 20px !important; line-height: 30px !important;">
#include <iostream></textarea></div>
<div class="crayon-main" style="">
<table class="crayon-table">
<tr class="crayon-row">
<td class="crayon-nums " data-settings="show">
<div class="crayon-nums-content" style="font-size: 20px !important; line-height: 30px !important;"><div class="crayon-num" data-line="crayon-5a77f1c62ea27685691437-1">1</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 20px !important; line-height: 30px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5a77f1c62ea27685691437-1"><span class="crayon-p">#include <iostream></span></div></div></td>
</tr>
</table>
</div>
</div><p>但是,之后还不能直接使用cout,这是因为c++中引入了命名空间(namespace)的概念,它可以帮助避免一些命名冲突的问题。标准库中定义的名字都在命名空间std中,当使用时需要通过作用域运算符(::)来指出。这样使用cout就应该用std::cout。</p>
<h4 id="向流写入数据(输出)">向流写入数据(输出)</h4>
<p>一个输出hello world的例子如下:</p>
<div id="crayon-5a77f1c62ea33727782734" class="crayon-syntax crayon-theme-classic crayon-font-consolas crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim wrap" style=" margin-top: 12px; margin-bottom: 12px; font-size: 20px !important; line-height: 30px !important;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 20px !important; line-height: 30px !important;">
#include <iostream>
int main()
{
std::cout << "hello world" << std::endl;
return 0;
}</textarea></div>
<div class="crayon-main" style="">
<table class="crayon-table">
<tr class="crayon-row">
<td class="crayon-nums " data-settings="show">
<div class="crayon-nums-content" style="font-size: 20px !important; line-height: 30px !important;"><div class="crayon-num" data-line="crayon-5a77f1c62ea33727782734-1">1</div><div class="crayon-num" data-line="crayon-5a77f1c62ea33727782734-2">2</div><div class="crayon-num" data-line="crayon-5a77f1c62ea33727782734-3">3</div><div class="crayon-num" data-line="crayon-5a77f1c62ea33727782734-4">4</div><div class="crayon-num" data-line="crayon-5a77f1c62ea33727782734-5">5</div><div class="crayon-num" data-line="crayon-5a77f1c62ea33727782734-6">6</div><div class="crayon-num" data-line="crayon-5a77f1c62ea33727782734-7">7</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 20px !important; line-height: 30px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5a77f1c62ea33727782734-1"><span class="crayon-p">#include <iostream></span></div><div class="crayon-line" id="crayon-5a77f1c62ea33727782734-2"> </div><div class="crayon-line" id="crayon-5a77f1c62ea33727782734-3"><span class="crayon-t">int</span><span class="crayon-h"> </span><span class="crayon-e">main</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5a77f1c62ea33727782734-4"><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5a77f1c62ea33727782734-5"><span class="crayon-h"> </span><span class="crayon-v">std</span><span class="crayon-o">::</span><span class="crayon-r">cout</span><span class="crayon-h"> </span><span class="crayon-o"><<</span><span class="crayon-h"> </span><span class="crayon-s">"hello world"</span><span class="crayon-h"> </span><span class="crayon-o"><<</span><span class="crayon-h"> </span><span class="crayon-v">std</span><span class="crayon-o">::</span><span class="crayon-v">endl</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5a77f1c62ea33727782734-6"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-cn">0</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5a77f1c62ea33727782734-7"><span class="crayon-sy">}</span></div></div></td>
</tr>
</table>
</div>
</div><p>其中endl是一个特殊的操纵符,它的效果是结束当前行,并将缓冲区的内容写入到设备。</p>
<p>左移运算符<<在c++中又称为输出运算符,它左侧是ostream对象,右侧就是要输出的值,作用就是在输出对象上打印消息。由于它的运算结果正好是左侧的那个ostream对象,因此可以连用:</p>
<div id="crayon-5a77f1c62ea3a306534074" class="crayon-syntax crayon-theme-classic crayon-font-consolas crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim wrap" style=" margin-top: 12px; margin-bottom: 12px; font-size: 20px !important; line-height: 30px !important;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 20px !important; line-height: 30px !important;">
std::cout << "hello" << "world";</textarea></div>
<div class="crayon-main" style="">
<table class="crayon-table">
<tr class="crayon-row">
<td class="crayon-nums " data-settings="show">
<div class="crayon-nums-content" style="font-size: 20px !important; line-height: 30px !important;"><div class="crayon-num" data-line="crayon-5a77f1c62ea3a306534074-1">1</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 20px !important; line-height: 30px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5a77f1c62ea3a306534074-1"><span class="crayon-v">std</span><span class="crayon-o">::</span><span class="crayon-v">cout</span><span class="crayon-h"> </span><span class="crayon-o"><<</span><span class="crayon-h"> </span><span class="crayon-s">"hello"</span><span class="crayon-h"> </span><span class="crayon-o"><<</span><span class="crayon-h"> </span><span class="crayon-s">"world"</span><span class="crayon-sy">;</span></div></div></td>
</tr>
</table>
</div>
</div><p>std::cout << “hello”的结果还是std::cout,上面这句话相当于两句:</p>
<div id="crayon-5a77f1c62ea3f510740751" class="crayon-syntax crayon-theme-classic crayon-font-consolas crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim wrap" style=" margin-top: 12px; margin-bottom: 12px; font-size: 20px !important; line-height: 30px !important;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 20px !important; line-height: 30px !important;">
std::cout << "hello";
std::cout << "world";</textarea></div>
<div class="crayon-main" style="">
<table class="crayon-table">
<tr class="crayon-row">
<td class="crayon-nums " data-settings="show">
<div class="crayon-nums-content" style="font-size: 20px !important; line-height: 30px !important;"><div class="crayon-num" data-line="crayon-5a77f1c62ea3f510740751-1">1</div><div class="crayon-num" data-line="crayon-5a77f1c62ea3f510740751-2">2</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 20px !important; line-height: 30px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5a77f1c62ea3f510740751-1"><span class="crayon-v">std</span><span class="crayon-o">::</span><span class="crayon-v">cout</span><span class="crayon-h"> </span><span class="crayon-o"><<</span><span class="crayon-h"> </span><span class="crayon-s">"hello"</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5a77f1c62ea3f510740751-2"><span class="crayon-v">std</span><span class="crayon-o">::</span><span class="crayon-v">cout</span><span class="crayon-h"> </span><span class="crayon-o"><<</span><span class="crayon-h"> </span><span class="crayon-s">"world"</span><span class="crayon-sy">;</span></div></div></td>
</tr>
</table>
</div>
</div><p></p>
<h4 id="命名空间">命名空间</h4>
<p>除了每次使用std::使用标准库中的名字,还可以使用using声明来一劳永逸。它的格式如下:</p>
<div id="crayon-5a77f1c62ea45177323062" class="crayon-syntax crayon-theme-classic crayon-font-consolas crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim wrap" style=" margin-top: 12px; margin-bottom: 12px; font-size: 20px !important; line-height: 30px !important;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 20px !important; line-height: 30px !important;">
using namespace::name;</textarea></div>
<div class="crayon-main" style="">
<table class="crayon-table">
<tr class="crayon-row">
<td class="crayon-nums " data-settings="show">
<div class="crayon-nums-content" style="font-size: 20px !important; line-height: 30px !important;"><div class="crayon-num" data-line="crayon-5a77f1c62ea45177323062-1">1</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 20px !important; line-height: 30px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5a77f1c62ea45177323062-1"><span class="crayon-r">using</span><span class="crayon-h"> </span><span class="crayon-t">namespace</span><span class="crayon-o">::</span><span class="crayon-v">name</span><span class="crayon-sy">;</span></div></div></td>
</tr>
</table>
</div>
</div><p>使用之后可以直接访问这个name</p>
<div id="crayon-5a77f1c62ea4a305764331" class="crayon-syntax crayon-theme-classic crayon-font-consolas crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim wrap" style=" margin-top: 12px; margin-bottom: 12px; font-size: 20px !important; line-height: 30px !important;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 20px !important; line-height: 30px !important;">
#include <iostream>
using std::cout;
using std::endl;
int main()
{
cout << "hello world" << endl;
return 0;
}</textarea></div>
<div class="crayon-main" style="">
<table class="crayon-table">
<tr class="crayon-row">
<td class="crayon-nums " data-settings="show">
<div class="crayon-nums-content" style="font-size: 20px !important; line-height: 30px !important;"><div class="crayon-num" data-line="crayon-5a77f1c62ea4a305764331-1">1</div><div class="crayon-num" data-line="crayon-5a77f1c62ea4a305764331-2">2</div><div class="crayon-num" data-line="crayon-5a77f1c62ea4a305764331-3">3</div><div class="crayon-num" data-line="crayon-5a77f1c62ea4a305764331-4">4</div><div class="crayon-num" data-line="crayon-5a77f1c62ea4a305764331-5">5</div><div class="crayon-num" data-line="crayon-5a77f1c62ea4a305764331-6">6</div><div class="crayon-num" data-line="crayon-5a77f1c62ea4a305764331-7">7</div><div class="crayon-num" data-line="crayon-5a77f1c62ea4a305764331-8">8</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 20px !important; line-height: 30px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5a77f1c62ea4a305764331-1"><span class="crayon-p">#include <iostream></span></div><div class="crayon-line" id="crayon-5a77f1c62ea4a305764331-2"><span class="crayon-r">using</span><span class="crayon-h"> </span><span class="crayon-v">std</span><span class="crayon-o">::</span><span class="crayon-r">cout</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5a77f1c62ea4a305764331-3"><span class="crayon-r">using</span><span class="crayon-h"> </span><span class="crayon-v">std</span><span class="crayon-o">::</span><span class="crayon-v">endl</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5a77f1c62ea4a305764331-4"><span class="crayon-t">int</span><span class="crayon-h"> </span><span class="crayon-e">main</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5a77f1c62ea4a305764331-5"><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5a77f1c62ea4a305764331-6"><span class="crayon-h"> </span><span class="crayon-r">cout</span><span class="crayon-h"> </span><span class="crayon-o"><<</span><span class="crayon-h"> </span><span class="crayon-s">"hello world"</span><span class="crayon-h"> </span><span class="crayon-o"><<</span><span class="crayon-h"> </span><span class="crayon-v">endl</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5a77f1c62ea4a305764331-7"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-cn">0</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5a77f1c62ea4a305764331-8"><span class="crayon-sy">}</span></div></div></td>
</tr>
</table>
</div>
</div><p>还可以直接使用using指示,引入一个命名空间的所有名字</p>
<div id="crayon-5a77f1c62ea4f332016925" class="crayon-syntax crayon-theme-classic crayon-font-consolas crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim wrap" style=" margin-top: 12px; margin-bottom: 12px; font-size: 20px !important; line-height: 30px !important;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 20px !important; line-height: 30px !important;">
#include <iostream>
using namespace std;
int main()
{
cout << "hello world" << endl;
return 0;
}</textarea></div>
<div class="crayon-main" style="">
<table class="crayon-table">
<tr class="crayon-row">
<td class="crayon-nums " data-settings="show">
<div class="crayon-nums-content" style="font-size: 20px !important; line-height: 30px !important;"><div class="crayon-num" data-line="crayon-5a77f1c62ea4f332016925-1">1</div><div class="crayon-num" data-line="crayon-5a77f1c62ea4f332016925-2">2</div><div class="crayon-num" data-line="crayon-5a77f1c62ea4f332016925-3">3</div><div class="crayon-num" data-line="crayon-5a77f1c62ea4f332016925-4">4</div><div class="crayon-num" data-line="crayon-5a77f1c62ea4f332016925-5">5</div><div class="crayon-num" data-line="crayon-5a77f1c62ea4f332016925-6">6</div><div class="crayon-num" data-line="crayon-5a77f1c62ea4f332016925-7">7</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 20px !important; line-height: 30px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5a77f1c62ea4f332016925-1"><span class="crayon-p">#include <iostream></span></div><div class="crayon-line" id="crayon-5a77f1c62ea4f332016925-2"><span class="crayon-r">using</span><span class="crayon-h"> </span><span class="crayon-t">namespace</span><span class="crayon-h"> </span><span class="crayon-v">std</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5a77f1c62ea4f332016925-3"><span class="crayon-t">int</span><span class="crayon-h"> </span><span class="crayon-e">main</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5a77f1c62ea4f332016925-4"><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5a77f1c62ea4f332016925-5"><span class="crayon-h"> </span><span class="crayon-r">cout</span><span class="crayon-h"> </span><span class="crayon-o"><<</span><span class="crayon-h"> </span><span class="crayon-s">"hello world"</span><span class="crayon-h"> </span><span class="crayon-o"><<</span><span class="crayon-h"> </span><span class="crayon-v">endl</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5a77f1c62ea4f332016925-6"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-cn">0</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5a77f1c62ea4f332016925-7"><span class="crayon-sy">}</span></div></div></td>
</tr>
</table>
</div>
</div><p>使用之后,可以直接使用std下面的所有名字。一般不推荐使用,因为std中有很多名字,可能会和你自己的变量名字冲突。</p>
<h4 id="从流读取数据(输入)">从流读取数据(输入)</h4>
<p>右移运算符>> 也称为输入运算符,它左侧是一个istream对象,右侧是要写入内容的对象。它从istream中读取数据,并写入到右侧的对象中。和<<类似,它的结果也是左侧的istream对象,因此也可以连用</p>
<div id="crayon-5a77f1c62ea55657855097" class="crayon-syntax crayon-theme-classic crayon-font-consolas crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim wrap" style=" margin-top: 12px; margin-bottom: 12px; font-size: 20px !important; line-height: 30px !important;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 20px !important; line-height: 30px !important;">
#include <iostream>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
cout << a << "+" << b << "=" << a+b << endl;
return 0;
}</textarea></div>
<div class="crayon-main" style="">
<table class="crayon-table">
<tr class="crayon-row">
<td class="crayon-nums " data-settings="show">
<div class="crayon-nums-content" style="font-size: 20px !important; line-height: 30px !important;"><div class="crayon-num" data-line="crayon-5a77f1c62ea55657855097-1">1</div><div class="crayon-num" data-line="crayon-5a77f1c62ea55657855097-2">2</div><div class="crayon-num" data-line="crayon-5a77f1c62ea55657855097-3">3</div><div class="crayon-num" data-line="crayon-5a77f1c62ea55657855097-4">4</div><div class="crayon-num" data-line="crayon-5a77f1c62ea55657855097-5">5</div><div class="crayon-num" data-line="crayon-5a77f1c62ea55657855097-6">6</div><div class="crayon-num" data-line="crayon-5a77f1c62ea55657855097-7">7</div><div class="crayon-num" data-line="crayon-5a77f1c62ea55657855097-8">8</div><div class="crayon-num" data-line="crayon-5a77f1c62ea55657855097-9">9</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 20px !important; line-height: 30px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5a77f1c62ea55657855097-1"><span class="crayon-p">#include <iostream></span></div><div class="crayon-line" id="crayon-5a77f1c62ea55657855097-2"><span class="crayon-r">using</span><span class="crayon-h"> </span><span class="crayon-t">namespace</span><span class="crayon-h"> </span><span class="crayon-v">std</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5a77f1c62ea55657855097-3"><span class="crayon-t">int</span><span class="crayon-h"> </span><span class="crayon-e">main</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5a77f1c62ea55657855097-4"><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5a77f1c62ea55657855097-5"><span class="crayon-h"> </span><span class="crayon-t">int</span><span class="crayon-h"> </span><span class="crayon-v">a</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">b</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5a77f1c62ea55657855097-6"><span class="crayon-h"> </span><span class="crayon-v">cin</span><span class="crayon-h"> </span><span class="crayon-o">>></span><span class="crayon-h"> </span><span class="crayon-v">a</span><span class="crayon-h"> </span><span class="crayon-o">>></span><span class="crayon-h"> </span><span class="crayon-v">b</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5a77f1c62ea55657855097-7"><span class="crayon-h"> </span><span class="crayon-r">cout</span><span class="crayon-h"> </span><span class="crayon-o"><<</span><span class="crayon-h"> </span><span class="crayon-v">a</span><span class="crayon-h"> </span><span class="crayon-o"><<</span><span class="crayon-h"> </span><span class="crayon-s">"+"</span><span class="crayon-h"> </span><span class="crayon-o"><<</span><span class="crayon-h"> </span><span class="crayon-v">b</span><span class="crayon-h"> </span><span class="crayon-o"><<</span><span class="crayon-h"> </span><span class="crayon-s">"="</span><span class="crayon-h"> </span><span class="crayon-o"><<</span><span class="crayon-h"> </span><span class="crayon-v">a</span><span class="crayon-o">+</span><span class="crayon-v">b</span><span class="crayon-h"> </span><span class="crayon-o"><<</span><span class="crayon-h"> </span><span class="crayon-v">endl</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5a77f1c62ea55657855097-8"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-cn">0</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5a77f1c62ea55657855097-9"><span class="crayon-sy">}</span></div></div></td>
</tr>
</table>
</div>
</div><p>当需要读取的数据个数未知时,可以检测cin对象的值。下面是一个计算求和的例子</p>
<div id="crayon-5a77f1c62ea5b524248822" class="crayon-syntax crayon-theme-classic crayon-font-consolas crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim wrap" style=" margin-top: 12px; margin-bottom: 12px; font-size: 20px !important; line-height: 30px !important;">
<div class="crayon-plain-wrap"><textarea class="crayon-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 20px !important; line-height: 30px !important;">
#include <iostream>
using namespace std;
int main()
{
int sum = 0, value;
while (cin >> value)
{
sum += value;
}
cout << "sum = " << sum;
return 0;
}</textarea></div>
<div class="crayon-main" style="">
<table class="crayon-table">
<tr class="crayon-row">
<td class="crayon-nums " data-settings="show">
<div class="crayon-nums-content" style="font-size: 20px !important; line-height: 30px !important;"><div class="crayon-num" data-line="crayon-5a77f1c62ea5b524248822-1">1</div><div class="crayon-num" data-line="crayon-5a77f1c62ea5b524248822-2">2</div><div class="crayon-num" data-line="crayon-5a77f1c62ea5b524248822-3">3</div><div class="crayon-num" data-line="crayon-5a77f1c62ea5b524248822-4">4</div><div class="crayon-num" data-line="crayon-5a77f1c62ea5b524248822-5">5</div><div class="crayon-num" data-line="crayon-5a77f1c62ea5b524248822-6">6</div><div class="crayon-num" data-line="crayon-5a77f1c62ea5b524248822-7">7</div><div class="crayon-num" data-line="crayon-5a77f1c62ea5b524248822-8">8</div><div class="crayon-num" data-line="crayon-5a77f1c62ea5b524248822-9">9</div><div class="crayon-num" data-line="crayon-5a77f1c62ea5b524248822-10">10</div><div class="crayon-num" data-line="crayon-5a77f1c62ea5b524248822-11">11</div><div class="crayon-num" data-line="crayon-5a77f1c62ea5b524248822-12">12</div></div>
</td>
<td class="crayon-code"><div class="crayon-pre" style="font-size: 20px !important; line-height: 30px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="crayon-5a77f1c62ea5b524248822-1"><span class="crayon-p">#include <iostream></span></div><div class="crayon-line" id="crayon-5a77f1c62ea5b524248822-2"><span class="crayon-r">using</span><span class="crayon-h"> </span><span class="crayon-t">namespace</span><span class="crayon-h"> </span><span class="crayon-v">std</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5a77f1c62ea5b524248822-3"><span class="crayon-t">int</span><span class="crayon-h"> </span><span class="crayon-e">main</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5a77f1c62ea5b524248822-4"><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5a77f1c62ea5b524248822-5"><span class="crayon-h"> </span><span class="crayon-t">int</span><span class="crayon-h"> </span><span class="crayon-v">sum</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">0</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-t">value</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5a77f1c62ea5b524248822-6"><span class="crayon-h"> </span><span class="crayon-st">while</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-v">cin</span><span class="crayon-h"> </span><span class="crayon-o">>></span><span class="crayon-h"> </span><span class="crayon-t">value</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="crayon-5a77f1c62ea5b524248822-7"><span class="crayon-h"> </span><span class="crayon-sy">{</span></div><div class="crayon-line" id="crayon-5a77f1c62ea5b524248822-8"><span class="crayon-h"> </span><span class="crayon-v">sum</span><span class="crayon-h"> </span><span class="crayon-o">+=</span><span class="crayon-h"> </span><span class="crayon-t">value</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5a77f1c62ea5b524248822-9"><span class="crayon-h"> </span><span class="crayon-sy">}</span></div><div class="crayon-line" id="crayon-5a77f1c62ea5b524248822-10"><span class="crayon-h"> </span><span class="crayon-r">cout</span><span class="crayon-h"> </span><span class="crayon-o"><<</span><span class="crayon-h"> </span><span class="crayon-s">"sum = "</span><span class="crayon-h"> </span><span class="crayon-o"><<</span><span class="crayon-h"> </span><span class="crayon-v">sum</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5a77f1c62ea5b524248822-11"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-cn">0</span><span class="crayon-sy">;</span></div><div class="crayon-line" id="crayon-5a77f1c62ea5b524248822-12"><span class="crayon-sy">}</span></div></div></td>
</tr>
</table>
</div>
</div><p>当遇到文件结束符(end-of-file,windows命令行中可以通过Ctrl+z模拟)或者遇到无效输入(上例中输入不是数字)时,istream对象的状态会变为无效,从而跳出while循环。</p>
<h4 id="c中的输入输出函数">c中的输入输出函数</h4>
<p>c++中也可以使用printf和scanf这些c中的输入输出函数,头文件为cstdio。</p>
<p>c中的大部分头文件都有对应的c++版本,头文件名字为c中的名字去掉.h并在最前面加c。</p>
</div>
</article>
<div class="social-main">
<div class="post-like">
<a href="javascript:;" data-action="ding" data-id="2808" class="specsZan ">点赞 <span class="count">
0</span>
</a>
</div>
<div class="reward-button"><a href="http://www.yalewoo.com/denote" target="_blank">赏</a>
<span class="reward-code">
<span class="alipay-code"> <img class="alipay-img wdp-appear" src="http://www.yalewoo.com/wp-content/themes/YLW3_lite/img/alipay.png"><b>支付宝打赏</b> </span> <span class="wechat-code"> <img class="wechat-img wdp-appear" src="http://www.yalewoo.com/wp-content/themes/YLW3_lite/img/wechatpay.png"><b>微信打赏</b> </span>
</span>
</div>
<div class="post-like">
<a id="fenxianganniu" onClick="show_bdsharebox();">分享
</a>
</div>
<div class="bdsharebuttonbox" id="bdsharebuttonbox">
</div>
</div>
<div class="reward-notice">
<p class="">如果文章对你有帮助,欢迎点赞或打赏(金额不限)。你的打赏将全部用于支付网站服务器费用和提高网站文章质量,谢谢支持。</p>
</div>
<div class="article-copyright">
<b> 版权声明: </b>
<p> 本文由 <a href="http://www.yalewoo.com/author/yalewoo" title="由yalewoo发布" rel="author">yalewoo</a> 原创,商业转载请联系作者获得授权。 <br>非商业转载请注明作者 <a href="http://www.yalewoo.com/author/yalewoo" title="由yalewoo发布" rel="author">yalewoo</a> 或 <a href="http://www.yalewoo.com/" title="雅乐网" ?>雅乐网</a> ,并附带本文链接:<br><a href="http://www.yalewoo.com/cpp_notes_2_input_and_output_library.html" title=C++笔记2——标准输入输出库>http://www.yalewoo.com/cpp_notes_2_input_and_output_library.html</a></p>
</div>
<div class="post-navigation">
<div class="post-previous">
<p>上一篇:</p>
<a href="http://www.yalewoo.com/cpp_notes_3_reference.html" rel="prev">C++笔记3——引用</a> </div>
<div class="post-next">
<p>下一篇:</p>
<a href="http://www.yalewoo.com/uilabel_auto_height_width.html" rel="next">UILabel的宽高自适应计算</a> </div>
</div>
<div class="related_posts">
<p>与 <a href="http://www.yalewoo.com/tag/c" rel="tag">c++</a> 相关的文章</p>
<ul>
<li><a rel="bookmark" href="http://www.yalewoo.com/sublimegdb.html" title="Sublime Text 3 使用 SublimeGDB 图形化调试c/c++程序" target="_blank">Sublime Text 3 使用 SublimeGDB 图形化调试c/c++程序</a></li>
<li><a rel="bookmark" href="http://www.yalewoo.com/cpp_notes_3_reference.html" title="C++笔记3——引用" target="_blank">C++笔记3——引用</a></li>
<li><a rel="bookmark" href="http://www.yalewoo.com/c_to_cpp_notes_1_main_function.html" title="C++笔记1——main函数和命令行参数" target="_blank">C++笔记1——main函数和命令行参数</a></li>
<li><a rel="bookmark" href="http://www.yalewoo.com/binary_tree_graphical_display.html" title="二叉树的图形化显示" target="_blank">二叉树的图形化显示</a></li>
<li><a rel="bookmark" href="http://www.yalewoo.com/vs_debug_watch_pointer_like_array.html" title="VS调试时把指向数组的指针按照数组方式查看" target="_blank">VS调试时把指向数组的指针按照数组方式查看</a></li>
<li><a rel="bookmark" href="http://www.yalewoo.com/sublime_text_3_gcc.html" title="sublime text 3配置c/c++编译环境" target="_blank">sublime text 3配置c/c++编译环境</a></li>
<li><a rel="bookmark" href="http://www.yalewoo.com/gdb-2.html" title="gdb调试新手入门(一)" target="_blank">gdb调试新手入门(一)</a></li>
<li><a rel="bookmark" href="http://www.yalewoo.com/ios_multiple-threads-request-resources-from-the-same-api-to-avoid-duplicate-requests.html" title="iOS多个线程请求同一API的资源,避免重复请求" target="_blank">iOS多个线程请求同一API的资源,避免重复请求</a></li>
</ul>
</div>
<div class="comments-template">
<div id="comments" class="comments-area">
<div id="respond" class="comment-respond">
<h3 id="reply-title" class="comment-reply-title">我要评论 <small><a rel="nofollow" id="cancel-comment-reply-link" href="/cpp_notes_2_input_and_output_library.html#respond" style="display:none;">取消回复</a></small></h3> <form action="http://www.yalewoo.com/wp-comments-post.php" method="post" id="commentform" class="comment-form">
<p class="comment-form-comment"><textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" aria-required="true" required="required"></textarea></p>
<script type="text/javascript" src="http://www.yalewoo.com/wp-content/themes/YLW3_lite/js/show_smilies.js"></script>
<div class="ylw_comment_toolbar">
<a class="button-insert-smilies" id="button-insert-smilies" title="插入表情" onClick="show_smilies();"></a>
</div>
<div class="ylw_smilies_box_wrapper">
<div class="ylw_smilies_box" id="ylw_smilies_box">
</div>
</div><div class="comment-name-email-url"><p class="comment-form-author"><label for="author">姓名</label><input id="author" name="author" type="text" value="" size="30" /> </p>
<p class="comment-form-email"><label for="email">邮箱</label><input id="email" name="email" type="text" value="" size="30" /> </p>
<p class="comment-form-url"><label for="url">网址</label><input id="url" name="url" type="text" value="" size="30" /></p></div><div class='comment_yzm'>验证码*: 3 + 7 = <input type='text' name='sum' class='math_textfield' required='required' value='' size='25' tabindex='4'><input type='hidden' name='num1' value='3'><input type='hidden' name='num2' value='7'></div><div class="comment-right"><div class="comment-submit-button">
<p class="form-submit"><input name="submit" type="submit" id="submit" class="submit" value="发表评论" /> <input type='hidden' name='comment_post_ID' value='2808' id='comment_post_ID' />
<input type='hidden' name='comment_parent' id='comment_parent' value='0' />
</p></div><div class="ylw_comment_notifyme"><input type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="checked" style="margin-left:20px;" /><label for="comment_mail_notify">有人回复时邮件通知我</label></div></div></div><div class="clear"></div> </form>
</div><!-- #respond -->
</div><!-- .comments-area -->
</div>
</section>
</div>
<footer id="footer">
Copyright © <a title="雅乐网" href="http://www.yalewoo.com">雅乐网</a> /<a title="自豪地采用WordPress" href="https://cn.wordpress.org" target="_blank">WordPress</a> / <a title="YLW3.0主题" href="http://www.yalewoo.com/ylw3.html" target="_blank">YLW3.0</a> / <a title="老薛主机" href="https://my.laoxuehost.net/aff.php?aff=2518" target="_blank">老薛主机</a> / <a title="七牛云存储" href="https://portal.qiniu.com/signup?code=3li1yeb2ph1ea" target="_black">七牛云存储</a>
<div id="footer_menu">
<div class="menu-%e5%ba%95%e9%83%a8-container"><ul id="menu-%e5%ba%95%e9%83%a8" class="menu"><li id="menu-item-655" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-655"><a target="_blank" href="http://www.yalewoo.com/sitemap.xml">站点地图</a></li>
</ul></div> </div>
<div id="cnzztongji">
<script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1252889774'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s5.cnzz.com/stat.php%3Fid%3D1252889774%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));</script>
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "//hm.baidu.com/hm.js?e937132d7f7e86dfb5300ce1ab2c25f7";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
</div>
</footer>
<script type="text/javascript" src="http://www.yalewoo.com/wp-content/themes/YLW3_lite/js/backtop.js"></script>
<script type='text/javascript' src='http://www.yalewoo.com/wp-includes/js/comment-reply.min.js?ver=4.7.9'></script>
</body>
</html>
<!-- Dynamic page generated in 1.275 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2018-02-05 13:55:18 -->
<!-- Compression = gzip -->
<!-- super cache -->