<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8409310322589078134</id><updated>2024-09-04T20:36:35.784-07:00</updated><title type='text'>TECH-THINKER</title><subtitle type='html'>Top Coder! Keep moving forward! accomplish my Millionaire dream!</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>46</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-1029697690925523926</id><published>2015-01-26T02:20:00.001-08:00</published><updated>2015-01-26T02:20:52.793-08:00</updated><title type='text'>Angelo Yin (阴平)&#39;s invitation is awaiting your response</title><content type='html'>&lt;span style=&quot;display: none !important;font-size: 1px;visibility: hidden;opacity: 0;color: transparent;height: 0;width: 0;mso-hide: all;&quot;&gt;&lt;/span&gt;    &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; border-collapse:collapse; width:100% !important; font-family:Helvetica,Arial,sans-serif; margin:0; padding:0;&quot; width=&quot;100%&quot; bgcolor=&quot;#DFDFDF&quot;&gt;     &lt;tbody&gt;      &lt;tr&gt;       &lt;td colspan=&quot;3&quot;&gt;        &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;1&quot;&gt;         &lt;tbody&gt;          &lt;tr&gt;           &lt;td&gt;            &lt;div style=&quot;height:5px;font-size:5px;line-height:5px;&quot;&gt;             &amp;nbsp;            &lt;/div&gt;&lt;/td&gt;          &lt;/tr&gt;         &lt;/tbody&gt;        &lt;/table&gt;&lt;/td&gt;      &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;        &lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; border=&quot;0&quot; align=&quot;center&quot; width=&quot;100%&quot; style=&quot;table-layout: fixed;&quot;&gt;         &lt;tbody&gt;          &lt;tr&gt;           &lt;td align=&quot;center&quot;&gt;            &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; min-width:290px;&quot; width=&quot;600&quot; class=&quot;responsive&quot;&gt;             &lt;tbody&gt;              &lt;tr&gt;               &lt;td style=&quot;font-family:Helvetica,Arial,sans-serif;&quot;&gt;                &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                 &lt;tbody&gt;                  &lt;tr&gt;                   &lt;td&gt;                    &lt;div style=&quot;height:8px;font-size:8px;line-height:8px&quot;&gt;                     &amp;nbsp;                    &lt;/div&gt;&lt;/td&gt;                  &lt;/tr&gt;                 &lt;/tbody&gt;                &lt;/table&gt;                &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif;&quot; width=&quot;100%&quot; bgcolor=&quot;#DDDDDD&quot;&gt;                 &lt;tbody&gt;                  &lt;tr&gt;                   &lt;td align=&quot;left&quot; valign=&quot;middle&quot; width=&quot;95&quot; height=&quot;21&quot; id=&quot;base-header-logo&quot;&gt;&lt;a style=&quot;text-decoration:none;cursor:pointer;border:none;display:block;height:21px;width:100%;&quot; href=&quot;https://www.linkedin.com/blink?simpleRedirect=ej5vs7xBnTpKqjRHpioPcj0Op6VFrmlObmlJrSxvrStLr2RybmRSrCBvrmRLoORIrmkZpSVFqSdxsDgCs6RKfngCpz8Je7tGpD1AdmAJpmhPs6pRbjRBfP9SbSkLrmZzbCVFp6lHrCBIbDtTtOYLeDdMt7hE&amp;amp;msgID=I5962787408821907456_500&amp;amp;markAsRead=&quot;&gt;&lt;img src=&quot;http://s.c.lnkd.licdn.com/scds/common/u/images/email/logos/logo_linkedin_tm_email_95x21_v1.png&quot; width=&quot;95&quot; height=&quot;21&quot; alt=&quot;LinkedIn&quot; style=&quot;border:none;text-decoration:none;&quot; /&gt;&lt;/a&gt;&lt;/td&gt;                  &lt;/tr&gt;                 &lt;/tbody&gt;                &lt;/table&gt;                &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                 &lt;tbody&gt;                  &lt;tr&gt;                   &lt;td&gt;                    &lt;div style=&quot;height:8px;font-size:8px;line-height:8px&quot;&gt;                     &amp;nbsp;                    &lt;/div&gt;&lt;/td&gt;                  &lt;/tr&gt;                 &lt;/tbody&gt;                &lt;/table&gt;                &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif;&quot; width=&quot;100%&quot; bgcolor=&quot;#333333&quot;&gt;                 &lt;tbody&gt;                  &lt;tr&gt;                   &lt;td width=&quot;20&quot; class=&quot;responsive-spacer&quot;&gt;                    &lt;table width=&quot;20&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:0px;font-size:0px;line-height:0px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                   &lt;td width=&quot;100%&quot;&gt;                    &lt;table width=&quot;560&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; border=&quot;0&quot; class=&quot;header-spacer&quot; style=&quot;table-layout: fixed;&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td width=&quot;560&quot;&gt;                        &lt;div style=&quot;height:12px;font-size:12px;line-height:12px;width:560px;&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                   &lt;td width=&quot;20&quot; class=&quot;responsive-spacer&quot;&gt;                    &lt;table width=&quot;20&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:0px;font-size:0px;line-height:0px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                  &lt;/tr&gt;                 &lt;/tbody&gt;                &lt;/table&gt;                &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;100%&quot; bgcolor=&quot;#FFFFFF&quot;&gt;                 &lt;tbody&gt;                  &lt;tr&gt;                   &lt;td width=&quot;20&quot; class=&quot;res-width10&quot;&gt;                    &lt;table width=&quot;20px&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer res-width10&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:0px;font-size:0px;line-height:0px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                   &lt;td style=&quot;color: #333333; font-family: Helvetica,Arial,sans-serif; font-size: 15px; line-height: 18px;&quot; align=&quot;left&quot;&gt;                    &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer res-height10&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:20px;font-size:20px;line-height:20px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;                    &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;100%&quot; class=&quot;responsive&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td style=&quot;font-family:Helvetica,Arial,sans-serif;color:#333333;&quot;&gt;&lt;b&gt;Angelo Yin (阴平)&lt;/b&gt; would like to connect on LinkedIn. How would you like to respond?&lt;/td&gt;                      &lt;/tr&gt;                      &lt;tr&gt;                       &lt;td style=&quot;border-bottom-color: #E5E5E5;border-bottom-width: 1px; border-bottom-style: solid;&quot;&gt;                        &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                         &lt;tbody&gt;                          &lt;tr&gt;                           &lt;td&gt;                            &lt;div style=&quot;height:20px;font-size:20px;line-height:20px&quot;&gt;                             &amp;nbsp;                            &lt;/div&gt;&lt;/td&gt;                          &lt;/tr&gt;                         &lt;/tbody&gt;                        &lt;/table&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;                    &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:20px;font-size:20px;line-height:20px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;                    &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;100%&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td width=&quot;150&quot; valign=&quot;top&quot; style=&quot;vertical-align: top;&quot; class=&quot;res-img100&quot;&gt;&lt;a href=&quot;https://www.linkedin.com/blink?simpleRedirect=3gSc3oRc38Ne3cTcPsUdP8SejkZh4BKrSBQonhFtCVF9A9zjCZBmn5afnBBiShBsC5EsOoVclZMu6lvtCVFfmJB9D9Bp6VFrmlObnhMpmdzoiRLt6ZEs2RybmtSrCBvrmRLoORIrmkZpSVFqSdxsDgCpnhFtCV9pSlipn9Mfm4Cpz8Je7tGpD1AdmAJpmhPs6pRbjRBfP9SbSkLrmZzbCVFp6lHrCBIbDtTtOYLeDdMt7hE&amp;amp;msgID=I5962787408821907456_500&amp;amp;markAsRead=&quot; style=&quot;text-decoration:none;cursor:pointer;&quot;&gt;&lt;img alt=&quot;Angelo Yin (阴平)&quot; src=&quot;http://m.c.lnkd.licdn.com/mpr/mpr/shrink_150_150/p/6/005/00b/218/240b12c.jpg&quot; width=&quot;150&quot; height=&quot;150&quot; border=&quot;0&quot; class=&quot;res-img100&quot; style=&quot;border:none;text-decoration:none;outline:hidden;display:block;&quot; /&gt;&lt;/a&gt;&lt;/td&gt;                       &lt;td width=&quot;20&quot;&gt;                        &lt;table width=&quot;20&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                         &lt;tbody&gt;                          &lt;tr&gt;                           &lt;td&gt;                            &lt;div style=&quot;height:0px;font-size:0px;line-height:0px&quot;&gt;                             &amp;nbsp;                            &lt;/div&gt;&lt;/td&gt;                          &lt;/tr&gt;                         &lt;/tbody&gt;                        &lt;/table&gt;&lt;/td&gt;                       &lt;td style=&quot;vertical-align: top; font-family: Helvetica,Arial,sans-serif;&quot; width=&quot;100%&quot;&gt;                        &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;100%&quot;&gt;                         &lt;tbody&gt;                          &lt;tr&gt;                           &lt;td valign=&quot;top&quot;&gt;&lt;a href=&quot;https://www.linkedin.com/blink?simpleRedirect=0Qdz0Sdj0OcjwPdPcTe3sOdzARfkh9rCZFt65QqnpKqip2oQVLplBNizRVpkJApn9xq7cCej5vs7xBnTpKqjRHpipOpmhKqmRBsyRQs6lzoS4JpmRxryRybmtSrCBvrmRLoORIrmkZpSVFqSdxsDgCpnhFtCV9pSlipn9Mfm4Cpz8Je7tGpD1AdmAJpmhPs6pRbjRBfP9SbSkLrmZzbCVFp6lHrCBIbDtTtOYLeDdMt7hE&amp;amp;msgID=I5962787408821907456_500&amp;amp;markAsRead=&quot; style=&quot;font-size: 20px; font-weight: bold; color:#000000;text-decoration:none;&quot;&gt;Angelo Yin (阴平)&lt;/a&gt;&lt;/td&gt;                          &lt;/tr&gt;                          &lt;tr&gt;                           &lt;td&gt;                            &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                             &lt;tbody&gt;                              &lt;tr&gt;                               &lt;td&gt;                                &lt;div style=&quot;height:3px;font-size:3px;line-height:3px&quot;&gt;                                 &amp;nbsp;                                &lt;/div&gt;&lt;/td&gt;                              &lt;/tr&gt;                             &lt;/tbody&gt;                            &lt;/table&gt;&lt;/td&gt;                          &lt;/tr&gt;                          &lt;tr&gt;                           &lt;td style=&quot;color: #666666; font-size: 15px;&quot; class=&quot;res-font16&quot;&gt;Senior Software Developer at Schlumberger&lt;/td&gt;                          &lt;/tr&gt;                          &lt;tr&gt;                           &lt;td&gt;                            &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                             &lt;tbody&gt;                              &lt;tr&gt;                               &lt;td&gt;                                &lt;div style=&quot;height:10px;font-size:10px;line-height:10px&quot;&gt;                                 &amp;nbsp;                                &lt;/div&gt;&lt;/td&gt;                              &lt;/tr&gt;                             &lt;/tbody&gt;                            &lt;/table&gt;&lt;/td&gt;                          &lt;/tr&gt;                          &lt;tr&gt;                           &lt;td&gt;                            &lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; align=&quot;left&quot;&gt;                             &lt;tbody&gt;                              &lt;tr&gt;                               &lt;td align=&quot;center&quot; height=&quot;30&quot; valign=&quot;middle&quot; bgcolor=&quot;#287BBC&quot; background=&quot;http://s.c.lnkd.licdn.com/scds/common/u/img/email/bg_btn_katy_blue_medium.png&quot; style=&quot;background:url(http://s.c.lnkd.licdn.com/scds/common/u/img/email/bg_btn_katy_blue_medium.png) repeat-x scroll bottom #287BBC;background-color:#287BBC;border:1px solid #1B5480;-moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px; cursor: pointer;&quot;&gt;                                &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;100%&quot; bgcolor=&quot;transparent&quot;&gt;                                 &lt;tbody&gt;                                  &lt;tr&gt;                                   &lt;td width=&quot;13&quot;&gt;                                    &lt;table width=&quot;13px&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                                     &lt;tbody&gt;                                      &lt;tr&gt;                                       &lt;td&gt;                                        &lt;div style=&quot;height:0px;font-size:0px;line-height:0px&quot;&gt;                                         &amp;nbsp;                                        &lt;/div&gt;&lt;/td&gt;                                      &lt;/tr&gt;                                     &lt;/tbody&gt;                                    &lt;/table&gt;&lt;/td&gt;                                   &lt;td&gt;&lt;a href=&quot;https://www.linkedin.com/blink?simpleRedirect=d3oMdzkMcz4UcPsPdPwTczoVdjR4imVLqnhxt6BSrCACgCderSlpskEZumlbp6lOomxP9zANnT1UplZSrCAZqSkCsClArCBJpn8Jt71BoSdxbmVLt7hRoyRybmtSrCBvrmRLoORIrmkZpSVFqSdxsDgCpnhFtCV9pSlipn9Mfm4Cpz8Je7tGpD1AdmAJpmhPs6pRbjRBfP9SbSkLrmZzbCVFp6lHrCBIbDtTtOYLeDdMt7hE&amp;amp;msgID=I5962787408821907456_500&amp;amp;markAsRead=&quot; style=&quot;text-decoration:none; font-size: 13px;font-weight: bold;color: white;white-space: nowrap;display: block;&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;font-size: 13px;font-weight: bold;color: white;white-space: nowrap;display: block;&quot;&gt;Confirm you know Angelo&lt;/span&gt;&lt;/a&gt;&lt;/td&gt;                                   &lt;td width=&quot;13&quot;&gt;                                    &lt;table width=&quot;13px&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                                     &lt;tbody&gt;                                      &lt;tr&gt;                                       &lt;td&gt;                                        &lt;div style=&quot;height:0px;font-size:0px;line-height:0px&quot;&gt;                                         &amp;nbsp;                                        &lt;/div&gt;&lt;/td&gt;                                      &lt;/tr&gt;                                     &lt;/tbody&gt;                                    &lt;/table&gt;&lt;/td&gt;                                  &lt;/tr&gt;                                 &lt;/tbody&gt;                                &lt;/table&gt;&lt;/td&gt;                              &lt;/tr&gt;                             &lt;/tbody&gt;                            &lt;/table&gt;&lt;/td&gt;                          &lt;/tr&gt;                         &lt;/tbody&gt;                        &lt;/table&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;                    &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:20px;font-size:20px;line-height:20px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                   &lt;td width=&quot;20&quot; class=&quot;res-width10&quot;&gt;                    &lt;table width=&quot;20px&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer res-width10&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:0px;font-size:0px;line-height:0px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                  &lt;/tr&gt;                 &lt;/tbody&gt;                &lt;/table&gt;&lt;/td&gt;              &lt;/tr&gt;             &lt;/tbody&gt;            &lt;/table&gt;            &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;600&quot; class=&quot;responsive&quot;&gt;             &lt;tbody&gt;              &lt;tr&gt;               &lt;td align=&quot;left&quot;&gt;                &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;100%&quot; class=&quot;responsive&quot;&gt;                 &lt;tbody&gt;                  &lt;tr&gt;                   &lt;td&gt;                    &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:10px;font-size:10px;line-height:10px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                  &lt;/tr&gt;                  &lt;tr&gt;                   &lt;td align=&quot;left&quot;&gt;                    &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; font-size:11px; font-family:Helvetica,Arial,sans-serif; color:#999999;&quot; width=&quot;100%&quot; class=&quot;responsive res-font10&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;You received an invitation to connect. LinkedIn will use your email address to make suggestions to our members in features like People You May Know. &lt;a style=&quot;text-decoration:none;color:#0077B5;&quot; href=&quot;https://www.linkedin.com/blink?simpleRedirect=6oObjxTqCpMp3lFbmlAsT1CtiQZp6BB9CdMlQQPuCR6h3tMi5BvqAV5oPphuzBHdnxRrCUJlTlyqjtUtntmmmxllz94hORmoTxGp65ds4t6p5FMplZMs5xqbm8NoyRalnAQkDhnq7tIsRl1gk5TnRgSkAdqlCxnjAthgjRAqmZI9zANnT1UplZSrCAZqSkCkjoPp4l7q5p6sCR6kk4ZrClHrRhAqmQCrDlIfngCpz8Je7tGpD1AdmAJpmhPs6pRbjRBfP9SbSkLrmZzbCVFp6lHrCBIbDtTtOYLeDdMt7hE&amp;amp;msgID=I5962787408821907456_500&amp;amp;markAsRead=&quot;&gt;Unsubscribe&lt;/a&gt;&lt;/td&gt;                      &lt;/tr&gt;                      &lt;tr&gt;                       &lt;td&gt;&lt;/td&gt;                      &lt;/tr&gt;                      &lt;tr&gt;                       &lt;td&gt;If you need assistance or have questions, please contact &lt;a style=&quot;color:#0077B5;text-decoration:none;&quot; target=&quot;_blank&quot; href=&quot;https://www.linkedin.com/blink?simpleRedirect=3ANnT1UplZSrCAZqSkCr79lpmdFtD9BkT9BrmZQsTlzfm4Cpz8Je7tGpD1AdmAJpmhPs6pRbjRBfP9SbSkLrmZzbCVFp6lHrCBIbDtTtOYLeDdMt7hE&amp;amp;msgID=I5962787408821907456_500&amp;amp;markAsRead=&quot;&gt;LinkedIn Customer Service&lt;/a&gt;.&lt;/td&gt;                      &lt;/tr&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                         &lt;tbody&gt;                          &lt;tr&gt;                           &lt;td&gt;                            &lt;div style=&quot;height:10px;font-size:10px;line-height:10px&quot;&gt;                             &amp;nbsp;                            &lt;/div&gt;&lt;/td&gt;                          &lt;/tr&gt;                         &lt;/tbody&gt;                        &lt;/table&gt;&lt;/td&gt;                      &lt;/tr&gt;                      &lt;tr&gt;                       &lt;td&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                  &lt;/tr&gt;                  &lt;tr&gt;                   &lt;td&gt;                    &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:20px;font-size:20px;line-height:20px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                  &lt;/tr&gt;                 &lt;/tbody&gt;                &lt;/table&gt;&lt;/td&gt;              &lt;/tr&gt;             &lt;/tbody&gt;            &lt;/table&gt;&lt;/td&gt;          &lt;/tr&gt;         &lt;/tbody&gt;        &lt;/table&gt;&lt;/td&gt;      &lt;/tr&gt;     &lt;/tbody&gt;    &lt;/table&gt;    &lt;img src=&quot;http://www.linkedin.com/emimp/-ufpsde-i5dpfjw8-2f.gif&quot; style=&quot;width:1px; height:1px;&quot; /&gt;   </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/1029697690925523926/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/1029697690925523926?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/1029697690925523926'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/1029697690925523926'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2015/01/angelo-yin-s-invitation-is-awaiting_26.html' title='Angelo Yin (阴平)&#39;s invitation is awaiting your response'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-3948879825984704393</id><published>2015-01-20T01:15:00.001-08:00</published><updated>2015-01-20T01:15:20.519-08:00</updated><title type='text'>Angelo Yin (阴平)&#39;s invitation is awaiting your response</title><content type='html'>&lt;span style=&quot;display: none !important;font-size: 1px;visibility: hidden;opacity: 0;color: transparent;height: 0;width: 0;mso-hide: all;&quot;&gt;&lt;/span&gt;    &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; border-collapse:collapse; width:100% !important; font-family:Helvetica,Arial,sans-serif; margin:0; padding:0;&quot; width=&quot;100%&quot; bgcolor=&quot;#DFDFDF&quot;&gt;     &lt;tbody&gt;      &lt;tr&gt;       &lt;td colspan=&quot;3&quot;&gt;        &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;1&quot;&gt;         &lt;tbody&gt;          &lt;tr&gt;           &lt;td&gt;            &lt;div style=&quot;height:5px;font-size:5px;line-height:5px;&quot;&gt;             &amp;nbsp;            &lt;/div&gt;&lt;/td&gt;          &lt;/tr&gt;         &lt;/tbody&gt;        &lt;/table&gt;&lt;/td&gt;      &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;        &lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; border=&quot;0&quot; align=&quot;center&quot; width=&quot;100%&quot; style=&quot;table-layout: fixed;&quot;&gt;         &lt;tbody&gt;          &lt;tr&gt;           &lt;td align=&quot;center&quot;&gt;            &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; min-width:290px;&quot; width=&quot;600&quot; class=&quot;responsive&quot;&gt;             &lt;tbody&gt;              &lt;tr&gt;               &lt;td style=&quot;font-family:Helvetica,Arial,sans-serif;&quot;&gt;                &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                 &lt;tbody&gt;                  &lt;tr&gt;                   &lt;td&gt;                    &lt;div style=&quot;height:8px;font-size:8px;line-height:8px&quot;&gt;                     &amp;nbsp;                    &lt;/div&gt;&lt;/td&gt;                  &lt;/tr&gt;                 &lt;/tbody&gt;                &lt;/table&gt;                &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif;&quot; width=&quot;100%&quot; bgcolor=&quot;#DDDDDD&quot;&gt;                 &lt;tbody&gt;                  &lt;tr&gt;                   &lt;td align=&quot;left&quot; valign=&quot;middle&quot; width=&quot;95&quot; height=&quot;21&quot; id=&quot;base-header-logo&quot;&gt;&lt;a style=&quot;text-decoration:none;cursor:pointer;border:none;display:block;height:21px;width:100%;&quot; href=&quot;https://www.linkedin.com/blink?simpleRedirect=ej5vs7xBnTpKqjRHpioPcj0Op6VFrmlObmlJrSxvrStLr2RybmRSrCBvrmRLoORIrmkZpSVFqSdxsDgCs6RKfngCtPkJsjdzpP8RdmAJpmhPs6pRbjRBfP9SbSkLrmZzbCVFp6lHrCBIbDtTtOYLeDdMt7hE&amp;amp;msgID=I5962787408821907456_500&amp;amp;markAsRead=&quot;&gt;&lt;img src=&quot;http://s.c.lnkd.licdn.com/scds/common/u/images/email/logos/logo_linkedin_tm_email_95x21_v1.png&quot; width=&quot;95&quot; height=&quot;21&quot; alt=&quot;LinkedIn&quot; style=&quot;border:none;text-decoration:none;&quot; /&gt;&lt;/a&gt;&lt;/td&gt;                  &lt;/tr&gt;                 &lt;/tbody&gt;                &lt;/table&gt;                &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                 &lt;tbody&gt;                  &lt;tr&gt;                   &lt;td&gt;                    &lt;div style=&quot;height:8px;font-size:8px;line-height:8px&quot;&gt;                     &amp;nbsp;                    &lt;/div&gt;&lt;/td&gt;                  &lt;/tr&gt;                 &lt;/tbody&gt;                &lt;/table&gt;                &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif;&quot; width=&quot;100%&quot; bgcolor=&quot;#333333&quot;&gt;                 &lt;tbody&gt;                  &lt;tr&gt;                   &lt;td width=&quot;20&quot; class=&quot;responsive-spacer&quot;&gt;                    &lt;table width=&quot;20&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:0px;font-size:0px;line-height:0px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                   &lt;td width=&quot;100%&quot;&gt;                    &lt;table width=&quot;560&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; border=&quot;0&quot; class=&quot;header-spacer&quot; style=&quot;table-layout: fixed;&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td width=&quot;560&quot;&gt;                        &lt;div style=&quot;height:12px;font-size:12px;line-height:12px;width:560px;&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                   &lt;td width=&quot;20&quot; class=&quot;responsive-spacer&quot;&gt;                    &lt;table width=&quot;20&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:0px;font-size:0px;line-height:0px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                  &lt;/tr&gt;                 &lt;/tbody&gt;                &lt;/table&gt;                &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;100%&quot; bgcolor=&quot;#FFFFFF&quot;&gt;                 &lt;tbody&gt;                  &lt;tr&gt;                   &lt;td width=&quot;20&quot; class=&quot;res-width10&quot;&gt;                    &lt;table width=&quot;20px&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer res-width10&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:0px;font-size:0px;line-height:0px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                   &lt;td style=&quot;color: #333333; font-family: Helvetica,Arial,sans-serif; font-size: 15px; line-height: 18px;&quot; align=&quot;left&quot;&gt;                    &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer res-height10&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:20px;font-size:20px;line-height:20px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;                    &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;100%&quot; class=&quot;responsive&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td style=&quot;font-family:Helvetica,Arial,sans-serif;color:#333333;&quot;&gt;&lt;b&gt;Angelo Yin (阴平)&lt;/b&gt; would like to connect on LinkedIn. How would you like to respond?&lt;/td&gt;                      &lt;/tr&gt;                      &lt;tr&gt;                       &lt;td style=&quot;border-bottom-color: #E5E5E5;border-bottom-width: 1px; border-bottom-style: solid;&quot;&gt;                        &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                         &lt;tbody&gt;                          &lt;tr&gt;                           &lt;td&gt;                            &lt;div style=&quot;height:20px;font-size:20px;line-height:20px&quot;&gt;                             &amp;nbsp;                            &lt;/div&gt;&lt;/td&gt;                          &lt;/tr&gt;                         &lt;/tbody&gt;                        &lt;/table&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;                    &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:20px;font-size:20px;line-height:20px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;                    &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;100%&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td width=&quot;150&quot; valign=&quot;top&quot; style=&quot;vertical-align: top;&quot; class=&quot;res-img100&quot;&gt;&lt;a href=&quot;https://www.linkedin.com/blink?simpleRedirect=3gSc3oRc38Ne3cTcPsUdP8SejkZh4BKrSBQonhFtCVF9A9zjCZBmn5afnBBiShBsC5EsOoVclZMu6lvtCVFfmJB9D9Bp6VFrmlObnhMpmdzoiRLt6ZEs2RybmtSrCBvrmRLoORIrmkZpSVFqSdxsDgCpnhFtCV9pSlipn9Mfm4CtPkJsjdzpP8RdmAJpmhPs6pRbjRBfP9SbSkLrmZzbCVFp6lHrCBIbDtTtOYLeDdMt7hE&amp;amp;msgID=I5962787408821907456_500&amp;amp;markAsRead=&quot; style=&quot;text-decoration:none;cursor:pointer;&quot;&gt;&lt;img alt=&quot;Angelo Yin (阴平)&quot; src=&quot;http://m.c.lnkd.licdn.com/mpr/mpr/shrink_150_150/p/6/005/00b/218/240b12c.jpg&quot; width=&quot;150&quot; height=&quot;150&quot; border=&quot;0&quot; class=&quot;res-img100&quot; style=&quot;border:none;text-decoration:none;outline:hidden;display:block;&quot; /&gt;&lt;/a&gt;&lt;/td&gt;                       &lt;td width=&quot;20&quot;&gt;                        &lt;table width=&quot;20&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                         &lt;tbody&gt;                          &lt;tr&gt;                           &lt;td&gt;                            &lt;div style=&quot;height:0px;font-size:0px;line-height:0px&quot;&gt;                             &amp;nbsp;                            &lt;/div&gt;&lt;/td&gt;                          &lt;/tr&gt;                         &lt;/tbody&gt;                        &lt;/table&gt;&lt;/td&gt;                       &lt;td style=&quot;vertical-align: top; font-family: Helvetica,Arial,sans-serif;&quot; width=&quot;100%&quot;&gt;                        &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;100%&quot;&gt;                         &lt;tbody&gt;                          &lt;tr&gt;                           &lt;td valign=&quot;top&quot;&gt;&lt;a href=&quot;https://www.linkedin.com/blink?simpleRedirect=0Qdz0Sdj0OcjwPdPcTe3sOdzARfkh9rCZFt65QqnpKqip2oQVLplBNizRVpkJApn9xq7cCej5vs7xBnTpKqjRHpipOpmhKqmRBsyRQs6lzoS4JpmRxryRybmtSrCBvrmRLoORIrmkZpSVFqSdxsDgCpnhFtCV9pSlipn9Mfm4CtPkJsjdzpP8RdmAJpmhPs6pRbjRBfP9SbSkLrmZzbCVFp6lHrCBIbDtTtOYLeDdMt7hE&amp;amp;msgID=I5962787408821907456_500&amp;amp;markAsRead=&quot; style=&quot;font-size: 20px; font-weight: bold; color:#000000;text-decoration:none;&quot;&gt;Angelo Yin (阴平)&lt;/a&gt;&lt;/td&gt;                          &lt;/tr&gt;                          &lt;tr&gt;                           &lt;td&gt;                            &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                             &lt;tbody&gt;                              &lt;tr&gt;                               &lt;td&gt;                                &lt;div style=&quot;height:3px;font-size:3px;line-height:3px&quot;&gt;                                 &amp;nbsp;                                &lt;/div&gt;&lt;/td&gt;                              &lt;/tr&gt;                             &lt;/tbody&gt;                            &lt;/table&gt;&lt;/td&gt;                          &lt;/tr&gt;                          &lt;tr&gt;                           &lt;td style=&quot;color: #666666; font-size: 15px;&quot; class=&quot;res-font16&quot;&gt;Senior Software Developer at Schlumberger&lt;/td&gt;                          &lt;/tr&gt;                          &lt;tr&gt;                           &lt;td&gt;                            &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                             &lt;tbody&gt;                              &lt;tr&gt;                               &lt;td&gt;                                &lt;div style=&quot;height:10px;font-size:10px;line-height:10px&quot;&gt;                                 &amp;nbsp;                                &lt;/div&gt;&lt;/td&gt;                              &lt;/tr&gt;                             &lt;/tbody&gt;                            &lt;/table&gt;&lt;/td&gt;                          &lt;/tr&gt;                          &lt;tr&gt;                           &lt;td&gt;                            &lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; align=&quot;left&quot;&gt;                             &lt;tbody&gt;                              &lt;tr&gt;                               &lt;td align=&quot;center&quot; height=&quot;30&quot; valign=&quot;middle&quot; bgcolor=&quot;#287BBC&quot; background=&quot;http://s.c.lnkd.licdn.com/scds/common/u/img/email/bg_btn_katy_blue_medium.png&quot; style=&quot;background:url(http://s.c.lnkd.licdn.com/scds/common/u/img/email/bg_btn_katy_blue_medium.png) repeat-x scroll bottom #287BBC;background-color:#287BBC;border:1px solid #1B5480;-moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px; cursor: pointer;&quot;&gt;                                &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;100%&quot; bgcolor=&quot;transparent&quot;&gt;                                 &lt;tbody&gt;                                  &lt;tr&gt;                                   &lt;td width=&quot;13&quot;&gt;                                    &lt;table width=&quot;13px&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                                     &lt;tbody&gt;                                      &lt;tr&gt;                                       &lt;td&gt;                                        &lt;div style=&quot;height:0px;font-size:0px;line-height:0px&quot;&gt;                                         &amp;nbsp;                                        &lt;/div&gt;&lt;/td&gt;                                      &lt;/tr&gt;                                     &lt;/tbody&gt;                                    &lt;/table&gt;&lt;/td&gt;                                   &lt;td&gt;&lt;a href=&quot;https://www.linkedin.com/blink?simpleRedirect=d3oMdzkMcz4UcPsPdPwTczoVdjR4imVLqnhxt6BSrCACgCderSlpskEZumlbp6lOomxP9zANnT1UplZSrCAZqSkCsClArCBJpn8Jt71BoSdxbmVLt7hRoyRybmtSrCBvrmRLoORIrmkZpSVFqSdxsDgCpnhFtCV9pSlipn9Mfm4CtPkJsjdzpP8RdmAJpmhPs6pRbjRBfP9SbSkLrmZzbCVFp6lHrCBIbDtTtOYLeDdMt7hE&amp;amp;msgID=I5962787408821907456_500&amp;amp;markAsRead=&quot; style=&quot;text-decoration:none; font-size: 13px;font-weight: bold;color: white;white-space: nowrap;display: block;&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;font-size: 13px;font-weight: bold;color: white;white-space: nowrap;display: block;&quot;&gt;Confirm you know Angelo&lt;/span&gt;&lt;/a&gt;&lt;/td&gt;                                   &lt;td width=&quot;13&quot;&gt;                                    &lt;table width=&quot;13px&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                                     &lt;tbody&gt;                                      &lt;tr&gt;                                       &lt;td&gt;                                        &lt;div style=&quot;height:0px;font-size:0px;line-height:0px&quot;&gt;                                         &amp;nbsp;                                        &lt;/div&gt;&lt;/td&gt;                                      &lt;/tr&gt;                                     &lt;/tbody&gt;                                    &lt;/table&gt;&lt;/td&gt;                                  &lt;/tr&gt;                                 &lt;/tbody&gt;                                &lt;/table&gt;&lt;/td&gt;                              &lt;/tr&gt;                             &lt;/tbody&gt;                            &lt;/table&gt;&lt;/td&gt;                          &lt;/tr&gt;                         &lt;/tbody&gt;                        &lt;/table&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;                    &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:20px;font-size:20px;line-height:20px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                   &lt;td width=&quot;20&quot; class=&quot;res-width10&quot;&gt;                    &lt;table width=&quot;20px&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer res-width10&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:0px;font-size:0px;line-height:0px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                  &lt;/tr&gt;                 &lt;/tbody&gt;                &lt;/table&gt;&lt;/td&gt;              &lt;/tr&gt;             &lt;/tbody&gt;            &lt;/table&gt;            &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;600&quot; class=&quot;responsive&quot;&gt;             &lt;tbody&gt;              &lt;tr&gt;               &lt;td align=&quot;left&quot;&gt;                &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;100%&quot; class=&quot;responsive&quot;&gt;                 &lt;tbody&gt;                  &lt;tr&gt;                   &lt;td&gt;                    &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:10px;font-size:10px;line-height:10px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                  &lt;/tr&gt;                  &lt;tr&gt;                   &lt;td align=&quot;left&quot;&gt;                    &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; font-size:11px; font-family:Helvetica,Arial,sans-serif; color:#999999;&quot; width=&quot;100%&quot; class=&quot;responsive res-font10&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;You received an invitation to connect. LinkedIn will use your email address to make suggestions to our members in features like People You May Know. &lt;a style=&quot;text-decoration:none;color:#0077B5;&quot; href=&quot;https://www.linkedin.com/blink?simpleRedirect=7sRbn4PoSsOdjlFbmlAsT1CtiQZp6BB9zxAiltip39Hu491h35od6EPkTxbhTFNiA5BjBxQt31GlS8Jon1Qs3kScAtGiCZedRsJdltgimh9qCxvmjAQtBF4u7xhi5h2pCBek7pTbmV7sRl1gk5hokBvqyR2pmdNj4xhgjRAqmZI9zANnT1UplZSrCAZqSkCkjoPp4l7q5p6sCR6kk4ZrClHrRhAqmQCrDlIfngCtPkJsjdzpP8RdmAJpmhPs6pRbjRBfP9SbSkLrmZzbCVFp6lHrCBIbDtTtOYLeDdMt7hE&amp;amp;msgID=I5962787408821907456_500&amp;amp;markAsRead=&quot;&gt;Unsubscribe&lt;/a&gt;&lt;/td&gt;                      &lt;/tr&gt;                      &lt;tr&gt;                       &lt;td&gt;&lt;/td&gt;                      &lt;/tr&gt;                      &lt;tr&gt;                       &lt;td&gt;If you need assistance or have questions, please contact &lt;a style=&quot;color:#0077B5;text-decoration:none;&quot; target=&quot;_blank&quot; href=&quot;https://www.linkedin.com/blink?simpleRedirect=3ANnT1UplZSrCAZqSkCr79lpmdFtD9BkT9BrmZQsTlzfm4CtPkJsjdzpP8RdmAJpmhPs6pRbjRBfP9SbSkLrmZzbCVFp6lHrCBIbDtTtOYLeDdMt7hE&amp;amp;msgID=I5962787408821907456_500&amp;amp;markAsRead=&quot;&gt;LinkedIn Customer Service&lt;/a&gt;.&lt;/td&gt;                      &lt;/tr&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                         &lt;tbody&gt;                          &lt;tr&gt;                           &lt;td&gt;                            &lt;div style=&quot;height:10px;font-size:10px;line-height:10px&quot;&gt;                             &amp;nbsp;                            &lt;/div&gt;&lt;/td&gt;                          &lt;/tr&gt;                         &lt;/tbody&gt;                        &lt;/table&gt;&lt;/td&gt;                      &lt;/tr&gt;                      &lt;tr&gt;                       &lt;td&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                  &lt;/tr&gt;                  &lt;tr&gt;                   &lt;td&gt;                    &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:20px;font-size:20px;line-height:20px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                  &lt;/tr&gt;                 &lt;/tbody&gt;                &lt;/table&gt;&lt;/td&gt;              &lt;/tr&gt;             &lt;/tbody&gt;            &lt;/table&gt;&lt;/td&gt;          &lt;/tr&gt;         &lt;/tbody&gt;        &lt;/table&gt;&lt;/td&gt;      &lt;/tr&gt;     &lt;/tbody&gt;    &lt;/table&gt;    &lt;img src=&quot;http://www.linkedin.com/emimp/-ufpsde-i552gc3q-5w.gif&quot; style=&quot;width:1px; height:1px;&quot; /&gt;   </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/3948879825984704393/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/3948879825984704393?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/3948879825984704393'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/3948879825984704393'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2015/01/angelo-yin-s-invitation-is-awaiting.html' title='Angelo Yin (阴平)&#39;s invitation is awaiting your response'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-7216660245019739600</id><published>2015-01-18T19:48:00.001-08:00</published><updated>2015-01-18T19:48:45.698-08:00</updated><title type='text'>I&#39;d like to add you to my professional network on LinkedIn</title><content type='html'>&lt;span style=&quot;display: none !important;font-size: 1px;visibility: hidden;opacity: 0;color: transparent;height: 0;width: 0;mso-hide: all;&quot;&gt;&lt;/span&gt;    &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; border-collapse:collapse; width:100% !important; font-family:Helvetica,Arial,sans-serif; margin:0; padding:0;&quot; width=&quot;100%&quot; bgcolor=&quot;#DFDFDF&quot;&gt;     &lt;tbody&gt;      &lt;tr&gt;       &lt;td colspan=&quot;3&quot;&gt;        &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;1&quot;&gt;         &lt;tbody&gt;          &lt;tr&gt;           &lt;td&gt;            &lt;div style=&quot;height:5px;font-size:5px;line-height:5px;&quot;&gt;             &amp;nbsp;            &lt;/div&gt;&lt;/td&gt;          &lt;/tr&gt;         &lt;/tbody&gt;        &lt;/table&gt;&lt;/td&gt;      &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;        &lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; border=&quot;0&quot; align=&quot;center&quot; width=&quot;100%&quot; style=&quot;table-layout: fixed;&quot;&gt;         &lt;tbody&gt;          &lt;tr&gt;           &lt;td align=&quot;center&quot;&gt;            &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; min-width:290px;&quot; width=&quot;600&quot; class=&quot;responsive&quot;&gt;             &lt;tbody&gt;              &lt;tr&gt;               &lt;td style=&quot;font-family:Helvetica,Arial,sans-serif;&quot;&gt;                &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                 &lt;tbody&gt;                  &lt;tr&gt;                   &lt;td&gt;                    &lt;div style=&quot;height:8px;font-size:8px;line-height:8px&quot;&gt;                     &amp;nbsp;                    &lt;/div&gt;&lt;/td&gt;                  &lt;/tr&gt;                 &lt;/tbody&gt;                &lt;/table&gt;                &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif;&quot; width=&quot;100%&quot; bgcolor=&quot;#DDDDDD&quot;&gt;                 &lt;tbody&gt;                  &lt;tr&gt;                   &lt;td align=&quot;left&quot; valign=&quot;middle&quot; width=&quot;95&quot; height=&quot;21&quot; id=&quot;base-header-logo&quot;&gt;&lt;a style=&quot;text-decoration:none;cursor:pointer;border:none;display:block;height:21px;width:100%;&quot; href=&quot;https://www.linkedin.com/blink?simpleRedirect=7hPpnlDnSlQqnpKqjRHpipLpSZIbmlQqnpKqiRQsSlRpORIrmkZpSVFqSdxsDgCs6RKfngCe3oJoCQNoS8PdmAJpmhPs6pRbjRBfP9SbSkLrmZzbCVFp6lHrCBIbDtTtOYLeDdMt7hE&amp;amp;msgID=I5962787408821907456_500&amp;amp;markAsRead=&quot;&gt;&lt;img src=&quot;http://s.c.lnkd.licdn.com/scds/common/u/images/email/logos/logo_linkedin_tm_email_95x21_v1.png&quot; width=&quot;95&quot; height=&quot;21&quot; alt=&quot;LinkedIn&quot; style=&quot;border:none;text-decoration:none;&quot; /&gt;&lt;/a&gt;&lt;/td&gt;                  &lt;/tr&gt;                 &lt;/tbody&gt;                &lt;/table&gt;                &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                 &lt;tbody&gt;                  &lt;tr&gt;                   &lt;td&gt;                    &lt;div style=&quot;height:8px;font-size:8px;line-height:8px&quot;&gt;                     &amp;nbsp;                    &lt;/div&gt;&lt;/td&gt;                  &lt;/tr&gt;                 &lt;/tbody&gt;                &lt;/table&gt;                &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif;&quot; width=&quot;100%&quot; bgcolor=&quot;#333333&quot;&gt;                 &lt;tbody&gt;                  &lt;tr&gt;                   &lt;td width=&quot;20&quot; class=&quot;responsive-spacer&quot;&gt;                    &lt;table width=&quot;20&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:0px;font-size:0px;line-height:0px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                   &lt;td width=&quot;100%&quot;&gt;&lt;span style=&quot;color:#FFF;&quot;&gt;                     &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                      &lt;tbody&gt;                       &lt;tr&gt;                        &lt;td&gt;                         &lt;div style=&quot;height:18px;font-size:18px;line-height:18px&quot;&gt;                          &amp;nbsp;                         &lt;/div&gt;&lt;/td&gt;                       &lt;/tr&gt;                      &lt;/tbody&gt;                     &lt;/table&gt;Angelo Yin (阴平) would like to stay in touch on LinkedIn.                     &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                      &lt;tbody&gt;                       &lt;tr&gt;                        &lt;td&gt;                         &lt;div style=&quot;height:18px;font-size:18px;line-height:18px&quot;&gt;                          &amp;nbsp;                         &lt;/div&gt;&lt;/td&gt;                       &lt;/tr&gt;                      &lt;/tbody&gt;                     &lt;/table&gt;&lt;/span&gt;&lt;/td&gt;                   &lt;td width=&quot;20&quot; class=&quot;responsive-spacer&quot;&gt;                    &lt;table width=&quot;20&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:0px;font-size:0px;line-height:0px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                  &lt;/tr&gt;                 &lt;/tbody&gt;                &lt;/table&gt;                &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;100%&quot; bgcolor=&quot;#FFFFFF&quot;&gt;                 &lt;tbody&gt;                  &lt;tr&gt;                   &lt;td width=&quot;20&quot; class=&quot;res-width10&quot;&gt;                    &lt;table width=&quot;20px&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer res-width10&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:0px;font-size:0px;line-height:0px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                   &lt;td style=&quot;color: #333333; font-family: Helvetica,Arial,sans-serif; font-size: 15px; line-height: 18px;&quot; align=&quot;left&quot;&gt;                    &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer res-height10&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:20px;font-size:20px;line-height:20px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;                    &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;100%&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;100%&quot;&gt;                         &lt;tbody&gt;                          &lt;tr&gt;                           &lt;td style=&quot;font-family:Helvetica,Arial,sans-serif;color:#333333;font-size:15px;&quot;&gt;I&#39;d like to add you to my professional network on LinkedIn.&lt;/td&gt;                          &lt;/tr&gt;                          &lt;tr&gt;                           &lt;td&gt;                            &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                             &lt;tbody&gt;                              &lt;tr&gt;                               &lt;td&gt;                                &lt;div style=&quot;height:15px;font-size:15px;line-height:15px&quot;&gt;                                 &amp;nbsp;                                &lt;/div&gt;&lt;/td&gt;                              &lt;/tr&gt;                             &lt;/tbody&gt;                            &lt;/table&gt;&lt;/td&gt;                          &lt;/tr&gt;                          &lt;tr&gt;                           &lt;td&gt;- Angelo&lt;/td&gt;                          &lt;/tr&gt;                          &lt;tr&gt;                           &lt;td&gt;                            &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                             &lt;tbody&gt;                              &lt;tr&gt;                               &lt;td&gt;                                &lt;div style=&quot;height:20px;font-size:20px;line-height:20px&quot;&gt;                                 &amp;nbsp;                                &lt;/div&gt;&lt;/td&gt;                              &lt;/tr&gt;                             &lt;/tbody&gt;                            &lt;/table&gt;&lt;/td&gt;                          &lt;/tr&gt;                          &lt;tr&gt;                           &lt;td&gt;                            &lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; align=&quot;left&quot;&gt;                             &lt;tbody&gt;                              &lt;tr&gt;                               &lt;td align=&quot;center&quot; height=&quot;30&quot; valign=&quot;middle&quot; bgcolor=&quot;#287BBC&quot; background=&quot;http://s.c.lnkd.licdn.com/scds/common/u/img/email/bg_btn_katy_blue_medium.png&quot; style=&quot;background:url(http://s.c.lnkd.licdn.com/scds/common/u/img/email/bg_btn_katy_blue_medium.png) repeat-x scroll bottom #287BBC;background-color:#287BBC;border:1px solid #1B5480;-moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px; cursor: pointer;&quot;&gt;                                &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;100%&quot; bgcolor=&quot;transparent&quot;&gt;                                 &lt;tbody&gt;                                  &lt;tr&gt;                                   &lt;td width=&quot;13&quot;&gt;                                    &lt;table width=&quot;13px&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                                     &lt;tbody&gt;                                      &lt;tr&gt;                                       &lt;td&gt;                                        &lt;div style=&quot;height:0px;font-size:0px;line-height:0px&quot;&gt;                                         &amp;nbsp;                                        &lt;/div&gt;&lt;/td&gt;                                      &lt;/tr&gt;                                     &lt;/tbody&gt;                                    &lt;/table&gt;&lt;/td&gt;                                   &lt;td&gt;&lt;a href=&quot;https://www.linkedin.com/blink?simpleRedirect=3gSc3oRc38Ne3cTcPsUdP8SejkZh4BKrSBQonhFtCVF9A9zjCZBmn5afnBBiShBsC5EsOpQsSlRpRZBt6BSrCAZqSkConhzbmlQqnpKqiRQsSlRpORIrmkZpSVFqSdxsDgCpnhFtCV9pSlipn9Mfm4Ce3oJoCQNoS8PdmAJpmhPs6pRbjRBfP9SbSkLrmZzbCVFp6lHrCBIbDtTtOYLeDdMt7hE&amp;amp;msgID=I5962787408821907456_500&amp;amp;markAsRead=&quot; style=&quot;text-decoration:none; font-size: 13px;font-family: Helvetica,Arial,sans-serif;font-weight: bold;color: white;white-space: nowrap;display: block;&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;font-size: 13px;font-family: Helvetica,Arial,sans-serif;font-weight: bold;color: white;white-space: nowrap;display: block;&quot;&gt;Confirm that you know Angelo&lt;/span&gt;&lt;/a&gt;&lt;/td&gt;                                   &lt;td width=&quot;13&quot;&gt;                                    &lt;table width=&quot;13px&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                                     &lt;tbody&gt;                                      &lt;tr&gt;                                       &lt;td&gt;                                        &lt;div style=&quot;height:0px;font-size:0px;line-height:0px&quot;&gt;                                         &amp;nbsp;                                        &lt;/div&gt;&lt;/td&gt;                                      &lt;/tr&gt;                                     &lt;/tbody&gt;                                    &lt;/table&gt;&lt;/td&gt;                                  &lt;/tr&gt;                                 &lt;/tbody&gt;                                &lt;/table&gt;&lt;/td&gt;                              &lt;/tr&gt;                             &lt;/tbody&gt;                            &lt;/table&gt;&lt;/td&gt;                          &lt;/tr&gt;                         &lt;/tbody&gt;                        &lt;/table&gt;&lt;/td&gt;                      &lt;/tr&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;100%&quot;&gt;                         &lt;tbody&gt;                          &lt;tr&gt;                           &lt;td style=&quot;border-bottom:1px solid #e5e5e5;&quot;&gt;                            &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                             &lt;tbody&gt;                              &lt;tr&gt;                               &lt;td&gt;                                &lt;div style=&quot;height:20px;font-size:20px;line-height:20px&quot;&gt;                                 &amp;nbsp;                                &lt;/div&gt;&lt;/td&gt;                              &lt;/tr&gt;                             &lt;/tbody&gt;                            &lt;/table&gt;&lt;/td&gt;                          &lt;/tr&gt;                         &lt;/tbody&gt;                        &lt;/table&gt;&lt;/td&gt;                      &lt;/tr&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                         &lt;tbody&gt;                          &lt;tr&gt;                           &lt;td&gt;                            &lt;div style=&quot;height:20px;font-size:20px;line-height:20px&quot;&gt;                             &amp;nbsp;                            &lt;/div&gt;&lt;/td&gt;                          &lt;/tr&gt;                         &lt;/tbody&gt;                        &lt;/table&gt;&lt;/td&gt;                      &lt;/tr&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;100%&quot;&gt;                         &lt;tbody&gt;                          &lt;tr&gt;                           &lt;td width=&quot;120&quot; valign=&quot;top&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;img alt=&quot;Angelo Yin (阴平)&quot; src=&quot;http://m.c.lnkd.licdn.com/mpr/mpr/shrink_60_60/p/6/005/00b/218/240b12c.jpg&quot; width=&quot;60&quot; height=&quot;60&quot; border=&quot;0&quot; style=&quot;border:none;outline:hidden;&quot; /&gt;&lt;/td&gt;                           &lt;td width=&quot;20&quot;&gt;                            &lt;table width=&quot;20&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                             &lt;tbody&gt;                              &lt;tr&gt;                               &lt;td&gt;                                &lt;div style=&quot;height:0px;font-size:0px;line-height:0px&quot;&gt;                                 &amp;nbsp;                                &lt;/div&gt;&lt;/td&gt;                              &lt;/tr&gt;                             &lt;/tbody&gt;                            &lt;/table&gt;&lt;/td&gt;                           &lt;td style=&quot;vertical-align: top; font-family: Helvetica,Arial,sans-serif;&quot; width=&quot;100%&quot;&gt;                            &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;100%&quot;&gt;                             &lt;tbody&gt;                              &lt;tr&gt;                               &lt;td style=&quot;font-size: 20px; font-weight: bold; color:#333333;&quot; valign=&quot;top&quot;&gt;Angelo Yin (阴平)&lt;/td&gt;                              &lt;/tr&gt;                              &lt;tr&gt;                               &lt;td style=&quot;color:#333333;font-size:15px;&quot; class=&quot;res-font16&quot;&gt;Senior Software Developer at Schlumberger&lt;/td&gt;                              &lt;/tr&gt;                              &lt;tr&gt;                               &lt;td style=&quot;color:#B3B3B3;font-size:13px;&quot; class=&quot;res-font16&quot;&gt;Huhehaote, Inner Mongolia, China&lt;/td&gt;                              &lt;/tr&gt;                             &lt;/tbody&gt;                            &lt;/table&gt;&lt;/td&gt;                          &lt;/tr&gt;                         &lt;/tbody&gt;                        &lt;/table&gt;&lt;/td&gt;                      &lt;/tr&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                         &lt;tbody&gt;                          &lt;tr&gt;                           &lt;td&gt;                            &lt;div style=&quot;height:20px;font-size:20px;line-height:20px&quot;&gt;                             &amp;nbsp;                            &lt;/div&gt;&lt;/td&gt;                          &lt;/tr&gt;                         &lt;/tbody&gt;                        &lt;/table&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                   &lt;td width=&quot;20&quot; class=&quot;res-width10&quot;&gt;                    &lt;table width=&quot;20px&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer res-width10&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:0px;font-size:0px;line-height:0px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                  &lt;/tr&gt;                 &lt;/tbody&gt;                &lt;/table&gt;&lt;/td&gt;              &lt;/tr&gt;             &lt;/tbody&gt;            &lt;/table&gt;            &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;600&quot; class=&quot;responsive&quot;&gt;             &lt;tbody&gt;              &lt;tr&gt;               &lt;td align=&quot;left&quot;&gt;                &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; &quot; width=&quot;100%&quot; class=&quot;responsive&quot;&gt;                 &lt;tbody&gt;                  &lt;tr&gt;                   &lt;td&gt;                    &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:10px;font-size:10px;line-height:10px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                  &lt;/tr&gt;                  &lt;tr&gt;                   &lt;td align=&quot;left&quot;&gt;                    &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;font-family:Helvetica,Arial,sans-serif; font-size:11px; font-family:Helvetica,Arial,sans-serif; color:#999999;&quot; width=&quot;100%&quot; class=&quot;responsive res-font10&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;You received an invitation to connect. LinkedIn will use your email address to make suggestions to our members in features like People You May Know. &lt;a style=&quot;text-decoration:none;color:#0077B5;&quot; href=&quot;https://www.linkedin.com/blink?simpleRedirect=0UdyRyrj5zozcRqiRBp7dMpDkJfmhFpipTenFjc4lyskdhnP1zhj5CpStluiRPlAFeuBxFl5BmpQZCjSJxkl1EuzgSpkFmrChbi7xHlR5FsPcJlQ54kBtTrnwMh7FPbkhjcSZHjD1kgndlgk51klFelklBlmlWtQl7kk4Zp6BLr2pQsSlRpRZBt6BSrCAZqSkCkjoPp4l7q5p6sCR6kk4ZrClHrRhAqmQCrDlIfngCe3oJoCQNoS8PdmAJpmhPs6pRbjRBfP9SbSkLrmZzbCVFp6lHrCBIbDtTtOYLeDdMt7hE&amp;amp;msgID=I5962787408821907456_500&amp;amp;markAsRead=&quot;&gt;Unsubscribe&lt;/a&gt;&lt;/td&gt;                      &lt;/tr&gt;                      &lt;tr&gt;                       &lt;td&gt;&lt;a style=&quot;text-decoration:none;color:#0077B5;&quot; href=&quot;https://www.linkedin.com/blink?simpleRedirect=0Ue3sQfmh9pmNzqnhOoipQsSlRpRZBt6BSrCAZqSkCr79lpmdFtD9BkT9BrmZQsTlzfm4Ce3oJoCQNoS8PdmAJpmhPs6pRbjRBfP9SbSkLrmZzbCVFp6lHrCBIbDtTtOYLeDdMt7hE&amp;amp;msgID=I5962787408821907456_500&amp;amp;markAsRead=&quot;&gt;Learn why we included this.&lt;/a&gt;&lt;/td&gt;                      &lt;/tr&gt;                      &lt;tr&gt;                       &lt;td&gt;If you need assistance or have questions, please contact &lt;a style=&quot;color:#0077B5;text-decoration:none;&quot; target=&quot;_blank&quot; href=&quot;https://www.linkedin.com/blink?simpleRedirect=1QsSlRpRZBt6BSrCAZqSkCr79lpmdFtD9BkT9BrmZQsTlzfm4Ce3oJoCQNoS8PdmAJpmhPs6pRbjRBfP9SbSkLrmZzbCVFp6lHrCBIbDtTtOYLeDdMt7hE&amp;amp;msgID=I5962787408821907456_500&amp;amp;markAsRead=&quot;&gt;LinkedIn Customer Service&lt;/a&gt;.&lt;/td&gt;                      &lt;/tr&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                         &lt;tbody&gt;                          &lt;tr&gt;                           &lt;td&gt;                            &lt;div style=&quot;height:10px;font-size:10px;line-height:10px&quot;&gt;                             &amp;nbsp;                            &lt;/div&gt;&lt;/td&gt;                          &lt;/tr&gt;                         &lt;/tbody&gt;                        &lt;/table&gt;&lt;/td&gt;                      &lt;/tr&gt;                      &lt;tr&gt;                       &lt;td&gt;&amp;copy; 2014, LinkedIn Corporation. 2029 Stierlin Ct. Mountain View, CA 94043, USA&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                  &lt;/tr&gt;                  &lt;tr&gt;                   &lt;td&gt;                    &lt;table width=&quot;1&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; class=&quot;email-spacer&quot;&gt;                     &lt;tbody&gt;                      &lt;tr&gt;                       &lt;td&gt;                        &lt;div style=&quot;height:20px;font-size:20px;line-height:20px&quot;&gt;                         &amp;nbsp;                        &lt;/div&gt;&lt;/td&gt;                      &lt;/tr&gt;                     &lt;/tbody&gt;                    &lt;/table&gt;&lt;/td&gt;                  &lt;/tr&gt;                 &lt;/tbody&gt;                &lt;/table&gt;&lt;/td&gt;              &lt;/tr&gt;             &lt;/tbody&gt;            &lt;/table&gt;&lt;/td&gt;          &lt;/tr&gt;         &lt;/tbody&gt;        &lt;/table&gt;&lt;/td&gt;      &lt;/tr&gt;     &lt;/tbody&gt;    &lt;/table&gt;    &lt;img src=&quot;http://www.linkedin.com/emimp/-ufpsde-i53bc1mb-68.gif&quot; style=&quot;width:1px; height:1px;&quot; /&gt;   </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/7216660245019739600/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/7216660245019739600?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/7216660245019739600'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/7216660245019739600'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2015/01/id-like-to-add-you-to-my-professional.html' title='I&#39;d like to add you to my professional network on LinkedIn'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-4293082337568658882</id><published>2011-02-15T21:16:00.000-08:00</published><updated>2011-02-21T02:42:46.520-08:00</updated><title type='text'>C++ Primer, Fourth Edition:Notes Chapter 14. Overloaded Operations and Conversions</title><content type='html'>&lt;ul&gt; &lt;li&gt;C++ lets us redefine the meaning of the operators when applied to objects of class type. It also lets us define conversion operations for class types. &lt;a name=&quot;ch14term3&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch14lev1sec11.html#gloss14_03&quot;&gt;Class-type conversions&lt;/a&gt; are used like the built-in conversions to implicitly convert an object of one type to another type when needed  &lt;li&gt;Operator overloading allows the programmer to define versions of the operators for operands of class type  &lt;li&gt;Through operator overloading, we can redefine most of the operators from &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch05.html#ch05&quot;&gt;Chapter 5&lt;/a&gt; to work on objects of class type. Judicious use of operator overloading can make class types as intuitive to use as the built-in types  &lt;li&gt;Allowing programs to use expressions rather than named functions can make the programs much easier to write and read.&lt;/li&gt;&lt;/ul&gt; &lt;h4&gt;Section 14.1 Defining an Overloaded Operator&lt;/h4&gt; &lt;ul&gt; &lt;li&gt;&lt;strong&gt;Overloaded operators are functions with special names: the keyword &lt;tt&gt;operator&lt;/tt&gt; followed by the symbol for the operator being defined. Like any other function, an overloaded operator has a return type and a parameter list&lt;/strong&gt;  &lt;li&gt;&lt;strong&gt;With the exception of the function-call operator, an overloaded operator has the same number of parameters (including the implicit &lt;tt&gt;this&lt;/tt&gt; pointer for member functions) as the operator has operands. The function-call operator takes any number of operands&lt;/strong&gt;  &lt;li&gt;Overloaded Operator Names  &lt;ul&gt; &lt;p style=&quot;line-height: normal&quot;&gt; &lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt; &lt;caption&gt; &lt;h5 class=&quot;docTableTitle&quot;&gt;&lt;font color=&quot;#000000&quot; face=&quot;Times New Roman&quot;&gt;Table 14.1. Overloadable Operators&lt;/font&gt;&lt;/h5&gt;&lt;/caption&gt; &lt;colgroup&gt; &lt;col width=&quot;75&quot;&gt; &lt;col width=&quot;75&quot;&gt; &lt;col width=&quot;75&quot;&gt; &lt;col width=&quot;75&quot;&gt; &lt;col width=&quot;75&quot;&gt; &lt;col width=&quot;75&quot;&gt;&lt;/colgroup&gt; &lt;thead&gt;&lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;+&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;-&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;*&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;/&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;%&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;^&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;&amp;amp;&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;|&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;~&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;!&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;,&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;=&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;&amp;lt;&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;&amp;gt;&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;&amp;lt;=&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;&amp;gt;=&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;++&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;--&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;&amp;lt;&amp;lt;&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;&amp;gt;&amp;gt;&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;==&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;!=&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;&amp;amp;&amp;amp;&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;||&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;+=&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;-=&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;/=&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;%=&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;^=&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;&amp;amp;=&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;|=&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;*=&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;&amp;lt;&amp;lt;=&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;&amp;gt;&amp;gt;=&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;[]&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;()&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;-&amp;gt;&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;-&amp;gt;*&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;new&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;new []&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;delete&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;75&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;delete[]&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;br&gt; &lt;p style=&quot;line-height: normal&quot;&gt; &lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt; &lt;caption&gt; &lt;h5 class=&quot;docTableTitle&quot;&gt;&lt;font color=&quot;#000000&quot; face=&quot;Times New Roman&quot;&gt;Table 14.2. Operators That Cannot Be Overloaded&lt;/font&gt;&lt;/h5&gt;&lt;/caption&gt; &lt;colgroup&gt; &lt;col width=&quot;125&quot;&gt; &lt;col width=&quot;125&quot;&gt; &lt;col width=&quot;125&quot;&gt; &lt;col width=&quot;125&quot;&gt;&lt;/colgroup&gt; &lt;thead&gt;&lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;125&quot; align=&quot;middle&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;::&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;125&quot; align=&quot;middle&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;.*&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;125&quot; align=&quot;middle&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;.&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;125&quot; align=&quot;middle&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;?:&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;br&gt; &lt;li&gt;New operators may not be created by concatenating other legal symbols  &lt;li&gt;&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Overloaded Operators Must Have an Operand of Class Type  &lt;ul&gt; &lt;li&gt;The meaning of an operator for the built-in types may not be changed.Nor may additional operators be defined for the built-in data types  &lt;li&gt;&lt;strong&gt;An overloaded operator must have at least one operand of class or enumeration (&lt;/strong&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch02lev1sec7.html#ch02lev1sec7&quot;&gt;&lt;strong&gt;Section 2.7&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;, p. &lt;/strong&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch02lev1sec7.html#ch02lev1sec7&quot;&gt;&lt;strong&gt;62&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;) type. This rule enforces the requirement that an overloaded operator may not redefine the meaning of the operators when applied to objects of built-in type&lt;/strong&gt;  &lt;li&gt;&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Precedence and Associativity Are Fixed  &lt;ul&gt; &lt;li&gt;The precedence (&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch05lev1sec10.html#ch05lev2sec14&quot;&gt;Section 5.10.1&lt;/a&gt;, p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch05lev1sec10.html#ch05lev2sec14&quot;&gt;168&lt;/a&gt;), associativity, or number of operands of an operator cannot be changed  &lt;li&gt;Four symbols (&lt;tt&gt;+, -, *&lt;/tt&gt;, and &lt;tt&gt;&amp;amp;)&lt;/tt&gt; serve as both unary and binary operators. Either or both of these operators can be overloaded. Which operator is being defined is controlled by the number of operands. Default arguments for overloaded operators are illegal, except for &lt;tt&gt;operator()&lt;/tt&gt;, the function-call operator&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Short-Ciruit Evaluation Is Not Preserved  &lt;ul&gt; &lt;li&gt;Overloaded operators make no guarantees about the order in which operands are evaluated. In particular, the operand-evaluation guarantees of the built-in logical AND, logical OR (Section 5.2, p. 152), and comma (Section 5.9, p. 168) operators are not preserved. Both operands to an overloaded version of &amp;amp;&amp;amp; or || are always evaluated. The order in which those operands are evaluated is not stipulated. The order in which the operands to the comma are evaluated is also not defined. For this reason, it is usually a bad idea to overload &amp;amp;&amp;amp;, ||, or the comma operator.&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Class Member versus Nonmember  &lt;ul&gt; &lt;li&gt;Most overloaded operators may be defined as ordinary nonmember functions or as class member functions  &lt;li&gt;&lt;strong&gt;Overloaded functions that are members of a class may appear to have one less parameter than the number of operands. Operators that are member functions have an implicit &lt;tt&gt;this&lt;/tt&gt; parameter that is bound to the first operand&lt;/strong&gt;  &lt;li&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Operator Overloading and Friendship  &lt;ul&gt; &lt;li&gt;When operators are defined as nonmember functions, they often must be made friends (&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch12lev1sec5.html#ch12lev1sec5&quot;&gt;Section 12.5&lt;/a&gt;, p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch12lev1sec5.html#ch12lev1sec5&quot;&gt;465&lt;/a&gt;) of the class(es) on which they operate&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Using Overloaded Operators  &lt;ul&gt; &lt;li&gt;We can use an overloaded operator in the same way that we&#39;d use the operator on operands of built-in type  &lt;li&gt;We also can call an overloaded operator function in the same way that we call an ordinary function: We name the function and pass an appropriate number of arguments of the appropriate type  &lt;ul&gt; &lt;li&gt; &lt;div style=&quot;border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px&quot; id=&quot;codeSnippetWrapper&quot;&gt; &lt;div style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot; id=&quot;codeSnippet&quot;&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;cout &amp;lt;&amp;lt; item1 + item2 &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&amp;nbsp;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&lt;span style=&quot;color: #008000&quot;&gt;// equivalent direct call to nonmember operator function&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&lt;span style=&quot;color: #008000&quot;&gt;// This call has the same effect as the expression that added item1 and item2.&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #0000ff&quot;&gt;operator&lt;/span&gt;+(item1, item2) &amp;lt;&amp;lt; endl;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;We call a member operator function the same way we call any other member function: We name an object on which to run the function and then use the dot or arrow operator to fetch the function we wish to call passing the required number and type of arguments. In the case of a binary member operator function, we must pass a single operand &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;div style=&quot;border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px&quot; id=&quot;codeSnippetWrapper&quot;&gt;&lt;br /&gt;&lt;div style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot; id=&quot;codeSnippet&quot;&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;item1 += item2;            &lt;span style=&quot;color: #008000&quot;&gt;// expression based &quot;call&quot;&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;item1.&lt;span style=&quot;color: #0000ff&quot;&gt;operator&lt;/span&gt;+=(item2);   // equivalent call to member &lt;span style=&quot;color: #0000ff&quot;&gt;operator&lt;/span&gt; &lt;span style=&quot;color: #cc6633&quot;&gt;function&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;/div&gt;&lt;/div&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;14.1.1. Overloaded Operator Design &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Don&#39;t Overload Operators with Built-in Meanings &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;The assignment, address of, and comma operators have default meanings for operands of class types. If there is no overloaded version specified, the compiler defines its own version of these operators &lt;br /&gt;&lt;li&gt;The synthesized assignment operator (&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch13lev1sec2.html#ch13lev1sec2&quot;&gt;Section 13.2&lt;/a&gt;, p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch13lev1sec2.html#ch13lev1sec2&quot;&gt;482&lt;/a&gt;) does memberwise assignment: It uses each member&#39;s own assignment operator to assign each member in turn &lt;br /&gt;&lt;li&gt;By default the address of (&lt;tt&gt;&amp;amp;&lt;/tt&gt;) and comma (&lt;tt&gt;,&lt;/tt&gt;) operators execute on class type objects the same way they do on objects of built-in type. The address of operator returns the address in memory of the object to which it is applied. The comma operator evaluates each expression from left to right and returns the value of its rightmost operand &lt;br /&gt;&lt;li&gt;The built-in logical AND (&lt;tt&gt;&amp;amp;&amp;amp;&lt;/tt&gt;) and OR(&lt;tt&gt;||)&lt;/tt&gt; operators apply short-circuit evaluation (&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch05lev1sec2.html#ch05lev1sec2&quot;&gt;Section 5.2&lt;/a&gt;, p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch05lev1sec2.html#ch05lev1sec2&quot;&gt;152&lt;/a&gt;). If the operator is redefined, the short-circuit nature of the operators is lost &lt;br /&gt;&lt;li&gt;&lt;strong&gt;It is usually not a good idea to overload the comma, address-of, logical AND, or logical OR operators. These operators have built-in meanings that are useful and become inaccessible if we define our own versions&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;We sometimes must define our own version of assignment. When we do so, it should behave analogously to the synthesized operators: After an assignment, the values in the left-hand and right-hand operands should be the same and the operator should return a reference to its left-hand operand. Overloaded assignment should customize the built-in meaning of assignment, not circumvent it&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;Most Operators Have No Meaning for Class Objects &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Operators other than assignment, address-of, and comma have no meaning when applied to an operand of class type unless an overloaded definition is provided. When designing a class, we decide which, if any, operators to support &lt;br /&gt;&lt;li&gt;The best way to design operators for a class is first to design the class&#39; public interface. Once the interface is defined, it is possible to think about which operations should be defined as overloaded operators. Those operations with a logical mapping to an operator are good candidates&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;Compound Assignment Operators &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;If a class has an arithmetic (&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch05lev1sec1.html#ch05lev1sec1&quot;&gt;Section 5.1&lt;/a&gt;, p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch05lev1sec1.html#ch05lev1sec1&quot;&gt;149&lt;/a&gt;) or bitwise (&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch05lev1sec3.html#ch05lev1sec3&quot;&gt;Section 5.3&lt;/a&gt;, p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch05lev1sec3.html#ch05lev1sec3&quot;&gt;154&lt;/a&gt;) operator, then it is usually a good idea to provide the corresponding compound-assignment operator as well&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;Equality and Relational Operators &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Classes that will be used as the key type of an associative container should define the &lt;tt&gt;&amp;lt;&lt;/tt&gt; operator. The associative containers by default use the &lt;tt&gt;&amp;lt;&lt;/tt&gt; operator of the key type. Even if the type will be stored only in a sequential container, the class ordinarily should define the equality (&lt;tt&gt;==&lt;/tt&gt;) and less-than (&lt;tt&gt;&amp;lt;&lt;/tt&gt;) operators. The reason &lt;a name=&quot;idd1e105030&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e105038&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e105046&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e105051&quot;&gt;&lt;/a&gt;is that many algorithms assume that these operators exist &lt;br /&gt;&lt;li&gt;If the class defines the equality operator, it should also define &lt;tt&gt;!=&lt;/tt&gt;. Users of the class will assume that if they can compare for equality, they can also compare for inequality. The same argument applies to the other relational operators as well. If the class defines &lt;tt&gt;&amp;lt;&lt;/tt&gt;, then it probably should define all four relational operators (&lt;tt&gt;&amp;gt;, &amp;gt;=, &amp;lt;&lt;/tt&gt;, and &lt;tt&gt;&amp;lt;=)&lt;/tt&gt; &lt;br /&gt;&lt;h2 style=&quot;text-transform: none&quot; class=&quot;docSidebarTitle&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;&lt;font style=&quot;font-size: 18pt&quot; color=&quot;#000000&quot;&gt;&lt;font style=&quot;font-weight: bold&quot;&gt;Caution: Use Operator Overloading Judiciously&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/h2&gt;&lt;br /&gt;&lt;p style=&quot;line-height: normal&quot; class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font color=&quot;#000000&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;&lt;font style=&quot;font-size: 12pt&quot;&gt;Each operator has an associated meaning from its use on the built-in types. Binary &lt;/font&gt;&lt;/font&gt;&lt;font style=&quot;font-size: 12pt&quot;&gt;&lt;tt&gt;+&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt;, for example, is strongly identified with addition. Mapping binary &lt;/font&gt;&lt;tt&gt;+&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt; to an analogous operation for a class type can provide a convenient notational shorthand. For example, the library &lt;/font&gt;&lt;tt&gt;string&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt; type, following a convention common to many programming languages, uses &lt;/font&gt;&lt;tt&gt;+&lt;/tt&gt;&lt;/font&gt;&lt;font face=&quot;Times New Roman&quot;&gt;&lt;font style=&quot;font-size: 12pt&quot;&gt; to represent concatenation&quot;adding&quot; one string to the other.&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style=&quot;line-height: normal&quot; class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;&lt;font style=&quot;font-size: 12pt&quot; color=&quot;#000000&quot;&gt;Operator overloading is most useful when there is a logical mapping of a built-in operator to an operation on our type. Using overloaded operators rather than inventing named operations can make our programs more natural and intuitive. Overuse or outright abuse of operator overloading can make our classes incomprehensible.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style=&quot;line-height: normal&quot; class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font color=&quot;#000000&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;&lt;font style=&quot;font-size: 12pt&quot;&gt;Obvious abuses of operator overloading rarely happen in practice. As an example, no responsible programmer would define &lt;/font&gt;&lt;/font&gt;&lt;font style=&quot;font-size: 12pt&quot;&gt;&lt;tt&gt;operator+&lt;/tt&gt;&lt;/font&gt;&lt;font face=&quot;Times New Roman&quot;&gt;&lt;font style=&quot;font-size: 12pt&quot;&gt; to perform subtraction. More common, but still inadvisable, are uses that contort an operator&#39;s &quot;normal&quot; meaning to force a fit to a given type. Operators should be used only for operations that are likely to be unambiguous to users. An operator with ambiguous meaning, in this sense, is one that supports equally well a number of different interpretations.&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;a style=&quot;line-height: normal&quot; name=&quot;ch14note04&quot;&gt;&lt;/a&gt;&lt;br /&gt;&lt;div style=&quot;line-height: normal&quot; class=&quot;docNote&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; width=&quot;90%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot; width=&quot;60&quot;&gt;&lt;img border=&quot;0&quot; alt=&quot;&quot; align=&quot;left&quot; src=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/images/0201721481/graphics/bestpractices.jpg;400478&quot; width=&quot;116&quot; height=&quot;74&quot;&gt;&lt;/td&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font color=&quot;#000000&quot; face=&quot;Times New Roman&quot;&gt;When the meaning of an overloaded operator is not obvious, it is better to give the operation a name. It is also usually better to use a named function rather than an operator for operations that are rarely done. If the operation is unusual, the brevity of using an operator is unnecessary.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;Choosing Member or Nonmember Implementation &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;The following guidelines can be of help when deciding whether to make an operator a member or an ordinary nonmember function &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;The assignment (&lt;tt&gt;=&lt;/tt&gt;), subscript (&lt;tt&gt;[]&lt;/tt&gt;), call (&lt;tt&gt;()&lt;/tt&gt;), and member access arrow (&lt;tt&gt;-&amp;gt;&lt;/tt&gt;) operators must be defined as members. Defining any of these operators as a nonmember function is flagged at compile time as an error &lt;br /&gt;&lt;li&gt;Like assignment, the compound-assignment operators ordinarily ought to be members of the class. Unlike assignment, they are not required to be so and the compiler will not complain if a nonmember compound-assignment operator is defined &lt;br /&gt;&lt;li&gt;Other operators that change the state of their object or that are closely tied to their given typesuch as increment, decrement, and dereferenceusually should be members of the class &lt;br /&gt;&lt;li&gt;Symmetric operators, such as the arithmetic, equality, relational, and bitwise operators, are best defined as ordinary nonmember functions&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;h4&gt;Section 14.2 Input and Output Operators&lt;/h4&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Classes that support I/O ordinarily should do so by using the same interface as defined by the &lt;tt&gt;iostream&lt;/tt&gt; library for the built-in types. Thus, many classes provide overloaded instances of the input and output operators &lt;br /&gt;&lt;li&gt;14.2.1. Overloading the Output Operator &amp;lt;&amp;lt; &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;To be consistent with the IO library, the operator should take an &lt;tt&gt;ostream&amp;amp;&lt;/tt&gt; as its first parameter and a reference to a &lt;tt&gt;const&lt;/tt&gt; object of the class type as its second. The operator should return a reference to its &lt;tt&gt;ostream&lt;/tt&gt; parameter &lt;br /&gt;&lt;div style=&quot;border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px&quot; id=&quot;codeSnippetWrapper&quot;&gt;&lt;br /&gt;&lt;div style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot; id=&quot;codeSnippet&quot;&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&lt;span style=&quot;color: #008000&quot;&gt;// general skeleton of the overloaded output operator&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;ostream&amp;amp;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    &lt;span style=&quot;color: #0000ff&quot;&gt;operator&lt;/span&gt; &amp;lt;&amp;lt;(ostream&amp;amp; os, &lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; ClassType &amp;amp;object)&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;{&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    &lt;span style=&quot;color: #008000&quot;&gt;// any special logic to prepare object&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&amp;nbsp;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    &lt;span style=&quot;color: #008000&quot;&gt;// actual output of members&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    os &amp;lt;&amp;lt; &lt;span style=&quot;color: #008000&quot;&gt;// ...&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&amp;nbsp;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;        &lt;span style=&quot;color: #008000&quot;&gt;// return ostream object&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;        &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; os;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;}&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;The Sales_item Output Operator &lt;br /&gt;&lt;li&gt;Output Operators Usually Do Minimal Formatting &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Class designers face one significant decision about output: whether and how much formatting to perform &lt;br /&gt;&lt;li&gt;Generally, output operators should print the contents of the object, with minimal formatting. They should not print a newline &lt;br /&gt;&lt;li&gt;The output operators for the built-in types do little if any formatting and do not print newlines. Given this treatment for the built-in types, users expect class output operators to behave similarly. By limiting the output operator to printing just the contents of the object, we let the users determine what if any additional formatting to perform. In particular, an output operator should not print a newline. If the operator does print a newline, then users would be unable to print descriptive text along with the object on the same line. By having the output operator perform minimal formatting, we let users control the details of their output&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;IO Operators Must Be Nonmember Functions &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;When we define an input or output operator that conforms to the conventions of the &lt;tt&gt;iostream&lt;/tt&gt; library, we must make it a nonmember operator &lt;br /&gt;&lt;li&gt;&lt;strong&gt;We cannot make the operator a member of our own class.If we did, then the left-hand operand would have to be an object of our class type&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;Instead, if we want to use the overloaded operators to do IO for our types, we must define them as a nonmember functions. IO operators usually read or write the non&lt;tt&gt;public&lt;/tt&gt; data members. As a consequence, classes often make the IO operators friends&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;14.2.2. Overloading the Input Operator &amp;gt;&amp;gt; &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Similar to the output operator, the input operator takes a first parameter that is a reference to the stream from which it is to read, and returns a reference to that same stream. Its second parameter is a non&lt;tt&gt;const&lt;/tt&gt; reference to the object into which to read. The second parameter must be non&lt;tt&gt;const&lt;/tt&gt; because the purpose of an input operator is to read data into this object &lt;br /&gt;&lt;li&gt;&lt;strong&gt;A more important, and less obvious, difference between input and output operators is that input operators must deal with the possibility of errors and end-of-file&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;The Sales_item Input Operator &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;div style=&quot;border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px&quot; id=&quot;codeSnippetWrapper&quot;&gt;&lt;br /&gt;&lt;div style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot; id=&quot;codeSnippet&quot;&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;istream&amp;amp; &lt;span style=&quot;color: #0000ff&quot;&gt;operator&lt;/span&gt;&amp;gt;&amp;gt;(istream&amp;amp; in, Sales_item&amp;amp; s)&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;{&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    &lt;span style=&quot;color: #0000ff&quot;&gt;double&lt;/span&gt; price;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    in &amp;gt;&amp;gt; s.isbn &amp;gt;&amp;gt; s.units_sold &amp;gt;&amp;gt; price;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    &lt;span style=&quot;color: #008000&quot;&gt;// check that the inputs succeeded&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    &lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt; (in)&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;        s.revenue = s.units_sold * price;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    &lt;span style=&quot;color: #0000ff&quot;&gt;else&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;        s = Sales_item(); &lt;span style=&quot;color: #008000&quot;&gt;// input failed: reset object to default state&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; in;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;}&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;/div&gt;&lt;/div&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;Errors During Input &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;The kinds of errors that might happen include: &lt;br /&gt;&lt;li&gt;Any of the read operations could fail because an incorrect value was provided. &lt;br /&gt;&lt;li&gt;Any of the reads could hit end-of-file or some other error on the input stream &lt;br /&gt;&lt;li&gt;Rather than checking each read, we check once before using the data we read&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;Handling Input Errors &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;If an input operator detects that the input failed, it is often a good idea to make sure that the object is in a usable and consistent state. Doing so is particularly important if the object might have been partially written before the error occurred &lt;br /&gt;&lt;li&gt;When designing an input operator, it is important to decide what to do about error-recovery, if anything&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;Indicating Errors &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;In addition to handling any errors that might occur, an input operator might need to set the condition state (&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch08lev1sec2.html#ch08lev1sec2&quot;&gt;Section 8.2&lt;/a&gt;, p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch08lev1sec2.html#ch08lev1sec2&quot;&gt;287&lt;/a&gt;) of its input &lt;tt&gt;istream&lt;/tt&gt; parameter &lt;br /&gt;&lt;li&gt;Some input operators do need to do additional checking. &lt;br /&gt;&lt;li&gt;Usually an input operator needs to set only the &lt;tt&gt;failbit&lt;/tt&gt;. Setting &lt;tt&gt;eofbit&lt;/tt&gt; would imply that the file was exhausted, and setting &lt;tt&gt;badbit&lt;/tt&gt; would indicate that the stream was corrupted. These errors are best left to the IO library itself to indicate.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;h4&gt;Section 14.3 Arithmetic and Relational Operators&lt;/h4&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Ordinarily, we define the arithmetic and relational operators as nonmember functions &lt;br /&gt;&lt;li&gt;Note that to be consistent with the built-in operator, addition returns an rvalue, not a reference &lt;br /&gt;&lt;li&gt;&lt;strong&gt;An arithmetic operator usually generates a new value that is the result of a computation on its two operands. That value is distinct from either operand and is calculated in a local variable. It would be a run-time error to return a reference to that variable&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;Classes that define both an arithmetic operator and the related compound assignment ordinarily ought to implement the arithmetic operator by using the compound assignment &lt;br /&gt;&lt;li&gt;It is simpler and more efficient to implement the arithmetic operator (e.g., &lt;tt&gt;+&lt;/tt&gt;) in terms of the compound-assignment operator (e.g., &lt;tt&gt;+=&lt;/tt&gt;) rather than the other way around. &lt;strong&gt;If we implemented += by calling +, then += would needlessly create and destroy a temporary to hold the result from +.&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;14.3.1. Equality Operators &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Ordinarily, classes in C++ use the equality operator to mean that the objects are equivalent. That is, they usually compare every data member and treat two objects &lt;a name=&quot;idd1e106183&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e106191&quot;&gt;&lt;/a&gt;as equal if and only if all corresponding members are the same &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;div style=&quot;border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px&quot; id=&quot;codeSnippetWrapper&quot;&gt;&lt;br /&gt;&lt;div style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot; id=&quot;codeSnippet&quot;&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;inline&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;bool&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    &lt;span style=&quot;color: #0000ff&quot;&gt;operator&lt;/span&gt;==(&lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; Sales_item &amp;amp;lhs, &lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; Sales_item &amp;amp;rhs)&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;{&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    &lt;span style=&quot;color: #008000&quot;&gt;// must be made a friend of Sales_item&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; lhs.units_sold == rhs.units_sold &amp;amp;&amp;amp;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;        lhs.revenue == rhs.revenue &amp;amp;&amp;amp;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;        lhs.same_isbn(rhs);&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;}&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;inline&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;bool&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    &lt;span style=&quot;color: #0000ff&quot;&gt;operator&lt;/span&gt;!=(&lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; Sales_item &amp;amp;lhs, &lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; Sales_item &amp;amp;rhs)&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;{&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; !(lhs == rhs); &lt;span style=&quot;color: #008000&quot;&gt;// != defined in terms of operator==&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;}&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;/div&gt;&lt;/div&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;The definition of these functions is trivial. More important are the design principles that these functions embody &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;If a class defines the &lt;tt&gt;==&lt;/tt&gt; operator, it defines it to mean that two objects contain the same data &lt;br /&gt;&lt;li&gt;If a class has an operation to determine whether two objects of the type are equal, it is usually right to define that function as &lt;tt&gt;operator==&lt;/tt&gt; rather than inventing a named operation. Users will expect to be able to compare objects using &lt;tt&gt;==&lt;/tt&gt;, and doing so is easier than remembering a new name &lt;br /&gt;&lt;li&gt;If a class defines &lt;tt&gt;operator==&lt;/tt&gt;, it should also define &lt;tt&gt;operator!=&lt;/tt&gt;. Users will expect that if they can use one operator, then the other will also exist &lt;br /&gt;&lt;li&gt;The equality and inequality operators should almost always be defined in terms of each other. One operator should do the real work to compare objects. The other should call the one that does the real work&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;Classes that define &lt;tt&gt;operator==&lt;/tt&gt; are easier to use with the standard library. Some algorithms, such as &lt;tt&gt;find&lt;/tt&gt;, use the &lt;tt&gt;==&lt;/tt&gt; operator by default. If a class defines &lt;tt&gt;==&lt;/tt&gt;, then these algorithms can be used on that class type without any specialization&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;14.3.2. Relational Operators &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;Classes for which the equality operator is defined also often have relational operators. In particular, because the associative containers and some of the algorithms use the less-than operator, it can be quite useful to define an &lt;tt&gt;operator&amp;lt;&lt;/tt&gt;.&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;Because the logical definition of &lt;tt&gt;&amp;lt;&lt;/tt&gt; is inconsistent with the logical definition of &lt;tt&gt;==&lt;/tt&gt;, it is better not to define &lt;tt&gt;&amp;lt;&lt;/tt&gt; at all. We&#39;ll see in &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch15.html#ch15&quot;&gt;Chapter 15&lt;/a&gt; how to use a separate named function to compare &lt;tt&gt;Sales_items&lt;/tt&gt; when we want to store them in an associative container &lt;br /&gt;&lt;li&gt;&lt;strong&gt;The associative containers, as well as some of the algorithms, use the &lt;tt&gt;&amp;lt;&lt;/tt&gt; operator by default. Ordinarily, the relational operators, like the equality operators, should be defined as nonmember functions&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;&lt;br&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;h4&gt;Section 14.4 Assignment Operators&lt;/h4&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;We covered the assignment of one object of class type to another object of its type in &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch13lev1sec2.html#ch13lev1sec2&quot;&gt;Section 13.2&lt;/a&gt; (p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch13lev1sec2.html#ch13lev1sec2&quot;&gt;482&lt;/a&gt;). The class assignment operator takes a parameter that is the class type. Usually the parameter is a &lt;tt&gt;const&lt;/tt&gt; reference to the class type. However, the parameter could be the class type or a non&lt;tt&gt;const&lt;/tt&gt; reference to the class type. &lt;strong&gt;This operator will be synthesized by the compiler if we do not define it ourselves. The class assignment operator must be a member of the class so the compiler can know whether it needs to synthesize one&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;Additional assignment operators that differ by the type of the right-hand operand can be defined for a class type &lt;br /&gt;&lt;li&gt;&lt;strong&gt;Assignment operators can be overloaded. Unlike the compound-assignment operators, every assignment operator, regardless of parameter type, must be defined as a member function&lt;/strong&gt; &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;div style=&quot;border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px&quot; id=&quot;codeSnippetWrapper&quot;&gt;&lt;br /&gt;&lt;div style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot; id=&quot;codeSnippet&quot;&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&lt;span style=&quot;color: #008000&quot;&gt;// illustration of assignment operators for class string&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; string {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt;:&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    string&amp;amp; &lt;span style=&quot;color: #0000ff&quot;&gt;operator&lt;/span&gt;=(&lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; string &amp;amp;);      &lt;span style=&quot;color: #008000&quot;&gt;// s1 = s2;&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    string&amp;amp; &lt;span style=&quot;color: #0000ff&quot;&gt;operator&lt;/span&gt;=(&lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;char&lt;/span&gt; *);        &lt;span style=&quot;color: #008000&quot;&gt;// s1 = &quot;str&quot;;&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    string&amp;amp; &lt;span style=&quot;color: #0000ff&quot;&gt;operator&lt;/span&gt;=(&lt;span style=&quot;color: #0000ff&quot;&gt;char&lt;/span&gt;);                &lt;span style=&quot;color: #008000&quot;&gt;// s1 = &#39;c&#39;;&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    &lt;span style=&quot;color: #008000&quot;&gt;// ....&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt; };&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;/div&gt;&lt;/div&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;Assignment Should Return a Reference to *this &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;The &lt;tt&gt;string&lt;/tt&gt; assignment operators return a reference to &lt;tt&gt;string&lt;/tt&gt;, which is consistent with assignment for the built-in types. Moreover, because assignment returns a reference there is no need to create and destroy a temporary copy of the result. The return value is usually a reference to the left-hand operand &lt;br /&gt;&lt;li&gt;Ordinarily, assignment operators and compound-assignment operators ought to return a reference to the left-hand operand&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;h4&gt;Section 14.5 Subscript Operator&lt;/h4&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Classes that represent containers from which individual elements can be retrieved usually define the subscript operator, &lt;tt&gt;operator[]&lt;/tt&gt;. The library classes, &lt;tt&gt;string&lt;/tt&gt; and &lt;tt&gt;vector&lt;/tt&gt;, are examples of classes that define the subscript operator&lt;/li&gt;&lt;br /&gt;&lt;li&gt;The subscript operator must be defined as a class member function&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Providing Read and Write Access&lt;/li&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;One complication in defining the subscript operator is that we want it to do the right thing when used as either the left- or right-hand operand of an assignment&lt;/li&gt;&lt;br /&gt;&lt;li&gt;It is also a good idea to be able to subscript &lt;tt&gt;const&lt;/tt&gt; and non&lt;tt&gt;const&lt;/tt&gt; objects. When applied to a &lt;tt&gt;const&lt;/tt&gt; object, the return should be a &lt;tt&gt;const&lt;/tt&gt; reference so that it is not usable as the target of an assignment&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;Ordinarily, a class that defines subscript needs to define two versions: one that is a non&lt;tt&gt;const&lt;/tt&gt; member and returns a reference and one that is a &lt;tt&gt;const&lt;/tt&gt; member and returns a &lt;tt&gt;const&lt;/tt&gt; reference&lt;/strong&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;Prototypical Subscript Operator&lt;/li&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;div style=&quot;border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px&quot; id=&quot;codeSnippetWrapper&quot;&gt;&lt;br /&gt;&lt;div style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot; id=&quot;codeSnippet&quot;&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&lt;span style=&quot;color: #cc6633&quot;&gt;#include&lt;/span&gt; &lt;span style=&quot;color: #006080&quot;&gt;&quot;stdafx.h&quot;&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&lt;span style=&quot;color: #cc6633&quot;&gt;#include&lt;/span&gt; &amp;lt;vector&amp;gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;namespace&lt;/span&gt;  std;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; Foo {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt;:&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    &lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; &amp;amp;&lt;span style=&quot;color: #0000ff&quot;&gt;operator&lt;/span&gt;[] (&lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; size_t);&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    &lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; &amp;amp;&lt;span style=&quot;color: #0000ff&quot;&gt;operator&lt;/span&gt;[] (&lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; size_t) &lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt;;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    &lt;span style=&quot;color: #008000&quot;&gt;// other interface members&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;private&lt;/span&gt;:&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; data;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    &lt;span style=&quot;color: #008000&quot;&gt;// other member data and private utility functions&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;};&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&amp;nbsp;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;amp; Foo::&lt;span style=&quot;color: #0000ff&quot;&gt;operator&lt;/span&gt;[] (&lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; size_t index)&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;{&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; data[index];  &lt;span style=&quot;color: #008000&quot;&gt;// no range checking on index&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;}&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;amp; Foo::&lt;span style=&quot;color: #0000ff&quot;&gt;operator&lt;/span&gt;[] (&lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; size_t index) &lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;{&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; data[index];  &lt;span style=&quot;color: #008000&quot;&gt;// no range checking on index&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;}&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;/div&gt;&lt;/div&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;br /&gt;&lt;h4&gt;Section 14.6 Member Access Operators&lt;/h4&gt;&lt;br /&gt;&lt;p&gt;2011.2.21 here&lt;/p&gt;&lt;br /&gt;&lt;h4&gt;Section 14.7 Increment and Decrement Operators&lt;/h4&gt;&lt;br /&gt;&lt;h4&gt;Section 14.8 Call Operator and Function Objects&lt;/h4&gt;&lt;br /&gt;&lt;h4&gt;Section 14.9 Conversions and Class Types&lt;/h4&gt;  </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/4293082337568658882/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/4293082337568658882?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/4293082337568658882'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/4293082337568658882'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2011/02/c-primer-fourth-editionnotes-chapter-14.html' title='C++ Primer, Fourth Edition:Notes Chapter 14. Overloaded Operations and Conversions'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-9147374267378666200</id><published>2011-02-15T01:37:00.003-08:00</published><updated>2011-02-15T01:37:29.328-08:00</updated><title type='text'>C++ Primer, Fourth Edition:Notes Chapter 17. Tools for Large Programs</title><content type='html'>&lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;h3&gt;Section 17.1 Exception Handling&lt;/h3&gt; &lt;h3&gt;Section 17.2 Namespaces&lt;/h3&gt; &lt;h3&gt;Section 17.3 Multiple and Virtual Inheritance&lt;/h3&gt;  </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/9147374267378666200/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/9147374267378666200?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/9147374267378666200'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/9147374267378666200'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2011/02/c-primer-fourth-editionnotes-chapter-17.html' title='C++ Primer, Fourth Edition:Notes Chapter 17. Tools for Large Programs'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-3281724949148104286</id><published>2011-02-15T01:37:00.001-08:00</published><updated>2011-02-15T01:37:03.277-08:00</updated><title type='text'>C++ Primer, Fourth Edition:Notes Chapter 16. Templates and Generic Programming</title><content type='html'>&lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;h3&gt;Section 16.1 Template Definitions&lt;/h3&gt; &lt;h3&gt;Section 16.2 Instantiation&lt;/h3&gt; &lt;h3&gt;Section 16.3 Template Compilation Models&lt;/h3&gt; &lt;h3&gt;Section 16.4 Class Template Members&lt;/h3&gt; &lt;h3&gt;Section 16.5 A Generic Handle Class&lt;/h3&gt; &lt;h3&gt;Section 16.6 Template Specializations&lt;/h3&gt; &lt;h3&gt;Section 16.7 Overloading and Function Templates&lt;/h3&gt;  </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/3281724949148104286/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/3281724949148104286?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/3281724949148104286'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/3281724949148104286'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2011/02/c-primer-fourth-editionnotes-chapter-16.html' title='C++ Primer, Fourth Edition:Notes Chapter 16. Templates and Generic Programming'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-8529451519608877302</id><published>2011-02-15T01:36:00.001-08:00</published><updated>2011-02-15T01:36:51.966-08:00</updated><title type='text'>C++ Primer, Fourth Edition:Notes Chapter 15. Object-Oriented Programming</title><content type='html'>&lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;h3&gt;Section 15.1 OOP: An Overview&lt;/h3&gt; &lt;h3&gt;Section 15.2 Defining Base and Derived Classes&lt;/h3&gt; &lt;h3&gt;Section 15.3 Conversions and Inheritance&lt;/h3&gt; &lt;h3&gt;Section 15.4 Constructors and Copy Control&lt;/h3&gt; &lt;h3&gt;Section 15.5 Class Scope under Inheritance&lt;/h3&gt; &lt;h3&gt;Section 15.6 Pure Virtual Functions&lt;/h3&gt; &lt;h3&gt;Section 15.7 Containers and Inheritance&lt;/h3&gt; &lt;h3&gt;Section 15.8 Handle Classes and Inheritance&lt;/h3&gt; &lt;h3&gt;Section 15.9 Text Queries Revisited&lt;/h3&gt;  </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/8529451519608877302/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/8529451519608877302?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/8529451519608877302'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/8529451519608877302'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2011/02/c-primer-fourth-editionnotes-chapter-15.html' title='C++ Primer, Fourth Edition:Notes Chapter 15. Object-Oriented Programming'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-207753371741175569</id><published>2011-02-15T01:34:00.003-08:00</published><updated>2011-02-15T01:34:53.492-08:00</updated><title type='text'>C++ Primer, Fourth Edition:Notes Chapter Chapter 18. Specialized Tools and Techniques</title><content type='html'>&lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;h3&gt;Section 18.1 Optimizing Memory Allocation&lt;/h3&gt; &lt;h3&gt;Section 18.2 Run-Time Type Identification&lt;/h3&gt; &lt;h3&gt;Section 18.3 Pointer to Class Member&lt;/h3&gt; &lt;h3&gt;Section 18.4 Nested Classes&lt;/h3&gt; &lt;h3&gt;Section 18.5 Union: A Space-Saving Class&lt;/h3&gt; &lt;h3&gt;Section 18.6 Local Classes&lt;/h3&gt; &lt;h3&gt;Section 18.7 Inherently Nonportable Features&lt;/h3&gt;  </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/207753371741175569/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/207753371741175569?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/207753371741175569'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/207753371741175569'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2011/02/c-primer-fourth-editionnotes-chapter.html' title='C++ Primer, Fourth Edition:Notes Chapter Chapter 18. Specialized Tools and Techniques'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-4081846711102593636</id><published>2011-02-10T02:06:00.001-08:00</published><updated>2011-02-15T01:09:18.562-08:00</updated><title type='text'>C++ Primer, Fourth Edition:Notes Chapter 13. Copy Control</title><content type='html'>&lt;ul&gt; &lt;li&gt;Each type, whether a built-in or class type, defines the meaning of a (possibly empty) set of operations on objects of that type  &lt;li&gt;Each type also defines what happens when objects of the type are created. Initialization of objects of class type is defined by constructors. Types also control what happens when objects of the type are copied, assigned, or destroyed. Classes control these actions through special member functions: the copy constructor, the assignment operator, and the destructor. This chapter covers these operations  &lt;li&gt;When we define a new type, we specifyexplicitly or implicitlywhat happens when objects of that type are copied, assigned, and destroyed. We do so by defining special members: the copy constructor, the assignment operator, and the destructor. If we do not explicitly define the copy constructor or the assignment operator, the compiler will (usually) define them for us  &lt;li&gt;&lt;strong&gt;The &lt;/strong&gt;&lt;a name=&quot;ch13term2&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch13lev1sec7.html#gloss13_02&quot;&gt;&lt;strong&gt;copy constructor&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt; is a special constructor that has a single parameter that is a (usually &lt;tt&gt;const&lt;/tt&gt;) reference to the class type. The copy constructor is used explicitly when we define a new object and initialize it from an object of the same type. It is used implicitly when we pass or return objects of that type to or from functions&lt;/strong&gt;  &lt;li&gt;The &lt;b&gt;&lt;a name=&quot;ch13term4&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch13lev1sec7.html#gloss13_04&quot;&gt;destructor&lt;/a&gt;&lt;/b&gt; is complementary to the constructors: It is applied automatically when an object goes out of scope or when a dynamically allocated object is deleted. The destructor is used to free resources acquired when the object was constructed or during the lifetime of the object. Regardless of whether a class defines its own destructor, the compiler automatically executes the destructors for the non&lt;tt&gt;static&lt;/tt&gt; data members of the class  &lt;li&gt;Like constructors, the assignment operator may be overloaded by specifying different types for the right-hand operand. The version whose right-hand operand is of the class type is special: If we do not write one, the compiler will synthesize one for us  &lt;li&gt;Collectively, the copy constructor, assignment operator, and destructor are referred to as &lt;b&gt;&lt;a name=&quot;ch13term3&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch13lev1sec7.html#gloss13_03&quot;&gt;copy control&lt;/a&gt;&lt;/b&gt;. The compiler automatically implements these operations, but the class may define its own versions  &lt;li&gt;Copy control is an essential part of defining any C++ class. Programmers new to C++ are often confused by having to define what happens when objects are copied, assigned, or destroyed. This confusion is compounded because if we do not explicitly define these operations, the compiler defines them for usalthough they might not behave as we intend  &lt;li&gt;Often the compiler-synthesized copy-control functions are finethey do exactly the work that needs to be done. But for some classes, relying on the default definitions leads to disaster. &lt;strong&gt;Frequently, the most difficult part of implementing the copy-control operations is recognizing when we need to override the default versions. One especially common case that requires the class to define its own the copy-control members is if the class has a pointer member&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt; &lt;h3&gt;Section 13.1 The Copy Constructor &lt;/h3&gt; &lt;ul&gt; &lt;li&gt;The constructor that takes a single parameter that is a (usually &lt;tt&gt;const&lt;/tt&gt;) reference to an object of the class type itself is called the copy constructor. Like the default constructor, the copy constructor can be implicitly invoked by the compiler. &lt;strong&gt;The copy constructor is used to&lt;/strong&gt;  &lt;ul&gt; &lt;li&gt;&lt;strong&gt;Explicitly or implicitly initialize one object from another of the same type&lt;/strong&gt;  &lt;li&gt;&lt;strong&gt;Copy an object to pass it as an argument to a function&lt;/strong&gt;  &lt;li&gt;&lt;strong&gt;Copy an object to return it from a function&lt;/strong&gt;  &lt;li&gt;&lt;strong&gt;Initialize the elements in a sequential container&lt;/strong&gt;  &lt;li&gt;&lt;strong&gt;Initialize elements in an array from a list of element initializers&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Forms of Object Definition  &lt;ul&gt; &lt;li&gt;&lt;strong&gt;Recall that C++ supports two forms of initialization (&lt;/strong&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch02lev1sec3.html#ch02lev2sec12&quot;&gt;&lt;strong&gt;Section 2.3.3&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;, p. &lt;/strong&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch02lev1sec3.html#ch02lev2sec12&quot;&gt;&lt;strong&gt;48&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;): direct and copy. Copy-initialization uses the = symbol, and direct-initialization places the initializer in parentheses&lt;/strong&gt;  &lt;li&gt;The copy and direct forms of initialization, when applied to objects of class type, are subtly different. Direct-initialization directly invokes the constructor matched by the arguments. Copy-initialization always involves the copy constructor. Copy-initialization first uses the indicated constructor to create a temporary object (&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch07lev1sec3.html#ch07lev2sec14&quot;&gt;Section 7.3.2&lt;/a&gt;, p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch07lev1sec3.html#ch07lev2sec14&quot;&gt;247&lt;/a&gt;). It then uses the copy constructor to copy that temporary into the one we are creating  &lt;li&gt;&lt;strong&gt;For objects of class type, copy-initialization can be used only when specifying a single argument or when we explicitly build a temporary object to copy&lt;/strong&gt;  &lt;li&gt;&lt;strong&gt;The copy form of initialization is primarily supported for compatibility with C usage. When it can do so, the compiler is permitted (but not obligated) to skip the copy constructor and create the object directly&lt;/strong&gt;  &lt;li&gt;Usually the difference between direct- or copy-initialization is at most a matter of low-level optimization. However, for types that do not support copying, or when using a constructor that is non&lt;tt&gt;explicit&lt;/tt&gt; (&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch12lev1sec4.html#ch12lev2sec23&quot;&gt;Section 12.4.4&lt;/a&gt;, p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch12lev1sec4.html#ch12lev2sec23&quot;&gt;462&lt;/a&gt;) the distinction can be essential&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Parameters and Return Values  &lt;ul&gt; &lt;li&gt;As we know, when a parameter is a nonreference type (&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch07lev1sec2.html#ch07lev2sec6&quot;&gt;Section 7.2.1&lt;/a&gt;, p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch07lev1sec2.html#ch07lev2sec6&quot;&gt;230&lt;/a&gt;), the argument is copied. Similarly, a nonreference return value (&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch07lev1sec3.html#ch07lev2sec14&quot;&gt;Section 7.3.2&lt;/a&gt;, p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch07lev1sec3.html#ch07lev2sec14&quot;&gt;247&lt;/a&gt;) is returned by copying the value in the &lt;tt&gt;return&lt;/tt&gt; statement  &lt;li&gt;&lt;strong&gt;When the parameter or return type is a class type, the copy is done by the copy constructor&lt;/strong&gt;  &lt;li&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Initializing Container Elements  &lt;ul&gt; &lt;li&gt;The copy constructor is used to initialize the elements in a sequential container  &lt;li&gt;&lt;strong&gt;As a general rule (&lt;/strong&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch09lev1sec1.html#ch09lev2sec1&quot;&gt;&lt;strong&gt;Section 9.1.1&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;, p. &lt;/strong&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch09lev1sec1.html#ch09lev2sec1&quot;&gt;&lt;strong&gt;307&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;), unless you intend to use the default initial value of the container elements, it is more efficient to allocate an empty container and add elements as the values for those elements become known&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Constructors and Array Elements  &lt;ul&gt; &lt;li&gt;If we provide no element initializers for an array of class type, then the default constructor is used to initialize each element. However, if we provide explicit element initializers using the normal brace-enclosed array initialization list (&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch04lev1sec1.html#ch04lev2sec1&quot;&gt;Section 4.1.1&lt;/a&gt;, p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch04lev1sec1.html#ch04lev2sec1&quot;&gt;111&lt;/a&gt;), then each element is initialized using copy-initialization. An element of the appropriate type is created from the specified value, and then the copy constructor is used to copy that value to the corresponding element  &lt;li&gt;&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;13.1.1. The Synthesized Copy Constructor  &lt;ul&gt; &lt;li&gt;If we do not otherwise define the copy constructor, the compiler synthesizes one for us. Unlike the synthesized default constructor (&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch12lev1sec4.html#ch12lev2sec22&quot;&gt;Section 12.4.3&lt;/a&gt;, p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch12lev1sec4.html#ch12lev2sec22&quot;&gt;458&lt;/a&gt;), a copy constructor is synthesized even if we define other constructors. The behavior of the &lt;b&gt;&lt;a name=&quot;ch13term12&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch13lev1sec7.html#gloss13_12&quot;&gt;synthesized copy constructor&lt;/a&gt;&lt;/b&gt; is to &lt;b&gt;&lt;a name=&quot;ch13term6&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch13lev1sec7.html#gloss13_06&quot;&gt;memberwise initialize&lt;/a&gt;&lt;/b&gt; the new object as a copy of the original object  &lt;li&gt;&lt;strong&gt;By memberwise, we mean that taking each non&lt;tt&gt;static&lt;/tt&gt; member in turn, the compiler copies the member from the existing object into the one being created. With one exception, the type of each member determines what it means to copy it. The synthesized copy constructor directly copies the value of members of built-in type. Members of class type are copied by using the copy constructor for that class. The one exception concerns array members. Even though we ordinarily &lt;/strong&gt;&lt;a name=&quot;idd1e99476&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e99479&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e99484&quot;&gt;&lt;/a&gt;&lt;strong&gt;cannot copy an array, if a class has a member that is an array, then the synthesized copy constructor will copy the array. It does so by copying each element&lt;/strong&gt;  &lt;li&gt;The simplest conceptual model of memberwise initialization is to think of the synthesized copy constructor as one in which each data member is initialized in the constructor initializer list  &lt;li&gt;&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;13.1.2. Defining Our Own Copy Constructor  &lt;ul&gt; &lt;li&gt;The copy constructor is the constructor that takes a single parameter that is a (usually &lt;tt&gt;const&lt;/tt&gt;) reference to the class type  &lt;div style=&quot;border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px&quot; id=&quot;codeSnippetWrapper&quot;&gt; &lt;div style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot; id=&quot;codeSnippet&quot;&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; Foo {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt;:&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;        Foo();           &lt;span style=&quot;color: #008000&quot;&gt;// default constructor&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;        Foo(&lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; Foo&amp;amp;); &lt;span style=&quot;color: #008000&quot;&gt;// copy constructor&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;        &lt;span style=&quot;color: #008000&quot;&gt;// ...&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;     };&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;li&gt;Usually the parameter is a &lt;tt&gt;const&lt;/tt&gt; reference, although we can also define the copy constructor to take a non&lt;tt&gt;const&lt;/tt&gt; reference. &lt;strong&gt;Because the constructor is used (implicitly) to pass and return objects to and from functions, it usually should not be made &lt;tt&gt;explicit&lt;/tt&gt; (&lt;/strong&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch12lev1sec4.html#ch12lev2sec23&quot;&gt;&lt;strong&gt;Section 12.4.4&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;, p. &lt;/strong&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch12lev1sec4.html#ch12lev2sec23&quot;&gt;&lt;strong&gt;462&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;).&lt;/strong&gt; The copy constructor should copy the members from its argument into the object that is being constructed &lt;br /&gt;&lt;li&gt;&lt;strong&gt;For many classes, the synthesized copy constructor does exactly the work that is needed. Classes that contain only members that are of class type or members that are of built-in (but not pointer type) often can be copied without explicitly defining the copy constructor&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;However, some classes must take control of what happens when objects are copied. Such classes often have a data member that is a pointer or that represents another resource that is allocated in the constructor. Other classes have bookkeeping that must be done whenever a new object is created. In both these cases, the copy constructor must be defined&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;Often the hardest part about defining a copy constructor is recognizing that a copy constructor is needed. Defining the constructor is usually pretty easy once the need for the constructor is recognized. The copy constructor itself is defined like any other constructor: It has the same name as the name of the class, it has no return value, it may (should) use a constructor initializer to initialize the members of the newly created object, and it may do any other necessary work inside a function body&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;13.1.3. Preventing Copies &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Some classes need to prevent copies from being made at all &lt;br /&gt;&lt;li&gt;&lt;strong&gt;To prevent copies, a class must explicitly declare its copy constructor as &lt;tt&gt;private&lt;/tt&gt;&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;If the copy constructor is private, then user code will not be allowed to copy objects of the class type. The compiler will reject any attempt to make a copy &lt;br /&gt;&lt;li&gt;&lt;strong&gt;However, the friends and members of the class could still make copies. If we want to prevent copies even within the friends and members, we can do so by declaring a (&lt;tt&gt;private&lt;/tt&gt;) copy constructor but not defining it&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;Most Classes Should Define Copy and Default Constructors &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Classes that do not define the default constructor and/or the copy constructor impose serious limits on users of the class. Objects of classes that do not allow copies may be passed to (or returned from) a function only as a reference. They also may not be used as elements in a container &lt;br /&gt;&lt;li&gt;&lt;strong&gt;It is usually best to defineeither implicitly or explicitlythe default and copy constructors. The default constructor is synthesized only if there are no other constructors. If the copy constructor is defined, then the default constructor must be defined as well&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;h3&gt;Section 13.2 The Assignment Operator&lt;/h3&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Just as classes control how objects are initialized, they also define what happens when objects of their type are assigned &lt;br /&gt;&lt;li&gt;&lt;strong&gt;As with the copy constructor, the compiler synthesizes an assignment operator if the class does not define its own&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;Introducing Overloaded Assignment &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Overloaded operators are functions that have the name &lt;tt&gt;operator&lt;/tt&gt; followed by the symbol for the operator being defined. Hence, we define assignment by defining a function named &lt;tt&gt;operator=&lt;/tt&gt;. Like any other function, an operator function has a return type and a parameter list. &lt;strong&gt;The parameter list must have the same number of parameters (including the implicit &lt;tt&gt;this&lt;/tt&gt; parameter if the operator is a &lt;/strong&gt;&lt;a name=&quot;idd1e99861&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e99868&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e99877&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e99882&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e99890&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e99900&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e99908&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e99916&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e99921&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e99924&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e99931&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e99940&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e99948&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e99954&quot;&gt;&lt;/a&gt;&lt;strong&gt;member) as the operator has operands&lt;/strong&gt;. Assignment is binary, so the operator function has two parameters: The first parameter corresponds to the left-hand operand, and the second to the right-hand operand &lt;br /&gt;&lt;li&gt;Most operators may be defined as member or nonmember functions. When an operator is a member function, its first operand is implicitly bound to the &lt;tt&gt;this&lt;/tt&gt; pointer. Some operators, assignment among them, must be members of the class for which the operator is defined. &lt;strong&gt;Because assignment must be a member of its class, &lt;tt&gt;this&lt;/tt&gt; is bound to a pointer to the left-hand operand. The assignment operator, therefore, takes a single parameter that is an object of the same class type. Usually, the right-hand operand is passed as a &lt;tt&gt;const&lt;/tt&gt; reference&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;The return type from the assignment operator should be the same as the return from assignment for the built-in types&lt;/strong&gt; (&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch05lev1sec4.html#ch05lev2sec7&quot;&gt;Section 5.4.1&lt;/a&gt;, p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch05lev1sec4.html#ch05lev2sec7&quot;&gt;160&lt;/a&gt;). Assignment to a built-in type returns a reference to its left-hand operand. Therefore, the assignment operator also returns a reference to the same type as its class&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;The Synthesized Assignment Operator &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;The &lt;b&gt;&lt;a name=&quot;ch13term11&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch13lev1sec7.html#gloss13_11&quot;&gt;synthesized assignment operator&lt;/a&gt;&lt;/b&gt; operates similarly to the synthesized copy constructor. It performs &lt;b&gt;&lt;a name=&quot;ch13term5&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch13lev1sec7.html#gloss13_05&quot;&gt;memberwise assignment&lt;/a&gt;&lt;/b&gt;: Each member of the right-hand object is assigned to the corresponding member of the left-hand object. Except for arrays, each member is assigned in the usual way for its type. For arrays, each array element is assigned &lt;br /&gt;&lt;li&gt;The synthesized assignment operator assigns each member in turn, using the built-in or class-defined assignment operator as appropriate to the type of the member. The operator returns &lt;tt&gt;*this&lt;/tt&gt;, which is a reference to the left-hand object&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;Copy and Assign Usually Go Together &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;Classes that can use the synthesized copy constructor usually can use the synthesized assignment operator as well&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;However, a class may define its own assignment operator. In general, if a class needs a copy constructor, it will also need an assignment operator&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;In fact, these operations should be thought of as a unit. If we require one, we almost surely require the other&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;h3&gt;Section 13.3 The Destructor&lt;/h3&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;The destructor is a special member function that can be used to do whatever resource deallocation is needed. It serves as the complement to the constructors of the class &lt;br /&gt;&lt;li&gt;When a Destructor Is Called &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;The destructor is called automatically whenever an object of its class is destroyed &lt;br /&gt;&lt;li&gt;&lt;strong&gt;An object that is dynamically allocated is destroyed only when a pointer pointing to the object is &lt;tt&gt;delete&lt;/tt&gt; d. If we do not &lt;tt&gt;delete&lt;/tt&gt; a pointer to a dynamically allocated object, then the destructor is never run on that object. The object will persist forever, leading to a memory leak. Moreover, any resources used inside the object will also not be released&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;The destructor is not run when a reference or a pointer to an object goes out of scope. The destructor is run only when a pointer to a dynamically allocated object is &lt;tt&gt;delete&lt;/tt&gt;d or when an actual object (not a reference to the object) goes out of scope &lt;br /&gt;&lt;li&gt;&lt;strong&gt;Destructors are also run on the elements of class type in a container whether a library container or built-in array when the container is destroyed&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;The elements in the container are always destroyed in reverse order: The element indexed by &lt;tt&gt;size() - 1&lt;/tt&gt; is destroyed first, followed by the one indexed by &lt;tt&gt;size() - 2&lt;/tt&gt; and so on until element &lt;tt&gt;[0]&lt;/tt&gt;, which is destroyed last&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;When to Write an Explicit Destructor &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Many classes do not require an explicit destructor. In particular, a class that has a constructor does not necessarily need to define its own destructor. Destructors are needed only if there is work for them to do. Ordinarily they are used to relinquish resources acquired in the constructor or during the lifetime of the object &lt;br /&gt;&lt;li&gt;&lt;strong&gt;A useful rule of thumb is that if a class needs a destructor, it will also need the assignment operator and a copy constructor. This rule is often referred to as the &lt;/strong&gt;&lt;a name=&quot;ch13term9&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch13lev1sec7.html#gloss13_09&quot;&gt;&lt;strong&gt;Rule of Three&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;, indicating that if you need a destructor, then you need all three copy-control members&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;A destructor is not limited only to relinquishing resources. A destructor, in general, can perform any operation that the class designer wishes to have executed subsequent to the last use of an object of that class&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;The Synthesized Destructor &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Unlike the copy constructor or assignment operator, the compiler always synthesizes a destructor for us. The synthesized destructor destroys each non&lt;tt&gt;static&lt;/tt&gt; member in the reverse order from that in which the object was created. In consequence, it destroys the members in reverse order from which they are declared in the class. For each member that is of class type, the synthesized destructor invokes that member&#39;s destructor to destroy the object &lt;br /&gt;&lt;li&gt;Destroying a member of built-in or compound type has no effect. In particular, the synthesized destructor does not &lt;tt&gt;delete&lt;/tt&gt; the object pointed to by a pointer member&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;How to Write a Destructor &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Classes that do allocate resources usually need to define a destructor to free those resources. The destructor is a member function with the name of the class prefixed by a tilde (&lt;tt&gt;~&lt;/tt&gt;). It has no return value and takes no parameters. Because it cannot specify any parameters, it cannot be overloaded. Although we can define multiple class constructors, we can provide only a single destructor to be applied to all objects of our class &lt;br /&gt;&lt;li&gt;An important difference between the destructor and the copy constructor or assignment operator is that even if we write our own destructor, the synthesized destructor is still run&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;h3&gt;Section 13.4 A Message-Handling Example&lt;/h3&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;The Message Class&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Copy Control for the Message Class&lt;/li&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;When we write our own copy constructor, we must explicitly copy any members that we want copied. An explicitly defined copy constructor copies nothing automatically&lt;/li&gt;&lt;br /&gt;&lt;li&gt;As with any other constructor, if we do not initialize a class member, then that member is initialized using the member&#39;s default constructor. Default initialization in a copy constructor does not use the member&#39;s copy constructor&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;The put_Msg_in_Folders Member&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Message Assignment Operator&lt;/li&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;The assignment operator starts by checking that the left- and right-hand operands are not the same. We do this check for reasons that will become apparent as we walk through the rest of the function&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Assignment involves obliterating the left-hand operand. Once the members of the left-hand operand are destroyed, those in the right-hand operand are assigned to the corresponding left-hand members. If the objects were the same, then destroying the left-hand members would also destroy the right-hand members&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;It is crucially important for assignment operators to work correctly, even when an object is assigned to iself. A common way to ensure this behavior is by checking explicitly for self-assignment&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;The remove_Msg_from_Folders Member&lt;/li&gt;&lt;br /&gt;&lt;li&gt;The Message Destructor&lt;/li&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;The assignment operator often does the same work as is needed in the copy constructor and destructor. In such cases, the common work should be put in &lt;tt&gt;private&lt;/tt&gt; utility functions&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;h3&gt;Section 13.5 Managing Pointer Members&lt;/h3&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Classes that contain pointers require careful attention to copy control. The reason they must do so is that copying a pointer copies only the address in the pointer. Copying a pointer does not copy the object to which the pointer points.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;When designing a class with a pointer member, the first decision a class author must make is what behavior that pointer should provide. When we copy one pointer to another, the two pointers point to the same object. When two pointers point to the same object, it is possible to use either pointer to change the underlying object. Similarly, it is possible for one pointer to &lt;tt&gt;delete&lt;/tt&gt; the object even though the user of the other pointer still thinks the underlying object exists&lt;/strong&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;By default, a pointer member has the same behavior as a pointer object. However, through different copy-control strategies we can implement different behavior for pointer members&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Most C++ classes take one of three approaches to managing pointer members&lt;/li&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;The pointer member can be given normal pointer like behavior. Such classes will have all the pitfalls of pointers but will require no special copy control.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;The class can implement so-called &quot;smart pointer&quot; behavior. The object to which the pointer points is shared, but the class prevents dangling pointers.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;The class can be given value like behavior. The object to which the pointer points will be unique to and managed separately by each class object.&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;In this section we look at three classes that implement each of these different approaches to managing their pointer members.&lt;/strong&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;A Simple Class with a Pointer Member&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Default Copy/Assignment and Pointer Members&lt;/li&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Classes that have pointer members and use default synthesized copy control have all the pitfalls of ordinary pointers. In particular, the class itself has no way to avoid dangling pointers&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;Pointers Share the Same Object&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Dangling Pointers Are Possible&lt;/li&gt;&lt;br /&gt;&lt;li&gt;13.5.1. Defining Smart Pointer Classes&lt;/li&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;An alternative to having a pointer member behave exactly like a pointer is to define what is sometimes referred to as a smart pointer class. A smart pointer behaves like an ordinary pointer except that it adds functionality. &lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;In this case, we&#39;ll give our smart pointer the responsibility for deleting the shared object. Users will dynamically allocate an object and pass the address of that object to our new HasPtr class. The user may still access the object through a plain pointer but must not delete the pointer. The HasPtr class will ensure that the object is deleted when the last HasPtr that points to it is destroyed.&lt;/strong&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;Our new &lt;tt&gt;HasPtr&lt;/tt&gt; class will need a destructor to delete the pointer. However, the destructor cannot delete the pointer unconditionally. If two &lt;tt&gt;HasPtr&lt;/tt&gt; objects point to the same underlying object, we don&#39;t want to delete the object until both objects are destroyed. To write the destructor, we need to know whether this &lt;tt&gt;HasPtr&lt;/tt&gt; is the last one pointing to a given object&lt;/strong&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Introducing Use Counts&lt;/li&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;A common technique used in defining smart pointers is to use a &lt;b&gt;&lt;a name=&quot;ch13term13&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch13lev1sec7.html#gloss13_13&quot;&gt;use count&lt;/a&gt;&lt;/b&gt;. The pointerlike class associates a counter with the object to which the class points. The use count keeps track of how many objects of the class share the same pointer. When the use count goes to zero, then the object is deleted. A use count is sometimes also referred to as a &lt;b&gt;&lt;a name=&quot;ch13term8&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch13lev1sec7.html#gloss13_08&quot;&gt;reference count&lt;/a&gt;&lt;/b&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;Each time a new object of the class is created, the pointer is initialized and the use count is set to 1. When an object is created as a copy of another, the copy constructor copies the pointer and increments the associated use count. When an object is assigned to, the assignment operator decrements the use count of the object to which the left-hand operand points (and deletes that object if the use count goes to zero) and increments the use count of the object pointed to by the right-hand operand. Finally, when the destructor is called, it decrements the use count and deletes the underlying object if the count goes to zero&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;The Use-Count Class&lt;/li&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;There are two classic strategies for implementing a use count, one of which we will use here; the other approach is described in &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch15lev1sec8.html#ch15lev2sec21&quot;&gt;Section 15.8.1&lt;/a&gt; (p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch15lev1sec8.html#ch15lev2sec21&quot;&gt;599&lt;/a&gt;). In the approach we use here, we&#39;ll define a separate concrete class to encapsulate the use count and the associated pointer&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;Using the Use-Counted Class&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Assignment and Use Counts&lt;/li&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;This assignment operator guards against self-assignment by incrementing the use count of &lt;tt&gt;rhs&lt;/tt&gt; before decrementing the use count of the left-hand operand&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;Changing Other Members&lt;/li&gt;&lt;br /&gt;&lt;h2 style=&quot;text-transform: none&quot; class=&quot;docSidebarTitle&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;&lt;font style=&quot;font-size: 18pt&quot; color=&quot;#000000&quot;&gt;&lt;font style=&quot;font-weight: bold&quot;&gt;Advice: Managing Pointer Members&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/h2&gt;&lt;br /&gt;&lt;p style=&quot;line-height: normal&quot; class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;&lt;font style=&quot;font-size: 12pt&quot; color=&quot;#000000&quot;&gt;Objects with pointer members often need to define the copy-control members. If we rely on the synthesized versions, then the class puts a burden on its users. Users must ensure that the object to which the member points stays around for at least as long as the object that points to it does.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style=&quot;line-height: normal&quot; class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;&lt;font style=&quot;font-size: 12pt&quot; color=&quot;#000000&quot;&gt;To manage a class with pointer members, we must define all three copy-control members: the copy constructor, assignment operator, and the destructor. These members can define either pointerlike or valuelike behavior for the pointer member.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style=&quot;line-height: normal&quot; class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;&lt;font style=&quot;font-size: 12pt&quot; color=&quot;#000000&quot;&gt;Valuelike classes give each object its own copy of the underlying values pointed to by pointer members. The copy constructor allocates a new element and copies the value from the object it is copying. The assignment operator destroys the existing object it holds and copies the value from its right-hand operand into its left-hand operand. The destructor destroys the object.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style=&quot;line-height: normal&quot; class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font color=&quot;#000000&quot; face=&quot;Times New Roman&quot;&gt;&lt;font style=&quot;font-size: 12pt&quot;&gt;As an alternative to defining either valuelike behavior or pointerlike behavior some classes are so-called &quot;smart pointers.&quot; These classes share the same underlying value between objects, thus providing pointerlike behavior. But they use copy-control techniques to avoid some of the pitfalls of regular pointers. To implement smart pointer behavior, a class needs to ensure that the underlying object stays around until the last copy goes away. Use counting (&lt;/font&gt;&lt;/font&gt;&lt;font style=&quot;font-size: 12pt&quot;&gt;&lt;a class=&quot;docLink&quot; href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch13lev1sec5.html#ch13lev2sec25&quot;&gt;&lt;font color=&quot;#0066cc&quot; face=&quot;Times New Roman&quot;&gt;&lt;u&gt;Section 13.5.1&lt;/u&gt;&lt;/font&gt;&lt;/a&gt;&lt;font color=&quot;#000000&quot; face=&quot;Times New Roman&quot;&gt;, p. &lt;/font&gt;&lt;a class=&quot;docLink&quot; href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch13lev1sec5.html#ch13lev2sec25&quot;&gt;&lt;font color=&quot;#0066cc&quot; face=&quot;Times New Roman&quot;&gt;&lt;u&gt;495&lt;/u&gt;&lt;/font&gt;&lt;/a&gt;&lt;/font&gt;&lt;font face=&quot;Times New Roman&quot;&gt;&lt;font style=&quot;font-size: 12pt&quot; color=&quot;#000000&quot;&gt;), is a common technique for managing smart pointer classes. Each copy of the same underlying value is given a use count. The copy constructor copies the pointer from the old object into the new one and increments the use count. The assignment operator decrements the use count of the left-hand operand and increments the count of the right-hand operand. If the use count of the left-hand operand goes to zero, the assignment operator must delete the object to which it points. Finally, the assignment operator copies the pointer from the right-hand operand into its left-hand operand. The destructor decrements the use count and deletes the underlying object if the count goes to zero.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;a style=&quot;line-height: normal&quot; name=&quot;ch13note14&quot;&gt;&lt;/a&gt;&lt;br /&gt;&lt;div style=&quot;line-height: normal&quot; class=&quot;docNote&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; width=&quot;90%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot; width=&quot;60&quot;&gt;&lt;img border=&quot;0&quot; alt=&quot;&quot; align=&quot;left&quot; src=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/images/0201721481/graphics/beware.jpg;400478&quot; width=&quot;57&quot; height=&quot;96&quot;&gt;&lt;/td&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font color=&quot;#000000&quot; face=&quot;Times New Roman&quot;&gt;These approaches to managing pointers occur so frequently that programmers who use classes with pointer members must be thoroughly familiar with these programming techniques.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/div&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;13.5.2. Defining Value like Classes&lt;/li&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;A completely different approach to the problem of managing pointer members is to give them &lt;b&gt;&lt;a name=&quot;ch13term14&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch13lev1sec7.html#gloss13_14&quot;&gt;value semantics&lt;/a&gt;&lt;/b&gt;. Simply put, classes with value semantics define &lt;a name=&quot;idd1e103008&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e103013&quot;&gt;&lt;/a&gt;objects that behave like the arithmetic types: When we copy a valuelike object, we get a new, distinct copy. Changes made to the copy are not reflected in the original, and vice versa. The &lt;tt&gt;string&lt;/tt&gt; class is an example of a valuelike class&lt;/li&gt;&lt;br /&gt;&lt;li&gt;The copy constructor no longer copies the pointer. It now allocates a new &lt;tt&gt;int&lt;/tt&gt; object and initializes that object to hold the same value as the object of which it is a copy. Each object always holds its own, distinct copy of its &lt;tt&gt;int&lt;/tt&gt; value. Because each object holds its own copy, the destructor unconditionally deletes the pointer&lt;/li&gt;&lt;br /&gt;&lt;li&gt;As always, the assignment operator must be correct even if we&#39;re assigning an object to itself. In this case, the operations are inherently safe even if the left- and right-hand objects are the same. Thus, there is no need to explicitly check for self-assignment&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ul&gt;  </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/4081846711102593636/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/4081846711102593636?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/4081846711102593636'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/4081846711102593636'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2011/02/c-primer-fourth-editionnotes-chapter-13.html' title='C++ Primer, Fourth Edition:Notes Chapter 13. Copy Control'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-1532721550168903930</id><published>2011-02-09T21:08:00.001-08:00</published><updated>2011-02-09T21:08:33.185-08:00</updated><title type='text'>C++ Primer, Fourth Edition:Notes Chapter 11. Generic Algorithms</title><content type='html'>&lt;h4&gt;Section 11.1 Overview &lt;/h4&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;h4&gt;Section 11.2 A First Look at the Algorithms&lt;/h4&gt; &lt;h4&gt;Section 11.3 Revisiting Iterators&lt;/h4&gt; &lt;h4&gt;Section 11.4 Structure of Generic Algorithms&lt;/h4&gt; &lt;h4&gt;Section 11.5 Container-Specific Algorithms&lt;/h4&gt;  </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/1532721550168903930/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/1532721550168903930?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/1532721550168903930'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/1532721550168903930'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2011/02/c-primer-fourth-editionnotes-chapter-11.html' title='C++ Primer, Fourth Edition:Notes Chapter 11. Generic Algorithms'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-8243013355595444074</id><published>2011-02-09T01:55:00.001-08:00</published><updated>2011-02-09T01:55:21.967-08:00</updated><title type='text'>Good windows live writer code format plugin</title><content type='html'>&lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;Please download it here:&lt;a href=&quot;http://wlwplugincollection.codeplex.com/releases/view/9392&quot;&gt;http://wlwplugincollection.codeplex.com/releases/view/9392&lt;/a&gt;&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/8243013355595444074/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/8243013355595444074?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/8243013355595444074'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/8243013355595444074'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2011/02/good-windows-live-writer-code-format.html' title='Good windows live writer code format plugin'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-4177634937739135153</id><published>2011-02-09T00:43:00.000-08:00</published><updated>2011-02-10T00:53:25.132-08:00</updated><title type='text'>C++ Primer, Fourth Edition:Notes Chapter 12. Classes</title><content type='html'>&lt;h4&gt;Section 12.1 Class Definitions and Declarations&lt;/h4&gt; &lt;p&gt;In C++ we use classes to define our own &lt;b&gt;&lt;a name=&quot;ch12term1&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch12lev1sec8.html#gloss12_01&quot;&gt;abstract data types&lt;/a&gt;&lt;/b&gt;. By defining types that mirror concepts in the problems we are trying to solve, we can make our programs easier to write, debug, and modify&lt;/p&gt; &lt;p&gt;Classes are the most important feature in C++. Early versions of the language were named &quot;C with Classes,&quot; emphasizing the central role of the class facility. As the language evolved, support for building classes increased. A primary goal of the language design has been to provide features that allow programmers to define their own types that are as easy and intuitive to use as the built-in types. This chapter presents many of the basic features of classes&lt;/p&gt; &lt;h4&gt;&amp;nbsp;&lt;/h4&gt; &lt;ul&gt; &lt;li&gt;12.1.1. Class Definitions: A Recap  &lt;ul&gt; &lt;li&gt;Most fundamentally, a class defines a new type and a new scope  &lt;li&gt;Class Members  &lt;ul&gt; &lt;li&gt;Each class defines zero or more members. Members can be either data, functions, or type definitions  &lt;li&gt;A class may contain multiple &lt;tt&gt;public, private&lt;/tt&gt;, and &lt;tt&gt;protected&lt;/tt&gt; sections  &lt;li&gt;All members must be declared inside the class; there is no way to add members once the class definition is complete&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Constructors  &lt;ul&gt; &lt;li&gt;When we create an object of a class type, the compiler automatically uses a constructor (&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch02lev1sec3.html#ch02lev2sec12&quot;&gt;Section 2.3.3&lt;/a&gt;, p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch02lev1sec3.html#ch02lev2sec12&quot;&gt;49&lt;/a&gt;) to initialize the object. A constructor is a special member function that has the same name as the class. Its purpose is to ensure that each data member is set to sensible initial values  &lt;li&gt;A constructor generally should use a &lt;a name=&quot;ch12term9&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch12lev1sec8.html#gloss12_09&quot;&gt;constructor initializer list&lt;/a&gt; (&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch07lev1sec7.html#ch07lev2sec24&quot;&gt;Section 7.7.3&lt;/a&gt;, p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch07lev1sec7.html#ch07lev2sec24&quot;&gt;263&lt;/a&gt;), to initialize the data members of the object  &lt;li&gt;The constructor initializer list is a list of member names and parenthesized initial values. It follows the constructor&#39;s parameter list and begins with a colon&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Member Functions  &lt;ul&gt; &lt;li&gt;Member functions must be declared, and optionally may be defined, inside the class; functions defined inside the class are &lt;tt&gt;inline&lt;/tt&gt; (&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch07lev1sec6.html#ch07lev1sec6&quot;&gt;Section 7.6&lt;/a&gt;, p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch07lev1sec6.html#ch07lev1sec6&quot;&gt;256&lt;/a&gt;) by default  &lt;li&gt;Member functions defined outside the class must indicate that they are in the scope of the class. The definition of &lt;tt&gt;Sales_item::avg_price&lt;/tt&gt; uses the scope operator (&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch01lev1sec2.html#ch01lev2sec3&quot;&gt;Section 1.2.2&lt;/a&gt;, p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch01lev1sec2.html#ch01lev2sec3&quot;&gt;8&lt;/a&gt;) to indicate that the definition is for the &lt;tt&gt;avg_price&lt;/tt&gt; function of the &lt;tt&gt;Sales_item&lt;/tt&gt; class  &lt;li&gt;Member functions take an extra implicit argument that binds the function to the object on behalf of which the function is calledwhen we write &lt;strong&gt;trans.avg_price()&lt;/strong&gt; we are calling the avg_price function on the object named trans. If TRans is a Sales_item object, then references to a member of the Sales_item class inside the avg_price function are to the members in trans.  &lt;li&gt;&lt;strong&gt;A &lt;tt&gt;const&lt;/tt&gt; member may not change the data members of the object on which it operates. The &lt;tt&gt;const&lt;/tt&gt; must appear in both the declaration and definition. It is a compile-time error for the &lt;tt&gt;const&lt;/tt&gt; to be indicated on one but not the other&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;12.1.2. Data Abstraction and Encapsulation  &lt;ul&gt; &lt;li&gt;The fundamental ideas behind classes are data abstraction and encapsulation  &lt;li&gt;Data abstraction is a programming (and design) technique that relies on the separation of interface and implementation. The class designer must worry about how a class is implemented, but programmers that use the class need not know about these details. Instead, programmers who use a type need to know only the type&#39;s interface; they can think abstractly about what the type does rather than concretely about how the type works  &lt;li&gt;Encapsulation is a term that describes the technique of combining lower-level elements to form a new, higher-level entity. A function is one form of encapsulation: The detailed actions performed by the function are encapsulated in the larger entity that is the function itself. Encapsulated elements hide the details of their implementationwe may call a function but have no access to the statements that it executes. In the same way, a class is an encapsulated entity: It represents an aggregation of several members, and most (well-designed) class types hide the members that implement the type  &lt;li&gt;Access Labels Enforce Abstraction and Encapsulation  &lt;ul&gt; &lt;li&gt;Members defined after a public label are accessible to all parts of the program. The data-abstraction view of a type is defined by its public members  &lt;li&gt;Members defined after a private label are not accessible to code that uses the class. The private sections encapsulate (e.g., hide) the implementation from code that uses the type  &lt;li&gt;There are no restrictions on how often an access label may appear. Each access label specifies the access level of the succeeding member definitions. The specified access level remains in effect until the next access label is encountered or the closing right brace of the class body is seen  &lt;li&gt;&lt;strong&gt;A class may define members before any access label is seen. The access level of members defined after the open curly of the class and before the first access label depend on how the class is defined. If the class is defined with the &lt;/strong&gt;&lt;a name=&quot;ch12term24&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch12lev1sec8.html#gloss12_24&quot;&gt;&lt;strong&gt;&lt;tt&gt;struct&lt;/tt&gt; keyword&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;, then members defined before the first access label are &lt;tt&gt;public&lt;/tt&gt;; if the class is defined using the &lt;/strong&gt;&lt;a name=&quot;ch12term5&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch12lev1sec8.html#gloss12_05&quot;&gt;&lt;strong&gt;&lt;tt&gt;class&lt;/tt&gt; keyword&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;, then the members are &lt;tt&gt;private&lt;/tt&gt;&lt;/strong&gt;  &lt;li&gt;&lt;strong&gt;&lt;tt&gt;&lt;/tt&gt;&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p style=&quot;line-height: normal&quot;&gt; &lt;table border=&quot;1&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot; width=&quot;90%&quot;&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt; &lt;h2 style=&quot;text-transform: none&quot; class=&quot;docSidebarTitle&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;&lt;font style=&quot;font-size: 18pt&quot; color=&quot;#000000&quot;&gt;&lt;font style=&quot;font-weight: bold&quot;&gt;Advice: Concrete and Abstract Types&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/h2&gt; &lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font color=&quot;#000000&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;Not all types need to be abstract. The library &lt;/font&gt;&lt;tt&gt;pair&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt; class is a good example of a useful, well-designed class that is concrete rather than abstract. A &lt;/font&gt;&lt;/font&gt;&lt;span class=&quot;docEmphRoman&quot;&gt;&lt;a name=&quot;ch12term7&quot;&gt;&lt;/a&gt;&lt;a class=&quot;docLink&quot; href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch12lev1sec8.html#gloss12_07&quot;&gt;&lt;font color=&quot;#0066cc&quot; face=&quot;Times New Roman&quot;&gt;&lt;u&gt;concrete class&lt;/u&gt;&lt;/font&gt;&lt;/a&gt;&lt;/span&gt;&lt;font color=&quot;#000000&quot; face=&quot;Times New Roman&quot;&gt; is a class that exposes, rather than hides, its implementation.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font color=&quot;#000000&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;Some classes, such as &lt;/font&gt;&lt;tt&gt;pair&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt;, really have no abstract interface. The &lt;/font&gt;&lt;tt&gt;pair&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt; type exists to bundle two data members into a single object. There is no need or advantage to hiding the data members. Hiding the members in a class like &lt;/font&gt;&lt;tt&gt;pair&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt; would only complicate the use of the type.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font color=&quot;#000000&quot; face=&quot;Times New Roman&quot;&gt;Even so, such types often have member functions. In particular, it is a good idea for any class that has data members of built-in or compound type to define constructor(s) to initialize those members. The user of the class could initialize or assign to the data members but it is less error-prone for the class to do so.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;br&gt; &lt;li&gt; &lt;ul&gt; &lt;ul&gt;&lt;/ul&gt; &lt;li&gt;Different Kinds of Programming Roles  &lt;ul&gt; &lt;li&gt;When designing the interface to a class, the class designer should think about how &lt;a name=&quot;idd1e91667&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e91672&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e91677&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e91680&quot;&gt;&lt;/a&gt;easy it will be to use the class. When using the class, the designer shouldn&#39;t think about how the class works  &lt;li&gt;C++ programmers tend to speak of &quot;users&quot; interchangably as users of the application or users of a class  &lt;li&gt; &lt;p style=&quot;line-height: normal; text-indent: 0pt&quot;&gt; &lt;table border=&quot;1&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot; width=&quot;90%&quot;&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt; &lt;h2 style=&quot;text-transform: none&quot; class=&quot;docSidebarTitle&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;&lt;font style=&quot;font-size: 18pt&quot; color=&quot;#000000&quot;&gt;&lt;font style=&quot;font-weight: bold&quot;&gt;Key Concept: Benefits of Data Abstraction and Encapsulation&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/h2&gt; &lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font color=&quot;#000000&quot; face=&quot;Times New Roman&quot;&gt;Data abstraction and encapsulation provide two important advantages:&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;ul style=&quot;line-height: normal; padding-left: 0px; padding-right: 0px; margin-left: 30pt&quot;&gt; &lt;li style=&quot;text-indent: 0pt&quot;&gt; &lt;p class=&quot;docList&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font color=&quot;#000000&quot; face=&quot;Times New Roman&quot;&gt;Class internals are protected from inadvertent user-level errors, which might corrupt the state of the object.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;li style=&quot;text-indent: 0pt&quot;&gt; &lt;p class=&quot;docList&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font color=&quot;#000000&quot; face=&quot;Times New Roman&quot;&gt;The class implementation may evolve over time in response to changing requirements or bug reports without requiring change in user-level code.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font color=&quot;#000000&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;By defining data members only in the &lt;/font&gt;&lt;tt&gt;private&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt; section of the class, the class author is free to make changes in the data. If the implementation changes, only the class code needs to be examined to see what affect the change may have. If data are &lt;/font&gt;&lt;tt&gt;public&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt;, then any function that directly accesses the data members of the old representation might be broken. It would be necessary to locate and rewrite all those portions of code that relied on the old representation before the program could be used again.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font color=&quot;#000000&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;Similarly, if the internal state of the class is &lt;/font&gt;&lt;tt&gt;private&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt;, then changes to the member data can happen in only a limited number of places. The data is protected from mistakes that users might introduce. If there is a bug that corrupts the object&#39;s state, the places to look for the bug are localized: When data are &lt;/font&gt;&lt;tt&gt;private&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt;, only a member function could be responsible for the error. The search for the mistake is limited, greatly easing the problems of maintenance and program correctness.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font color=&quot;#000000&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;If the data are &lt;/font&gt;&lt;tt&gt;private&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt; and if the interface to the member functions does not change, then user functions that manipulate class objects require no change.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;a name=&quot;ch12note03&quot;&gt;&lt;/a&gt; &lt;div class=&quot;docNote&quot;&gt; &lt;p&gt; &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; width=&quot;90%&quot;&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td valign=&quot;top&quot; width=&quot;60&quot;&gt;&lt;img border=&quot;0&quot; alt=&quot;&quot; align=&quot;left&quot; src=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/images/0201721481/graphics/note.jpg;400478&quot; width=&quot;50&quot; height=&quot;54&quot;&gt;&lt;/td&gt; &lt;td valign=&quot;top&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font color=&quot;#000000&quot; face=&quot;Times New Roman&quot;&gt;Because changing a class definition in a header file effectively changes the text of every source file that includes that header, code that uses a class must be recompiled when the class changes.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br&gt;&lt;/p&gt;&lt;/div&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;br&gt;&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;12.1.3. More on Class Definitions  &lt;ul&gt; &lt;li&gt;Multiple Data Members of the Same Type  &lt;ul&gt; &lt;li&gt;One way in which member declarations and ordinary declarations are the same is that if a class has multiple data members with the same type, these members can be named in a single member declaration&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Using Typedefs to Streamline Classes  &lt;ul&gt; &lt;li&gt;In addition to defining data and function members, a class can also define its own local names for types  &lt;li&gt;Type names defined by a class obey the standard access controls of any other member. We put the definition of &lt;tt&gt;index&lt;/tt&gt; in the &lt;tt&gt;public&lt;/tt&gt; part of the class because &lt;a name=&quot;idd1e91914&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e91919&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e91924&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e91927&quot;&gt;&lt;/a&gt;we want users to use that name&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Member Functions May Be Overloaded  &lt;ul&gt; &lt;li&gt;With the exception of overloaded operators (&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch14lev1sec9.html#ch14lev2sec34&quot;&gt;Section 14.9.5&lt;/a&gt;, p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch14lev1sec9.html#ch14lev2sec34&quot;&gt;547&lt;/a&gt;)which have special rulesa member function overloads only other member functions of its own class. A class member function is unrelated to, and cannot overload, ordinary nonmember functions or functions declared in other classes. The same rules apply to overloaded member functions as apply to plain functions: Two overloaded members cannot have the same number and types of parameters&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Defining Overloaded Member Functions  &lt;li&gt;Explicitly Specifying inline Member Functions  &lt;ul&gt; &lt;li&gt;We can specify that a member is &lt;tt&gt;inline&lt;/tt&gt; as part of its declaration inside the class body. Alternatively, we can specify &lt;tt&gt;inline&lt;/tt&gt; on the function definition that appears outside the class body. It is legal to specify &lt;tt&gt;inline&lt;/tt&gt; both on the declaration and definition. One advantage of defining &lt;tt&gt;inline&lt;/tt&gt; functions outside the class is that it can make the class easier to read  &lt;li&gt;&lt;strong&gt;As with other &lt;tt&gt;inline&lt;/tt&gt;s, the definition of an &lt;tt&gt;inline&lt;/tt&gt; member function must be visible in every source file that calls the function. The definition for an &lt;tt&gt;inline&lt;/tt&gt; member function that is not defined within the class body ordinarily should be placed in the same header file in which the class definition appears&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;12.1.4. Class Declarations versus Definitions  &lt;ul&gt; &lt;li&gt;A class is completely defined once the closing curly brace appears. Once the class is defined, all the class members are known  &lt;li&gt;The size required to store an object of the class is known as well  &lt;li&gt;A class may be defined only once in a given source file. When a class is defined in multiple files, the definition in each file must be identical.  &lt;li&gt;&lt;strong&gt;By putting class definitions in header files, we can ensure that a class is defined the same way in each file that uses it. By using header guards (&lt;/strong&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch02lev1sec9.html#ch02lev2sec31&quot;&gt;&lt;strong&gt;Section 2.9.2&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;, p. &lt;/strong&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch02lev1sec9.html#ch02lev2sec31&quot;&gt;&lt;strong&gt;69&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;), &lt;/strong&gt;&lt;a name=&quot;idd1e92229&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e92236&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e92241&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e92244&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e92249&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e92254&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e92257&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e92260&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e92265&quot;&gt;&lt;/a&gt;&lt;strong&gt;we ensure that even if the header is included more than once in the same file, the class definition will be seen only once&lt;/strong&gt;  &lt;li&gt;It is possible to declare a class without defining it  &lt;li&gt;This declaration, sometimes referred to as a &lt;b&gt;&lt;a name=&quot;ch12term15&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch12lev1sec8.html#gloss12_15&quot;&gt;forward declaration&lt;/a&gt;&lt;/b&gt;, introduces the name &lt;tt&gt;Screen&lt;/tt&gt; into the program and indicates that &lt;tt&gt;Screen&lt;/tt&gt; refers to a class type. After a declaration and before a definition is seen, the type &lt;tt&gt;Screen&lt;/tt&gt; is an incompete typeit&#39;s known that &lt;tt&gt;Screen&lt;/tt&gt; is a type but not known what members that type contains  &lt;li&gt;&lt;strong&gt;An &lt;/strong&gt;&lt;a name=&quot;ch12term17&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch12lev1sec8.html#gloss12_17&quot;&gt;&lt;strong&gt;incomplete type&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt; can be used only in limited ways. Objects of the type may not be defined. An incomplete type may be used to define only pointers or references to the type or to declare (but not define) functions that use the type as a paremeter or return type&lt;/strong&gt;  &lt;li&gt;A data member can be specified to be of a class type only if the definition for the class has already been seen. If the type is incomplete, a data member can be only a pointer or a reference to that class type  &lt;li&gt;However, a class is considered declared as soon as its class name has been seen. Therefore, a class can have data members that are pointers or references to its own type  &lt;li&gt;A common use of class forward declarations is to write classes that are mutually dependent on one another&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;12.1.5. Class Objects  &lt;ul&gt; &lt;li&gt;When we define a class, we are defining a type. Once a class is defined, we can define objects of that type. Storage is allocated when we define objects, but (ordinarily) not when we define types  &lt;li&gt;Each object has its own copy of the class data members. Modifying the data members of &lt;tt&gt;item&lt;/tt&gt; does not change the data members of any other &lt;tt&gt;Sales_item&lt;/tt&gt; object  &lt;li&gt;Defining Objects of Class Type  &lt;ul&gt; &lt;li&gt;Using the class name directly as a type name  &lt;li&gt;&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Why a Class Definition Ends in a Semicolon  &lt;ul&gt; &lt;li&gt;A semicolon is required because we can follow a class definition by a list of object definitions. As always, a definition must end in a semicolon  &lt;li&gt;Ordinarily, it is a bad idea to define an object as part of a class definition. Doing so obscures what&#39;s happening. It is confusing to readers to combine definitions of two different entitiesthe class and a variablein a single statement&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;&lt;/li&gt;&lt;/ul&gt; &lt;h4&gt;Section 12.2 The Implicit this Pointer&lt;/h4&gt; &lt;ul&gt; &lt;li&gt;member functions have an extra implicit parameter that is a pointer to an object of the class type. This implicit parameter is named this, and is bound to the object on which the member function is called  &lt;li&gt;Member functions may not define the &lt;tt&gt;this&lt;/tt&gt; parameter; the compiler does so implicitly. The body of a member function may explicitly use the &lt;tt&gt;this&lt;/tt&gt; pointer, but is not required to do so. The compiler treats an unqualified reference to a class member as if it had been made through the &lt;tt&gt;this&lt;/tt&gt; pointer  &lt;li&gt;When to Use the this Pointer  &lt;ul&gt; &lt;li&gt;there is one case in which we must do so: when we need to refer to the object as a whole rather than to a member of the object&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Returning *this  &lt;li&gt;Returning *this from a const Member Function  &lt;ul&gt; &lt;li&gt;&lt;strong&gt;In an ordinary non&lt;tt&gt;const&lt;/tt&gt; member function, the type of &lt;tt&gt;this&lt;/tt&gt; is a &lt;tt&gt;const&lt;/tt&gt; pointer (&lt;/strong&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch04lev1sec2.html#ch04lev2sec7&quot;&gt;&lt;strong&gt;Section 4.2.5&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;, p. &lt;/strong&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch04lev1sec2.html#ch04lev2sec7&quot;&gt;&lt;strong&gt;126&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;) to the class type. We may change the value to which &lt;tt&gt;this&lt;/tt&gt; points but cannot change the address that &lt;tt&gt;this&lt;/tt&gt; holds&lt;/strong&gt;  &lt;li&gt;&lt;strong&gt;In a &lt;tt&gt;const&lt;/tt&gt; member function, the type of &lt;tt&gt;this&lt;/tt&gt; is a &lt;tt&gt;const&lt;/tt&gt; pointer to a &lt;tt&gt;const&lt;/tt&gt; class-type object. We may change neither the object to which &lt;tt&gt;this&lt;/tt&gt; points nor the address that &lt;tt&gt;this&lt;/tt&gt; holds&lt;/strong&gt;  &lt;li&gt;&lt;strong&gt;We cannot return a plain reference to the class object from a &lt;tt&gt;const&lt;/tt&gt; member function. A &lt;tt&gt;const&lt;/tt&gt; member function may return &lt;tt&gt;*this&lt;/tt&gt; only as a &lt;tt&gt;const&lt;/tt&gt; reference&lt;/strong&gt;  &lt;li&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Overloading Based on const  &lt;ul&gt; &lt;li&gt;We can overload a member function based on whether it is &lt;tt&gt;const&lt;/tt&gt; for the same reasons that we can overload a function based on whether a pointer parameter points to &lt;tt&gt;const&lt;/tt&gt;  &lt;li&gt;A &lt;tt&gt;const&lt;/tt&gt; object will use only the &lt;tt&gt;const&lt;/tt&gt; member. A non&lt;tt&gt;const&lt;/tt&gt; object could use either member, but the non&lt;tt&gt;const&lt;/tt&gt; version is a better match&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Mutable Data Members  &lt;ul&gt; &lt;li&gt;&lt;strong&gt;It sometimes (but not very often) happens that a class has a data member that we want to be able to modify, even inside a &lt;tt&gt;const&lt;/tt&gt; member function. We can indicate such members by declaring them as &lt;tt&gt;mutable&lt;/tt&gt;&lt;/strong&gt;  &lt;li&gt;&lt;strong&gt;A &lt;/strong&gt;&lt;a name=&quot;ch12term19&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch12lev1sec8.html#gloss12_19&quot;&gt;&lt;strong&gt;mutable data member&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt; is a member that is never &lt;tt&gt;const&lt;/tt&gt;, even when it is a member of a &lt;tt&gt;const&lt;/tt&gt; object. Accordingly, a &lt;tt&gt;const&lt;/tt&gt; member function may change a &lt;tt&gt;mutable&lt;/tt&gt; member&lt;/strong&gt;  &lt;p style=&quot;line-height: normal&quot;&gt; &lt;table border=&quot;1&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot; width=&quot;90%&quot;&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt; &lt;h2 style=&quot;text-transform: none&quot; class=&quot;docSidebarTitle&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;&lt;font style=&quot;font-size: 18pt&quot; color=&quot;#000000&quot;&gt;&lt;font style=&quot;font-weight: bold&quot;&gt;Advice: Use Private Utility Functions for Common Code&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/h2&gt; &lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font color=&quot;#000000&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;Some readers might be surprised that we bothered to define a separate &lt;/font&gt;&lt;tt&gt;do_display&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt; operation. After all, the calls to &lt;/font&gt;&lt;tt&gt;do_display&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt; aren&#39;t much simpler than the action done inside &lt;/font&gt;&lt;tt&gt;do_display&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt;. Why bother? We do so for several reasons:&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;div&gt; &lt;ol class=&quot;docList&quot; type=&quot;1&quot;&gt; &lt;li style=&quot;text-indent: 0pt&quot;&gt; &lt;div&gt; &lt;p class=&quot;docList&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font color=&quot;#000000&quot; face=&quot;Times New Roman&quot;&gt;A general desire to avoid writing the same code in more than one place.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt; &lt;li style=&quot;text-indent: 0pt&quot;&gt; &lt;div&gt; &lt;p class=&quot;docList&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font color=&quot;#000000&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;The &lt;/font&gt;&lt;tt&gt;display&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt; operation can be expected to become more complicated as our class evolves. As the actions involved become more complex, it makes more obvious sense to write those actions in one place, not two.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt; &lt;li style=&quot;text-indent: 0pt&quot;&gt; &lt;div&gt; &lt;p class=&quot;docList&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font color=&quot;#000000&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;It is likely that we might want to add debugging information to &lt;/font&gt;&lt;tt&gt;do_display&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt; during development that would be eliminated in the final product version of the code. It will be easier to do so if only one definition of &lt;/font&gt;&lt;tt&gt;do_display&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt; needs to be changed to add or remove the debugging code.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt; &lt;li style=&quot;text-indent: 0pt&quot;&gt; &lt;div&gt; &lt;p class=&quot;docList&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font color=&quot;#000000&quot;&gt;&lt;strong&gt;&lt;font face=&quot;Times New Roman&quot;&gt;There needn&#39;t be any overhead involved in this extra function call. We made &lt;/font&gt;&lt;tt&gt;do_display&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt; inline, so the run-time performance between calling &lt;/font&gt;&lt;tt&gt;do_display&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt; or putting the code directly into the &lt;/font&gt;&lt;tt&gt;display&lt;/tt&gt;&lt;/strong&gt;&lt;font face=&quot;Times New Roman&quot;&gt;&lt;strong&gt; operations should be identical&lt;/strong&gt;.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt; &lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font color=&quot;#000000&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;In practice, well-designed C++ programs tend to have lots of small functions such as &lt;/font&gt;&lt;tt&gt;do_display&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt; that are called to do the &quot;real&quot; work of some other set of functions.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;br&gt; &lt;li&gt;&lt;strong&gt;a&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;&lt;/li&gt;&lt;/ul&gt; &lt;h4&gt;Section 12.3 Class Scope&lt;/h4&gt; &lt;ol&gt; &lt;li&gt;Every class defines its own new scope and a unique type. The declarations of the class members within the class body introduce the member names into the scope of their class. Two different classes have two different class scopes  &lt;li&gt;Even if two classes have exactly the same member list, they are different types. The members of each class are distinct from the members of any other class (or any other scope).  &lt;li&gt;Using a Class Member  &lt;ol&gt; &lt;li&gt;Outside the class scope, members may be accessed only through an object or a pointer using member access operators dot or arrow, respectively  &lt;li&gt;Some members are accessed using the member access operators; others are accessed directly from the class using the scope operator, (&lt;tt&gt;::&lt;/tt&gt;). Ordinary data or function members must be accessed through an object. Members that define types, such as &lt;tt&gt;Screen::index&lt;/tt&gt;, are accessed using the scope operator&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;Scope and Member Definitions  &lt;ol&gt; &lt;li&gt;Member definitions behave as if they are in the scope of the class, even if the member is defined outside the class body&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;Parameter Lists and Function Bodies Are in Class Scope  &lt;ol&gt; &lt;li&gt;These are defined inside the class scope and so may refer to other class members without qualification&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;Function Return Types Aren&#39;t Always in Class Scope  &lt;ol&gt; &lt;li&gt;&lt;strong&gt;If the function is defined outside the class body, then the name used for the return type is outside the class scope. If the return type uses a type defined by the class, it must use the fully qualified name&lt;/strong&gt;  &lt;li&gt;&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;12.3.1. Name Lookup in Class Scope  &lt;ol&gt; &lt;li&gt;In the programs we&#39;ve written so far, &lt;b&gt;&lt;a name=&quot;ch12term20&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch12lev1sec8.html#gloss12_20&quot;&gt;name lookup&lt;/a&gt;&lt;/b&gt; (the process of finding which declaration is matched to a given use of a name) has been relatively straightforward  &lt;ol&gt; &lt;li&gt;First, look for a declaration of the name in the block in which the name was used. Only names declared before the use are considered.  &lt;li&gt;If the name isn&#39;t found, the enclosing scope(s) are searched.&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;If no declaration is found, then the program is in error. In C++ programs, all names must be declared before they are used  &lt;li&gt;Class scopes may seem to behave a bit differently, but in reality they obey this same rule. Confusion can arise due to the way names are resolved inside a function defined within the class body itself  &lt;li&gt;Class definitions are actually processed in two phases  &lt;ol&gt; &lt;li&gt;First, the member declarations are compiled  &lt;li&gt;Only after all the class members have been seen are the definitions themselves compiled&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;Of course, the names used in class scope do not always have to be class member names. Name lookup in class scope finds names declared in other scopes as well. During name lookup, if a name used in class scope does not resolve to a class member name, the scopes surrounding the class or member definition are searched to find a declaration for the name  &lt;li&gt;Name Lookup for Class Member Declarations  &lt;ol&gt; &lt;li&gt;Names used in the declarations of a class member are resolved as follows  &lt;ol&gt; &lt;li&gt;The declarations of the class members that appear before the use of the name are considered  &lt;li&gt;If the lookup in step 1 is not successful, the declarations that appear in the scope in which the class is defined, and that appear before the class definition itself, are considered&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;Names of types defined in a class must be seen before they are used as the type of a data member or as the return type or parameter type(s) of a member function  &lt;li&gt;The compiler handles member declarations in the order in which they appear in the class. As usual, a name must be defined before it can be used. Moreover, once a name has been used as the name of a type, that name may not be redefined&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;Name Lookup in Class Member Definitions  &lt;ol&gt; &lt;li&gt;A name used in the body of a member function is resolved as follows  &lt;ol&gt; &lt;li&gt;Declarations in the member-function local scopes are considered first  &lt;li&gt;If the a declaration for the name is not found in the member function, the declarations for all the class members are considered  &lt;li&gt;If a declaration for the name is not found in the class, the declarations that appear in scope before the member function definition are considered&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;Class Members Follow Normal Block-Scope Name Lookup  &lt;ol&gt; &lt;li&gt;Programs that illustrate how name lookup works often have to rely on bad practices. The next several programs contain bad style deliberately  &lt;li&gt;The following function uses the same name for a parameter and a member, which normally should be avoided  &lt;li&gt;Even though the class member is hidden, it is still possible to use it by qualifying the member&#39;s name with the name of its class or by using the &lt;tt&gt;this&lt;/tt&gt; pointer explicitly&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;After Function Scope, Look in Class Scope  &lt;li&gt;After Class Scope, Look in the Surrounding Scope  &lt;ol&gt; &lt;li&gt;Even though the global object is hidden, it is still possible to use it by qualifying the name with the global scope resolution operator&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;Names Are Resolved Where They Appear within the File  &lt;ol&gt; &lt;li&gt;When a member is defined outside the class definition, the third step of name lookup not only considers the declarations in global scope that appear before the definition of class Screen, but also considers the global scope declarations that appear before the member function definition&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;&lt;/li&gt;&lt;/ol&gt; &lt;h4&gt;Section 12.4 Constructors&lt;/h4&gt; &lt;ol&gt; &lt;li&gt;Constructors (&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch02lev1sec3.html#ch02lev2sec12&quot;&gt;Section 2.3.3&lt;/a&gt;, p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch02lev1sec3.html#ch02lev2sec12&quot;&gt;49&lt;/a&gt;) are special member functions that are executed whenever we create new objects of a class type. The job of a constructor is to ensure that the data members of each object start out with sensible initial values  &lt;li&gt;Constructors have the same name as the name of the class and may not specify a return type. Like any other function, they may define zero or more parameters  &lt;li&gt;Constructors May Be Overloaded  &lt;ol&gt; &lt;li&gt;There is no constraint on the number of constructors we may declare for a class, provided that the parameter list of each constructor is unique. How can we know which or how many constructors to define? Ordinarily, constructors differ in ways that allow the user to specify differing ways to initialize the data members&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;Arguments Determine Which Constructor to Use  &lt;ol&gt; &lt;li&gt;The argument type(s) used to initialize an object determines which constructor is used&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;Constructors Are Executed Automatically  &lt;ol&gt; &lt;li&gt;The compiler runs a constructor whenever an object of the type is created&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;Constructors for const Objects  &lt;ol&gt; &lt;li&gt;A constructor may not be declared as &lt;tt&gt;const&lt;/tt&gt;  &lt;li&gt;There is no need for a &lt;tt&gt;const&lt;/tt&gt; constructor. When we create a &lt;tt&gt;const&lt;/tt&gt; object of a class type, an ordinary constructor is run to initialize the &lt;tt&gt;const&lt;/tt&gt; object. The job of the constructor is to initialize an object. A constructor is used to initialize an object regardless of whether the object is &lt;tt&gt;const&lt;/tt&gt;&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;12.4.1. The Constructor Initializer  &lt;ol&gt; &lt;li&gt;Like any other function, a constructor has a name, a parameter list, and a function body. Unlike other functions, a constructor may also contain a constructor initializer list  &lt;li&gt;The constructor initializer starts with a colon, which is followed by a comma-separated list of data members each of which is followed by an initializer inside parentheses  &lt;li&gt;&lt;strong&gt;As with any member function, constructors can be defined inside or outside of the class. The constructor initializer is specified only on the constructor definition, not its declaration&lt;/strong&gt;  &lt;li&gt;The constructor initializer is a feature that many reasonably experienced C++ programmers have not mastered  &lt;li&gt;&lt;strong&gt;Data members of class type are always initialized in the initialization phase, regardless of whether the member is initialized explicitly in the constructor initializer list. Initialization happens before the computation phase begins&lt;/strong&gt;  &lt;li&gt;Each member that is not explicitly mentioned in the constructor initializer is initialized using the same rules as those used to initialize variables (&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch02lev1sec3.html#ch02lev2sec13&quot;&gt;Section 2.3.4&lt;/a&gt;, p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch02lev1sec3.html#ch02lev2sec13&quot;&gt;50&lt;/a&gt;). Data members of class type are initialized by running the type&#39;s default constructor. The initial value of members of built-in or compound type depend on the scope of the object: At local scope those members are uninitialized, at global scope they are initialized to 0  &lt;li&gt;Constructor Initializers Are Sometimes Required  &lt;ol&gt; &lt;li&gt;If an initializer is not provided for a class member, then the compiler implicitly uses the default constructor for the member&#39;s type. If that class does not have a default constructor, then the attempt by the compiler to use it will fail. In such cases, an initializer must be provided in order to initialize the data member  &lt;li&gt;&lt;strong&gt;Some members must be initialized in the constructor initializer. For such members, assigning to them in the constructor body doesn&#39;t work. Members of a class type that do not have a default constructor and members that are &lt;tt&gt;const&lt;/tt&gt; or reference types must be initialized in the constructor initializer regardless of type&lt;/strong&gt;  &lt;li&gt;&lt;strong&gt;Remember that we can initialize but not assign to &lt;tt&gt;const&lt;/tt&gt; objects or objects of reference type. By the time the body of the constructor begins executing, initialization is complete. Our only chance to initialize &lt;tt&gt;const&lt;/tt&gt; or reference data members is in the constructor initializer&lt;/strong&gt;  &lt;h2 style=&quot;text-transform: none; list-style-type: disc&quot; class=&quot;docSidebarTitle&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;&lt;font style=&quot;font-size: 18pt&quot; color=&quot;#000000&quot;&gt;&lt;font style=&quot;font-weight: bold&quot;&gt;Advice: Use Constructor Initializers&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/h2&gt; &lt;p style=&quot;line-height: normal; list-style-type: disc&quot; class=&quot;docText&quot;&gt;&lt;a name=&quot;idd1e95153&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e95156&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e95161&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e95164&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e95169&quot;&gt;&lt;/a&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;&lt;font style=&quot;font-size: 12pt&quot; color=&quot;#000000&quot;&gt;In many classes, the distinction between initialization and assignment is strictly a matter of low-level efficiency: A data member is initialized and assigned when it could have been initialized directly. More important than the efficiency issue is the fact that some data members must be initialized.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;a style=&quot;line-height: normal; list-style-type: disc&quot; name=&quot;ch12note18&quot;&gt;&lt;/a&gt; &lt;div style=&quot;line-height: normal; list-style-type: disc&quot; class=&quot;docNote&quot;&gt; &lt;p&gt; &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;1&quot; width=&quot;90%&quot;&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td valign=&quot;top&quot; width=&quot;60&quot;&gt;&lt;img id=&quot;195131139046&quot; border=&quot;0&quot; alt=&quot;&quot; align=&quot;left&quot; src=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/images/0201721481/graphics/note.jpg;400478&quot; width=&quot;50&quot; height=&quot;54&quot;&gt;&lt;/td&gt; &lt;td valign=&quot;top&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font color=&quot;#000000&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;We must use an initializer for any &lt;/font&gt;&lt;tt&gt;const&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt; or reference member or for any member of a class type that does not have a default constructor.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br&gt;&lt;/p&gt;&lt;/div&gt;&lt;br&gt; &lt;p style=&quot;line-height: normal; list-style-type: disc&quot; class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;&lt;font style=&quot;font-size: 12pt&quot; color=&quot;#000000&quot;&gt;By routinely using constructor initializers, we can avoid being surprised by compile-time errors when we have a class with a member that requires a constructor initializer.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;li&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;Order of Member Initialization  &lt;ol&gt; &lt;li&gt;Not surprisingly, each member may be named only once in the constructor initializer. After all, what might it mean to give a member two initial values? What may be more surprising is that the constructor initializer list specifies only the values used to initialize the members, not the order in which those initializations are performed. The order in which members are initialized is the order in which the members are defined. The first member is initialized first, then the next, and so on  &lt;li&gt;The order of initialization often doesn&#39;t matter. However, if one member is initialized in terms of another, then the order in which members are initialized is crucially important  &lt;li&gt;&lt;strong&gt;It is a good idea to write constructor initializers in the same order as the members are declared. Moreover, when possible, avoid using members to initialize other members&lt;/strong&gt;  &lt;li&gt;It is often the case that we can avoid any problems due to order of execution for initializers by (re)using the constructor&#39;s parameters rather than using the object&#39;s data members&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;Initializers May Be Any Expression  &lt;ol&gt; &lt;li&gt;An initializer may be an arbitrarily complex expression  &lt;li&gt;&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;Initializers for Data Members of Class Type  &lt;ol&gt; &lt;li&gt;When we initialize a member of class type, we are specifying arguments to be passed to one of the constructors of that member&#39;s type. We can use any of that type&#39;s constructors&lt;/li&gt;&lt;/ol&gt;&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;12.4.2. Default Arguments and Constructors  &lt;ol&gt; &lt;li&gt;We can combine these constructors by supplying a default argument for the &lt;tt&gt;string&lt;/tt&gt; initializer  &lt;li&gt;We prefer to use a default argument because it reduces code duplication.  &lt;li&gt;&lt;img style=&quot;border-bottom-style: none; border-right-style: none; border-top-style: none; border-left-style: none&quot; class=&quot;wlEmoticon wlEmoticon-smile&quot; alt=&quot;Smile&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhejBdtnRIXSSnivtOHB7HP09zzfQrNtu5dJdpLC_1tKvAzxqJMegyHdLRvvGolQf9tJ0gx7EcGv1MdegT5FMYK6uAWEADep0emQo37gvaeeKFNjbB3HUiw7nmrpCNIEXQC8a1YOUCBqKg/?imgmax=800&quot;&gt;&amp;nbsp; &lt;div style=&quot;border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px&quot; id=&quot;codeSnippetWrapper&quot;&gt; &lt;div style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot; id=&quot;codeSnippet&quot;&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; Sales_item {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt;:&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;         &lt;span style=&quot;color: #008000&quot;&gt;// default argument for book is the empty string&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;         Sales_item(&lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; std::string &amp;amp;book = &lt;span style=&quot;color: #006080&quot;&gt;&quot;&quot;&lt;/span&gt;):&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;                   isbn(book), units_sold(0), revenue(0.0) { }&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;         Sales_item(std::istream &amp;amp;is);&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;         &lt;span style=&quot;color: #008000&quot;&gt;// as before&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;     };&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&amp;nbsp;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&amp;nbsp;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;12.4.3. The Default Constructor &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;The default constructor is used whenever we define an object but do not supply an initializer. A constructor that supplies default arguments for all its parameters also defines the default constructor &lt;br /&gt;&lt;li&gt;The Synthesized Default Constructor &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;If a class defines even one constructor, then the compiler will not generate the default constructor. The basis for this rule is that if a class requires control to initialize an object in one case, then the class is likely to require control in all cases &lt;br /&gt;&lt;li&gt;&lt;strong&gt;The compiler generates a default constructor automatically only if a class defines no constructors&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;The &lt;/strong&gt;&lt;a name=&quot;ch12term25&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch12lev1sec8.html#gloss12_25&quot;&gt;&lt;strong&gt;synthesized default constructor&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt; initializes members using the same rules as those that apply for how variables are initialized. Members that are of class type are initialized by running each member&#39;s own default constructor. Members of built-in or compound type, such as pointers and arrays, are initialized only for objects that are defined at global scope. When objects are defined at local scope, then members of built-in or compound type are uninitialized&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;If a class contains data members of built-in or compound type, then the class should not rely on the synthesized default constructor. It should define its own constructor to initialize these members&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Classes Should Usually Define a Default Constructor &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;In certain cases, the default constructor is applied implicitly by the compiler. If the class has no default constructor, then the class may not be used in these contexts &lt;br /&gt;&lt;li&gt;&lt;strong&gt;In practice, it is almost always right to provide a default constructor if other constructors are being defined. Ordinarily the initial values given to the members in the default constructor should indicate that the object is &quot;empty.&quot;&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Using the Default Constructor&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;12.4.4. Implicit Class-Type Conversions &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;To define an implicit conversion to a class type, we need to define an appropriate constructor. &lt;br /&gt;&lt;li&gt;Suppressing Implicit Conversions Defined by Constructors &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;We can prevent the use of a constructor in a context that requries an implicit conversion by declaring the constructor &lt;tt&gt;&lt;strong&gt;explicit&lt;/strong&gt;&lt;/tt&gt; &lt;br /&gt;&lt;li&gt;The &lt;tt&gt;explicit&lt;/tt&gt; keyword is used only on the constructor declaration inside the class. It is not repeated on a definition made outside the class body &lt;br /&gt;&lt;li&gt;&lt;strong&gt;When a constructor is declared &lt;tt&gt;explicit&lt;/tt&gt;, the compiler will not use it as a conversion operator&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Explicitly Using Constructors for Conversions &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;An &lt;a name=&quot;ch12term14&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch12lev1sec8.html#gloss12_14&quot;&gt;&lt;tt&gt;explicit&lt;/tt&gt; constructor&lt;/a&gt; can be used to generate a conversion as long as we do so explicitly &lt;br /&gt;&lt;li&gt;Making a constructor &lt;tt&gt;explicit&lt;/tt&gt; turns off only the use of the constructor implicitly. Any constructor can be used to explicitly create a temporary object &lt;br /&gt;&lt;li&gt;&lt;strong&gt;Ordinarily, single-parameter constructors should be &lt;tt&gt;explicit&lt;/tt&gt; unless there is an obvious reason to want to define an implicit conversion. Making constructors &lt;tt&gt;explicit&lt;/tt&gt; may avoid mistakes, and a user can explicitly construct an object when a conversion is useful&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;12.4.5. Explicit Initialization of Class Members &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Although most objects are initialized by running an appropriate constructor, it is possible to initialize the data members of simple nonabstract classes directly. Members of classes that define no constructors and all of whose data members are &lt;tt&gt;public&lt;/tt&gt; may be initialized in the same way that we initialize array elements &lt;br /&gt;&lt;div style=&quot;border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px&quot; id=&quot;codeSnippetWrapper&quot;&gt;&lt;br /&gt;&lt;div style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot; id=&quot;codeSnippet&quot;&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;struct&lt;/span&gt; Data {&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    &lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; ival;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;    &lt;span style=&quot;color: #0000ff&quot;&gt;char&lt;/span&gt; *ptr;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;};&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&lt;span style=&quot;color: #008000&quot;&gt;// val1.ival = 0; val1.ptr = 0&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;Data val1 = { 0, 0 };&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&amp;nbsp;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&lt;span style=&quot;color: #008000&quot;&gt;// val2.ival = 1024;&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;&lt;span style=&quot;color: #008000&quot;&gt;// val2.ptr = &quot;Anna Livia Plurabelle&quot;&lt;/span&gt;&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px&quot;&gt;Data val2 = { 1024, &lt;span style=&quot;color: #006080&quot;&gt;&quot;Anna Livia Plurabelle&quot;&lt;/span&gt; };&lt;/pre&gt;&lt;!--CRLF--&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;li&gt;This form of initialization is inherited from C and is supported for compatibility with C programs. There are three significant drawbacks to explicitly initializing the members of an object of class type &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;It requires that all the data members of the class be &lt;tt&gt;public&lt;/tt&gt; &lt;br /&gt;&lt;li&gt;It puts the burden on the programmer to initialize every member of every object. Such initialization is tedious and error-prone because it is easy to forget an initializer or to supply an inappropriate initializer &lt;br /&gt;&lt;li&gt;If a member is added or removed, all initializations have to be found and updated correctly&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;It is almost always better to define and use constructors. When we provide a default constructor for the types we define, we allow the compiler to automatically run that constructor, ensuring that every class object is properly initialized prior to the first use of that object&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;h4&gt;Section 12.5 Friends&lt;/h4&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;In some cases, it is convenient to let specific nonmember functions access the &lt;a name=&quot;ch12term21&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch12lev1sec8.html#gloss12_21&quot;&gt;private members&lt;/a&gt; of a class while still preventing general access&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;The &lt;/strong&gt;&lt;a name=&quot;ch12term16&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch12lev1sec8.html#gloss12_16&quot;&gt;&lt;tt&gt;&lt;strong&gt;friend&lt;/strong&gt;&lt;/tt&gt;&lt;/a&gt;&lt;strong&gt; mechanism allows a class to grant access to its nonpublic members to specified functions or classes. A friend declaration begins with the keyword &lt;tt&gt;friend&lt;/tt&gt;. It may appear only within a class definition. Friend declarations may appear anywhere in the class: Friends are not members of the class granting friendship, and so they are not affected by the access control of the section in which they are declared&lt;/strong&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Ordinarily it is a good idea to group friend declarations together either at the beginning or end of the class definition&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Friendship: An Example&lt;/li&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;A friend may be an ordinary, nonmember function, a member function of another previously defined class, or an entire class. In making a class a friend, all the member functions of the friend class are given access to the nonpublic members of the class granting friendship&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Making Another Class&#39; Member Function a Friend&lt;/li&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;When we declare a member function to be a friend, the name of the function must be qualified by the name of the class of which it is a member&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Friend Declarations and Scope&lt;/li&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Interdependencies among friend declarations and the definitions of the friends can require some care in order to structure the classes correctly&lt;/li&gt;&lt;br /&gt;&lt;li&gt;More generally, to make a member function a friend, the class containing that member must have been defined. On the other hand, a class or nonmember function need not have been declared to be made a friend&lt;/li&gt;&lt;br /&gt;&lt;li&gt;A friend declaration introduces the named class or nonmember function into the surrounding scope. Moreover, a friend function may be defined inside the class. The scope of the function is exported to the scope enclosing the class definition&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Overloaded Functions and Friendship&lt;/li&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;A class must declare as a friend each function in a set of overloaded functions that it wishes to make a friend&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;h4&gt;Section 12.6 static Class Members&lt;/h4&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;It is sometimes necessary for all the objects of a particular class type to access a global object. Perhaps a count is needed of how many objects of a particular class type have been created at any one point in the program, or the global object may be a pointer to an error-handling routine for the class, or it may be a pointer to the free-store memory for objects of this class type&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Ordinary, non&lt;tt&gt;static&lt;/tt&gt; data members exist in each object of the class type. Unlike ordinary data members, a &lt;tt&gt;static&lt;/tt&gt; data member exists independently of any object of its class; each &lt;tt&gt;static&lt;/tt&gt; data member is an object associated with the class, not with the objects of that class&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Just as a class may define shared &lt;tt&gt;static&lt;/tt&gt; data members, it may also define &lt;tt&gt;static&lt;/tt&gt; member functions. A &lt;tt&gt;static&lt;/tt&gt; member function has no &lt;tt&gt;this&lt;/tt&gt; parameter. It may directly access the &lt;tt&gt;static&lt;/tt&gt; members of its class but may not directly use the non&lt;tt&gt;static&lt;/tt&gt; members&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Advantages of Using Class static Members&lt;/li&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;The name of a &lt;tt&gt;static&lt;/tt&gt; member is in the scope of the class, thereby avoiding name collisions with members of other classes or global objects&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Encapsulation can be enforced. A &lt;tt&gt;static&lt;/tt&gt; member can be a private member; a global object cannot&lt;/li&gt;&lt;br /&gt;&lt;li&gt;It is easy to see by reading the program that a &lt;tt&gt;static&lt;/tt&gt; member is associated with a particular class. This visibility clarifies the programmer&#39;s intentions&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Defining static Members&lt;/li&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Amember ismade &lt;tt&gt;static&lt;/tt&gt; by prefixing the member declaration with the keyword &lt;tt&gt;static&lt;/tt&gt;. The &lt;tt&gt;static&lt;/tt&gt; members obey the normal public/private access rules&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Using a Class static Member&lt;/li&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;A &lt;tt&gt;static&lt;/tt&gt; member can be invoked directly from the class using the scope operator or indirectly through an object, reference, or pointer to an object of its class type&lt;/li&gt;&lt;br /&gt;&lt;li&gt;As with other members, a class member function can refer to a class &lt;tt&gt;static&lt;/tt&gt; member without the use of the scope operator&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;12.6.1. static Member Functions&lt;/li&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;When we define a &lt;tt&gt;static&lt;/tt&gt; member outside the class, we do not respecify the &lt;tt&gt;static&lt;/tt&gt; keyword.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;static Functions Have No this Pointer&lt;/li&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;A &lt;tt&gt;static&lt;/tt&gt; member is part of its class but not part of any object. Hence, a &lt;tt&gt;static&lt;/tt&gt; member function does not have a &lt;tt&gt;this&lt;/tt&gt; pointer. Referring to &lt;tt&gt;this&lt;/tt&gt; either explicitly or implicitly by using a non&lt;tt&gt;static&lt;/tt&gt; member is a compile-time error&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;Because a &lt;tt&gt;static&lt;/tt&gt; member is not part of any object, &lt;tt&gt;static&lt;/tt&gt; member functions may not be declared as &lt;tt&gt;const&lt;/tt&gt;. After all, declaring a member function as &lt;tt&gt;const&lt;/tt&gt; is a promise not to modify the object of which the function is a member. Finally, &lt;tt&gt;static&lt;/tt&gt; member functions may also not be declared as virtual&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;12.6.2. static Data Members&lt;/li&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;tt&gt;static&lt;/tt&gt; data members can be declared to be of any type. They can be &lt;tt&gt;const&lt;/tt&gt;s, references, arrays, class types, and so forth&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;tt&gt;static&lt;/tt&gt; data members must be defined (exactly once) outside the class body. Unlike ordinary data members, &lt;tt&gt;static&lt;/tt&gt; members are not initialized through the class constructor(s) and instead should be initialized when they are defined&lt;/li&gt;&lt;br /&gt;&lt;li&gt;The best way to ensure that the object is defined exactly once is to put the definition of &lt;tt&gt;static&lt;/tt&gt; data members in the same file that contains the definitions of the class noninline member functions&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;tt&gt;static&lt;/tt&gt; data members are defined in the same way that other class members and other variables are defined. The member is defined by naming its type followed by the fully qualified name of the member&lt;/li&gt;&lt;br /&gt;&lt;li&gt;As with any class member, when we refer to a class &lt;tt&gt;static&lt;/tt&gt; member outside the class body, we must specify the class in which the member is defined. The &lt;tt&gt;static&lt;/tt&gt; keyword, however, is used only on the declaration inside the class body. Definitions are not labeled &lt;tt&gt;static&lt;/tt&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;tt&gt;&lt;/tt&gt;Integral const static Members Are Special&lt;/li&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;Ordinarily, class static members, like ordinary data members, cannot be initialized in the class body. Instead, static data members are normally initialized when they are defined.&lt;/strong&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;When a &lt;tt&gt;const static&lt;/tt&gt; data member is initialized in the class body, the data member must still be defined outside the class definition&lt;/strong&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;When an initializer is provided inside the class, the definition of the member must not specify an initial value&lt;br&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;static Members Are Not Part of Class Objects&lt;/li&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Ordinary members are part of each object of the given class. &lt;tt&gt;static&lt;/tt&gt; members exist independently of any object and are not part of objects of the class type. Because &lt;tt&gt;static&lt;/tt&gt; data members are not part of any object, they can be used in ways that would be illegal for non&lt;tt&gt;static&lt;/tt&gt; data members&lt;/li&gt;&lt;br /&gt;&lt;li&gt;the type of a &lt;tt&gt;static&lt;/tt&gt; data member can be the class type of which it is a member. A non&lt;tt&gt;static&lt;/tt&gt; data member is restricted to being declared as a pointer or a reference to an object of its class&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Similarly, a &lt;tt&gt;static&lt;/tt&gt; data member can be used as a default argument&lt;/li&gt;&lt;br /&gt;&lt;li&gt;A non&lt;tt&gt;static&lt;/tt&gt; data member may not be used as a default argument because its value cannot be used independently of the object of which it is a part. Using a non&lt;tt&gt;static&lt;/tt&gt; data member as a default argument provides no object from which to obtain the member&#39;s value and so is an error&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;  </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/4177634937739135153/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/4177634937739135153?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/4177634937739135153'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/4177634937739135153'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2011/02/c-primer-fourth-editionnotes-chapter-12.html' title='C++ Primer, Fourth Edition:Notes Chapter 12. Classes'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhejBdtnRIXSSnivtOHB7HP09zzfQrNtu5dJdpLC_1tKvAzxqJMegyHdLRvvGolQf9tJ0gx7EcGv1MdegT5FMYK6uAWEADep0emQo37gvaeeKFNjbB3HUiw7nmrpCNIEXQC8a1YOUCBqKg/s72-c?imgmax=800" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-8302790814073711961</id><published>2011-01-21T04:52:00.000-08:00</published><updated>2011-02-09T21:07:18.831-08:00</updated><title type='text'>C++ Primer, Fourth Edition:Notes Chapter 10 Associative Containers</title><content type='html'>&lt;p&gt;Associative containers differ in a fundamental respect from the sequential containers: Elements in an associative container are stored and retrieved by a key, in contrast to elements in a sequential container, which are stored and accessed sequentially by their position within the container&lt;/p&gt; &lt;p&gt;&lt;b&gt;&lt;a name=&quot;ch10term2&quot;&gt;&lt;/a&gt;&lt;a href=&quot;ch10lev1sec8.html#gloss10_02&quot;&gt;Associative containers&lt;/a&gt;&lt;/b&gt; support efficient lookup and retrieval by a key. The two primary associative-container types are &lt;tt&gt;map&lt;/tt&gt; and &lt;a name=&quot;ch10term9&quot;&gt;&lt;/a&gt;&lt;a href=&quot;ch10lev1sec8.html#gloss10_09&quot;&gt;&lt;tt&gt;set&lt;/tt&gt;&lt;/a&gt;. The elements in a &lt;tt&gt;map&lt;/tt&gt; are keyvalue pairs: The key serves as an index into the &lt;tt&gt;map&lt;/tt&gt;, and the value represents the data that are stored and retrieved. A &lt;tt&gt;set&lt;/tt&gt; contains only a key and supports efficient queries to whether a given key is present&lt;/p&gt; &lt;p&gt;In general, a &lt;tt&gt;set&lt;/tt&gt; is most useful when we want to store a collection of distinct values efficiently, and a &lt;tt&gt;map&lt;/tt&gt; is most useful when we wish to store (and possibly modify) a value associated with each key&lt;/p&gt; &lt;p&gt;We might use a &lt;tt&gt;set&lt;/tt&gt; to hold words that we want to ignore when doing some kind of text processing. A dictionary would be a good use for a &lt;tt&gt;map&lt;/tt&gt;: The word would be the key, and its definition would be the value&lt;/p&gt; &lt;p&gt;An object of the &lt;tt&gt;map&lt;/tt&gt; or &lt;tt&gt;set&lt;/tt&gt; type may contain only a single element with a given key. There is no way to add a second element with the same key. If we need to have multiple instances with a single key, then we can use &lt;tt&gt;multimap&lt;/tt&gt; or &lt;tt&gt;multi set&lt;/tt&gt;, which do allow multiple elements with a given key&lt;/p&gt; &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td valign=&quot;top&quot;&gt; &lt;p&gt; &lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt; &lt;caption&gt; &lt;h5 class=&quot;docTableTitle&quot;&gt;Table 10.1. Associative Container Types&lt;/h5&gt;&lt;/caption&gt; &lt;colgroup&gt;&lt;strong&gt; &lt;col width=&quot;150&quot;&gt; &lt;col width=&quot;350&quot;&gt;&lt;/strong&gt;&lt;/colgroup&gt; &lt;thead&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;strong&gt;map&lt;/strong&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Associative array; elements stored and retrieved by key&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;strong&gt;set&lt;/strong&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Variable-sized collection with fast retrieval by key&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;strong&gt;multimap&lt;/strong&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;&lt;tt&gt;map&lt;/tt&gt; in which a key can appear multiple times&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;strong&gt;multiset&lt;/strong&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;&lt;tt&gt;set&lt;/tt&gt; in which a key can appear multiple times&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt; &lt;h4&gt;Section 10.1 Preliminaries: the pair Type&lt;/h4&gt; &lt;ol&gt; &lt;li&gt;Before we look at the associative containers, we need to know about a simple companion library type named pair, &lt;strong&gt;which is defined in the utility header.&lt;/strong&gt;  &lt;li&gt;Creating and Initializing pairs  &lt;ol&gt; &lt;li&gt;A &lt;tt&gt;pair&lt;/tt&gt; holds two data values. Like the containers, &lt;tt&gt;pair&lt;/tt&gt; is a template type. Unlike the containers we&#39;ve seen so far, we must supply two type names when we create a &lt;tt&gt;pair&lt;/tt&gt;: A &lt;tt&gt;pair&lt;/tt&gt; holds two data members, each of which has the corresponding named type  &lt;li&gt;When we create &lt;tt&gt;pair&lt;/tt&gt; objects with no initializer, the default constructor value-initializes the members  &lt;li&gt;The &lt;tt&gt;pair&lt;/tt&gt; type can be unwieldy to type, so when we wish to define a number of objects of the same &lt;tt&gt;pair&lt;/tt&gt; type, it is convenient to use a typedef  &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td valign=&quot;top&quot;&gt; &lt;p&gt; &lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt; &lt;caption&gt; &lt;h5 class=&quot;docTableTitle&quot;&gt;Table 10.2. Operations on &lt;tt&gt;pairs&lt;/tt&gt;&lt;/h5&gt;&lt;/caption&gt; &lt;colgroup&gt; &lt;col width=&quot;150&quot;&gt; &lt;col width=&quot;350&quot;&gt;&lt;/colgroup&gt; &lt;thead&gt;&lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;pair&amp;lt;T1, T2&amp;gt; p1;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Create an empty &lt;tt&gt;pair&lt;/tt&gt; with two elements of types &lt;tt&gt;T1&lt;/tt&gt; and &lt;tt&gt;T2&lt;/tt&gt;. The elements are value-initialized (&lt;a class=&quot;docLink&quot; href=&quot;ch03lev1sec3.html#ch03lev2sec6&quot;&gt;Section 3.3.1&lt;/a&gt;, p. &lt;a class=&quot;docLink&quot; href=&quot;ch03lev1sec3.html#ch03lev2sec6&quot;&gt;92)&lt;/a&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; colspan=&quot;2&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;pair&amp;lt;T1, T2&amp;gt; p1(v1, v2);&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&amp;nbsp;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Create a &lt;tt&gt;pair&lt;/tt&gt; with types &lt;tt&gt;T1&lt;/tt&gt; and &lt;tt&gt;T2&lt;/tt&gt; initializing the &lt;tt&gt;first&lt;/tt&gt; member from &lt;tt&gt;v1&lt;/tt&gt; and the &lt;tt&gt;second&lt;/tt&gt; from &lt;tt&gt;v2&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;make_pair(v1, v2)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Creates a new &lt;tt&gt;pair&lt;/tt&gt; from the values &lt;tt&gt;v1&lt;/tt&gt; and &lt;tt&gt;v2&lt;/tt&gt;. The type of the &lt;tt&gt;pair&lt;/tt&gt; is inferred from the types of &lt;tt&gt;v1&lt;/tt&gt; and &lt;tt&gt;v2&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;p1 &amp;lt; p2&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Less than between two pair objects. Less than is defined as dictionary ordering: Returns &lt;tt&gt;true&lt;/tt&gt; if &lt;tt&gt;p1.first &amp;lt; p2.first&lt;/tt&gt; or if &lt;tt&gt;!(p2.first &amp;lt; p1.first) &amp;amp;&amp;amp; p1.second &amp;lt; p2.second&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;p1 == p2&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Two &lt;tt&gt;pair&lt;/tt&gt;s are equal if their &lt;tt&gt;first&lt;/tt&gt; and &lt;tt&gt;second&lt;/tt&gt; members are respectively equal. Uses the underlying element &lt;tt&gt;==&lt;/tt&gt; operator.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;p.first&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Returns the (&lt;tt&gt;public&lt;/tt&gt;) data member of &lt;tt&gt;p&lt;/tt&gt; named &lt;tt&gt;first&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;p.second&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Returns the (&lt;tt&gt;public&lt;/tt&gt;) data member of &lt;tt&gt;p&lt;/tt&gt; named &lt;tt&gt;second&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;Operations on pairs  &lt;ol&gt; &lt;li&gt;Unlike other library types, the &lt;tt&gt;pair&lt;/tt&gt; class gives us direct access to its data members: Its members are &lt;tt&gt;public&lt;/tt&gt;  &lt;li&gt;&lt;tt&gt;&lt;/tt&gt;&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;Generating a New pair  &lt;ol&gt; &lt;li&gt;In addition to the constructors, the library defines the &lt;tt&gt;make_pair&lt;/tt&gt; function, which generates a new &lt;tt&gt;pair&lt;/tt&gt; from its two arguments. We might use this function to make a new &lt;tt&gt;pair&lt;/tt&gt; to assign to an existing &lt;tt&gt;pair&lt;/tt&gt;  &lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;pre&gt;&lt;span style=&quot;color: #008000&quot;&gt;// EXE-10.1.cpp : Defines the entry point for the console application.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;//&lt;/span&gt;&lt;br&gt;&lt;br&gt;#include &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;stdafx.h&lt;/span&gt;&quot;&lt;br&gt;#include &amp;lt;utility&amp;gt;&lt;br&gt;#include &amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;&lt;br&gt;#include &amp;lt;iostream&amp;gt;&lt;br&gt;#include &amp;lt;vector&amp;gt;&lt;br&gt;&lt;br&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;namespace&lt;/span&gt; std;&lt;br&gt;&lt;br&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; main()&lt;br&gt;{&lt;br&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;typedef&lt;/span&gt; pair&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;, &lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; Author;&lt;br&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Creating and Initializing pairs&lt;/span&gt;&quot;)&lt;br&gt;	{&lt;br&gt;		pair&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;, &lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; anon;       &lt;span style=&quot;color: #008000&quot;&gt;// holds two strings&lt;/span&gt;&lt;br&gt;		pair&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;, &lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; word_count;    &lt;span style=&quot;color: #008000&quot;&gt;// holds a string and an int&lt;/span&gt;&lt;br&gt;		pair&amp;lt; &lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;, vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; &amp;gt; line; &lt;span style=&quot;color: #008000&quot;&gt;// holds string and vector&amp;lt;int&amp;gt;&lt;/span&gt;&lt;br&gt;		pair&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;, &lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; author(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;James&lt;/span&gt;&quot;, &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Joyce&lt;/span&gt;&quot;);&lt;br&gt;		&lt;br&gt;		Author proust(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Marcel&lt;/span&gt;&quot;, &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Proust&lt;/span&gt;&quot;);&lt;br&gt;		Author joyce(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;James&lt;/span&gt;&quot;, &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Joyce&lt;/span&gt;&quot;);&lt;br&gt;&lt;br&gt;	}&lt;br&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Operations on pairs&lt;/span&gt;&quot;)&lt;br&gt;	{&lt;br&gt;		Author author(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;James&lt;/span&gt;&quot;,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Joyce&lt;/span&gt;&quot;);&lt;br&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; firstBook;&lt;br&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// access and test the data members of the pair&lt;/span&gt;&lt;br&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt; (author.first == &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;James&lt;/span&gt;&quot; &amp;amp;&amp;amp; author.second == &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Joyce&lt;/span&gt;&quot;)&lt;br&gt;			firstBook = &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Stephen Hero&lt;/span&gt;&quot;;&lt;br&gt;&lt;br&gt;	}&lt;br&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Generating a New pair&lt;/span&gt;&quot;)&lt;br&gt;	{&lt;br&gt;		pair&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;, &lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; next_auth;&lt;br&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; first, last;&lt;br&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt; (cin &amp;gt;&amp;gt; first &amp;gt;&amp;gt; last) {&lt;br&gt;			&lt;span style=&quot;color: #008000&quot;&gt;// generate a pair from first and last&lt;/span&gt;&lt;br&gt;			next_auth = make_pair(first, last);&lt;br&gt;&lt;br&gt;			&lt;span style=&quot;color: #008000&quot;&gt;// use pair constructor to make first and last into a pair&lt;/span&gt;&lt;br&gt;			&lt;span style=&quot;color: #008000&quot;&gt;//next_auth = pair&amp;lt;string, string&amp;gt;(first, last);&lt;/span&gt;&lt;br&gt;&lt;br&gt;			&lt;span style=&quot;color: #008000&quot;&gt;// process next_auth...&lt;/span&gt;&lt;br&gt;		}&lt;br&gt;	}&lt;br&gt;&lt;br&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; screen_hold;&lt;br&gt;	cin &amp;gt;&amp;gt; screen_hold;&lt;br&gt;&lt;br&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; 0;&lt;br&gt;}&lt;br&gt;&lt;/pre&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br&gt;&lt;br /&gt;&lt;h4&gt;Section 10.2 Associative Containers&lt;/h4&gt;&lt;br&gt;&lt;br /&gt;&lt;ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;The operations common to sequential and associative containers are &lt;br&gt;&lt;br /&gt;&lt;ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;The first three constructors described in Table 9.2 (p. 307): &lt;br&gt;&lt;br /&gt;&lt;li&gt;&lt;pre&gt;     C&amp;lt;T&amp;gt; c;          &lt;span style=&quot;color: #008000&quot;&gt;// creates an empty container&lt;/span&gt;&lt;br&gt;     &lt;span style=&quot;color: #008000&quot;&gt;// c2 must be same type as c1&lt;/span&gt;&lt;br&gt;     C&amp;lt;T&amp;gt; c1(c2);    &lt;span style=&quot;color: #008000&quot;&gt;// copies elements from c2 into c1&lt;/span&gt;&lt;br&gt;     &lt;span style=&quot;color: #008000&quot;&gt;// b and e are iterators denoting a sequence&lt;/span&gt;&lt;br&gt;     C&amp;lt;T&amp;gt; c(b, e);   &lt;span style=&quot;color: #008000&quot;&gt;// copies elements from the sequence into c&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;/pre&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;The relational operations described in &lt;a href=&quot;ch09lev1sec3.html#ch09lev2sec9&quot;&gt;Section 9.3.4&lt;/a&gt; (p. &lt;a href=&quot;ch09lev1sec3.html#ch09lev2sec9&quot;&gt;321&lt;/a&gt; &lt;br&gt;&lt;br /&gt;&lt;li&gt;The &lt;tt&gt;begin, end, rbegin&lt;/tt&gt;, and &lt;tt&gt;rend&lt;/tt&gt; operations of &lt;a href=&quot;ch09lev1sec3.html#ch09table06&quot;&gt;Table 9.6&lt;/a&gt; (p. &lt;a href=&quot;ch09lev1sec3.html#ch09table06&quot;&gt;317&lt;/a&gt; &lt;br&gt;&lt;br /&gt;&lt;li&gt;The typedefs listed in &lt;a href=&quot;ch09lev1sec3.html#ch09table05&quot;&gt;Table 9.5&lt;/a&gt; (p. &lt;a href=&quot;ch09lev1sec3.html#ch09table05&quot;&gt;316&lt;/a&gt;). Note that for &lt;tt&gt;map&lt;/tt&gt;, the &lt;tt&gt;value_type&lt;/tt&gt; is not the same as the element type. Instead, &lt;tt&gt;value_type&lt;/tt&gt; is a &lt;tt&gt;pair&lt;/tt&gt; representing the types of the keys and associated values. &lt;a href=&quot;ch10lev1sec3.html#ch10lev2sec6&quot;&gt;Section 10.3.2&lt;/a&gt; (p. &lt;a href=&quot;ch10lev1sec3.html#ch10lev2sec6&quot;&gt;361&lt;/a&gt;) explains the typedefs for &lt;tt&gt;map&lt;/tt&gt;s in more detail &lt;br&gt;&lt;br /&gt;&lt;li&gt;The &lt;tt&gt;swap&lt;/tt&gt; and assignment operator described in &lt;a href=&quot;ch09lev1sec3.html#ch09table11&quot;&gt;Table 9.11&lt;/a&gt; (p. &lt;a href=&quot;ch09lev1sec3.html#ch09table11&quot;&gt;329&lt;/a&gt;). Associative containers do not provide the &lt;tt&gt;assign&lt;/tt&gt; functions &lt;br&gt;&lt;br /&gt;&lt;li&gt;The &lt;tt&gt;clear&lt;/tt&gt; and &lt;tt&gt;erase&lt;/tt&gt; operations from &lt;a href=&quot;ch09lev1sec3.html#ch09table10&quot;&gt;Table 9.10&lt;/a&gt; (p. &lt;a href=&quot;ch09lev1sec3.html#ch09table10&quot;&gt;326&lt;/a&gt;), except that the &lt;tt&gt;erase&lt;/tt&gt; operation on an associative container returns &lt;tt&gt;void&lt;/tt&gt; &lt;br&gt;&lt;br /&gt;&lt;li&gt;The size operations in &lt;a href=&quot;ch09lev1sec3.html#ch09table08&quot;&gt;Table 9.8&lt;/a&gt; (p. &lt;a href=&quot;ch09lev1sec3.html#ch09table08&quot;&gt;324&lt;/a&gt;) except for &lt;tt&gt;resize&lt;/tt&gt;, which we cannot use on an associative container &lt;br&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;Elements Are Ordered by Key &lt;br&gt;&lt;br /&gt;&lt;ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;The associative container types define additional operations beyond the ones just listed. They also redefine the meaning or return type of operations that are in common with the sequential containers. The differences in these common operations reflect the use of keys in the associative containers &lt;br&gt;&lt;br /&gt;&lt;li&gt;There is one important consequence of the fact that elements are ordered by key: When we iterate across an associative container, we are guaranteed that the elements are accessed in key order, irrespective of the order in which the elements were placed in the container&lt;/li&gt;&lt;/ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br&gt;&lt;br /&gt;&lt;h4&gt;Section 10.3 The map Type&lt;/h4&gt;&lt;br&gt;&lt;br /&gt;&lt;ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;A &lt;tt&gt;map&lt;/tt&gt; is a collection of keyvalue pairs. The &lt;tt&gt;map&lt;/tt&gt; type is often referred to as an &lt;b&gt;&lt;a name=&quot;ch10term1&quot;&gt;&lt;/a&gt;&lt;a href=&quot;ch10lev1sec8.html#gloss10_01&quot;&gt;associative array&lt;/a&gt;&lt;/b&gt;: It is like the built-in array type, in that the key can be used as an index to fetch a value. It is associative in that values are associated with a particular key rather than being fetched by position in the array &lt;br&gt;&lt;br /&gt;&lt;li&gt;10.3.1. Defining a map &lt;br&gt;&lt;br /&gt;&lt;ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;To use a &lt;b&gt;&lt;a name=&quot;ch10term4&quot;&gt;&lt;/a&gt;&lt;a href=&quot;ch10lev1sec8.html#gloss10_04&quot;&gt;&lt;tt&gt;map&lt;/tt&gt;&lt;/a&gt;&lt;/b&gt;, we must include the &lt;tt&gt;map&lt;/tt&gt; header &lt;br&gt;&lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br&gt;&lt;br /&gt;&lt;tr&gt;&lt;br&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;p&gt;&lt;br&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;caption&gt;&lt;br&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 10.3. Constructors for &lt;tt&gt;map&lt;/tt&gt;&lt;/h5&gt;&lt;/caption&gt;&lt;br&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;175&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;325&quot;&gt;&lt;/colgroup&gt;&lt;br&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br&gt;&lt;br /&gt;&lt;tr&gt;&lt;br&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;map&amp;lt;k, v&amp;gt; m;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Create an empty &lt;tt&gt;map&lt;/tt&gt; named &lt;tt&gt;m&lt;/tt&gt; with key and value types &lt;tt&gt;k&lt;/tt&gt; and &lt;tt&gt;v&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br&gt;&lt;br /&gt;&lt;tr&gt;&lt;br&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;map&amp;lt;k, v&amp;gt; m(m2);&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Create &lt;tt&gt;m&lt;/tt&gt; as a copy of &lt;tt&gt;m2&lt;/tt&gt;; &lt;tt&gt;m&lt;/tt&gt; and &lt;tt&gt;m2&lt;/tt&gt; must have the same key and value types.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br&gt;&lt;br /&gt;&lt;tr&gt;&lt;br&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;map&amp;lt;k, v&amp;gt; m(b, e);&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Create &lt;tt&gt;m&lt;/tt&gt; as a copy of the elements from the range denoted by iterators &lt;tt&gt;b&lt;/tt&gt; and &lt;tt&gt;e&lt;/tt&gt;. Elements must have a type that can be converted to &lt;tt&gt;pair&amp;lt;const k, v&amp;gt;&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;Constraints on the Key Type &lt;br&gt;&lt;br /&gt;&lt;ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;Whenever we use an associative container, its keys have not only a type, but also an associated comparison function. By default, the library uses the &lt;tt&gt;&amp;lt;&lt;/tt&gt; operator for the &lt;a name=&quot;ch10term3&quot;&gt;&lt;/a&gt;&lt;a href=&quot;ch10lev1sec8.html#gloss10_03&quot;&gt;key type&lt;/a&gt; to compare the keys &lt;br&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;Whichever comparison function we use must define a &lt;/strong&gt;&lt;a name=&quot;ch10term10&quot;&gt;&lt;/a&gt;&lt;a href=&quot;ch10lev1sec8.html#gloss10_10&quot;&gt;&lt;strong&gt;strict weak ordering&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt; over the key type&lt;/strong&gt;. We can think of a strict weak ordering as &quot;less than,&quot; although we might choose to define a more complicated comparison function. &lt;strong&gt;However we define it, such a comparison function must always yield false when we compare a key with itself&lt;/strong&gt;. &lt;strong&gt;Moreover, if we compare two keys, they cannot both be &quot;less than&quot; each other, and if &lt;tt&gt;k1&lt;/tt&gt; is &quot;less than&quot; &lt;tt&gt;k2&lt;/tt&gt;, which in turn is &quot;less than&quot; &lt;tt&gt;k3&lt;/tt&gt;, then &lt;tt&gt;k1&lt;/tt&gt; must be &quot;less than&quot; &lt;tt&gt;k3&lt;/tt&gt;&lt;/strong&gt;. If we have two keys, neither of which is &quot;less than&quot; the other, the container will treat them as equal. When used as a key to a &lt;tt&gt;map&lt;/tt&gt;, either value could be used to access the corresponding element &lt;br&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;In practice, what&#39;s important is that the key type must define the &amp;lt; operator and that that operator should &quot;do the right thing.&quot;&lt;/strong&gt; &lt;br&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;The key type needs to support only the &lt;tt&gt;&amp;lt;&lt;/tt&gt; operator. There is no requirement that it support the other relational or equality operators&lt;/strong&gt; &lt;br&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;10.3.2. Types Defined by map &lt;br&gt;&lt;br /&gt;&lt;ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;The elements of a &lt;tt&gt;map&lt;/tt&gt; are keyvalue pairs That is, each element has two parts: its key and the value associated with that key. The &lt;tt&gt;value_type&lt;/tt&gt; for a &lt;tt&gt;map&lt;/tt&gt; reflects this fact. This type is more complicated than those we&#39;ve seen for other containers: &lt;tt&gt;value_type&lt;/tt&gt; is a &lt;tt&gt;pair&lt;/tt&gt; that holds the key and value of a given element. Moreover, the key is &lt;tt&gt;const&lt;/tt&gt; &lt;br&gt;&lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br&gt;&lt;br /&gt;&lt;tr&gt;&lt;br&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;p&gt;&lt;br&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;caption&gt;&lt;br&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 10.4. Types Defined by the &lt;tt&gt;map&lt;/tt&gt; Class&lt;/h5&gt;&lt;/caption&gt;&lt;br&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;200&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;325&quot;&gt;&lt;/colgroup&gt;&lt;br&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br&gt;&lt;br /&gt;&lt;tr&gt;&lt;br&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;map&amp;lt;K, V&amp;gt;::key_type&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;The type of the keys used to index the &lt;tt&gt;map&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br&gt;&lt;br /&gt;&lt;tr&gt;&lt;br&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;map&amp;lt;K, V&amp;gt;::mapped_type&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;The type of the values associated with the keys in the &lt;tt&gt;map&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br&gt;&lt;br /&gt;&lt;tr&gt;&lt;br&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;map&amp;lt;K, V&amp;gt;::value_type&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;A pair whose first element has type &lt;tt&gt;const&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br&gt;&lt;br /&gt;&lt;tr&gt;&lt;br&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;br&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;map&amp;lt;K, V&amp;gt;::key_type&lt;/tt&gt; and &lt;tt&gt;second&lt;/tt&gt; has type&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br&gt;&lt;br /&gt;&lt;tr&gt;&lt;br&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;br&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;map&amp;lt;K, V&amp;gt;::mapped_type&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;When learning the &lt;tt&gt;map&lt;/tt&gt; interface, it is essential to remember that the &lt;tt&gt;value_type&lt;/tt&gt; is a &lt;tt&gt;pair&lt;/tt&gt; and that we can change the value but not the key member of that &lt;tt&gt;pair&lt;/tt&gt;&lt;/strong&gt; &lt;br&gt;&lt;br /&gt;&lt;li&gt;Dereferencing a map Iterator Yields a pair &lt;br&gt;&lt;br /&gt;&lt;ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;When we dereference an iterator, we get a reference to a value of the container&#39;s &lt;tt&gt;value_type&lt;/tt&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;Additional map Typedefs&lt;br&gt;&lt;br /&gt;&lt;ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;The &lt;tt&gt;map&lt;/tt&gt; class defines two additional types, &lt;tt&gt;key_type&lt;/tt&gt; and &lt;b&gt;&lt;a name=&quot;ch10term5&quot;&gt;&lt;/a&gt;&lt;a href=&quot;ch10lev1sec8.html#gloss10_05&quot;&gt;&lt;tt&gt;mapped_type&lt;/tt&gt;&lt;/a&gt;&lt;/b&gt;, that let us access the type of either the key or the value&lt;br&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;10.3.3. Adding Elements to a map &lt;br&gt;&lt;br /&gt;&lt;ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;We can do so either by using the &lt;tt&gt;insert&lt;/tt&gt; member or by fetching an element using the subscript operator and then assigning a value to the element returned. In both cases, the fact that there can be only a single element for a given key affects the behavior of these operations&lt;br&gt;&lt;br /&gt;&lt;li&gt;The following steps take place&lt;br&gt;&lt;br /&gt;&lt;ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;word_count is searched for the element whose key is Anna. The element is not found.&lt;br&gt;&lt;br /&gt;&lt;li&gt;A new keyvalue pair is inserted into word_count. The key is a const string holding Anna. The value is value initialized, meaning in this case that the value is 0.&lt;br&gt;&lt;br /&gt;&lt;li&gt;The new keyvalue pair is inserted into word_count.&lt;br&gt;&lt;br /&gt;&lt;li&gt;The newly inserted element is fetched and is given the value 1.&lt;/li&gt;&lt;/ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;Subscripting a &lt;tt&gt;map&lt;/tt&gt; behaves quite differently from subscripting an array or &lt;tt&gt;vector&lt;/tt&gt;: Using an index that is not already present adds an element with that index to the &lt;tt&gt;map&lt;/tt&gt;&lt;/strong&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;As with other subscript operators, the &lt;tt&gt;map&lt;/tt&gt; subscript takes an index (that is, a key) and fetches the value associated with that key. When we look for a key that is already in the &lt;tt&gt;map&lt;/tt&gt;, then the behavior is the same for a &lt;tt&gt;map&lt;/tt&gt; subscript or a &lt;tt&gt;vector&lt;/tt&gt; subscript: The value associated with the key is returned. For &lt;tt&gt;map&lt;/tt&gt;s only, if the key is not already present, a new element is created and inserted into the &lt;tt&gt;map&lt;/tt&gt; for that key. The associated value is value-initialized: An element of class type is initialized using the default constructor for the element type; a built-in type is initialized to 0&lt;br&gt;&lt;br /&gt;&lt;li&gt;Using the Value Returned from a Subscript Operation&lt;br&gt;&lt;br /&gt;&lt;ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;Unlike &lt;tt&gt;vector&lt;/tt&gt; or &lt;tt&gt;string&lt;/tt&gt;, the type returned by &lt;tt&gt;map&lt;/tt&gt; subscript operator differs from the type obtained by dereferencing a &lt;tt&gt;map&lt;/tt&gt; iterator&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;Programming Implications of the Subscript Behavior&lt;/li&gt;&lt;/ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;10.3.4. Subscripting a map &lt;br&gt;&lt;br /&gt;&lt;li&gt;10.3.5. Using map::insert &lt;br&gt;&lt;br /&gt;&lt;ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;The &lt;tt&gt;insert&lt;/tt&gt; members operate similarly to the operations on sequential containers (&lt;a href=&quot;ch09lev1sec3.html#ch09lev2sec8&quot;&gt;Section 9.3.3&lt;/a&gt;, p. &lt;a href=&quot;ch09lev1sec3.html#ch09lev2sec8&quot;&gt;318&lt;/a&gt;), with one important caveat: &lt;strong&gt;We must account for the effect of the key&lt;/strong&gt;. &lt;br&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;The key impacts the argument types: The versions that insert a single element take a value that is a keyvalue &lt;tt&gt;pair&lt;/tt&gt;. Similarly, for the version that takes an iterator pair, the iterators must refer to elements that are keyvalue &lt;tt&gt;pair&lt;/tt&gt;s. The other difference is the return type from the version of &lt;tt&gt;insert&lt;/tt&gt; that takes a single value, which we will cover in the remainder of this section&lt;/strong&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;Using insert Instead of Subscripting&lt;br&gt;&lt;br /&gt;&lt;ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;Alternatively, we could insert the element directly by using the syntactically more intimidating &lt;tt&gt;insert&lt;/tt&gt; member&lt;br&gt;&lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br&gt;&lt;br /&gt;&lt;tr&gt;&lt;br&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;p&gt;&lt;br&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;caption&gt;&lt;br&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 10.5. &lt;tt&gt;insert&lt;/tt&gt; Operations on maps&lt;/h5&gt;&lt;/caption&gt;&lt;br&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;150&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;350&quot;&gt;&lt;/colgroup&gt;&lt;br&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br&gt;&lt;br /&gt;&lt;tr&gt;&lt;br&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;a name=&quot;idd1e78653&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e78661&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e78669&quot;&gt;&lt;/a&gt;&lt;tt&gt;m.insert(e)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;e&lt;/tt&gt; is a value of the &lt;tt&gt;value_type&lt;/tt&gt; for &lt;tt&gt;m&lt;/tt&gt;. If the key(&lt;tt&gt;e.first)&lt;/tt&gt; is not in &lt;tt&gt;m&lt;/tt&gt;, inserts a new element with value &lt;tt&gt;e.second&lt;/tt&gt;. &lt;strong&gt;If the key is in &lt;tt&gt;m&lt;/tt&gt;, then &lt;tt&gt;m&lt;/tt&gt; is unchanged. Returns a &lt;tt&gt;pair&lt;/tt&gt; containing a &lt;tt&gt;map&lt;/tt&gt; iterator referring to the element with key &lt;tt&gt;e.first&lt;/tt&gt; and a &lt;tt&gt;bool&lt;/tt&gt; indicating whether the element was inserted or not.&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br&gt;&lt;br /&gt;&lt;tr&gt;&lt;br&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;m.insert(beg, end)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;beg&lt;/tt&gt; and &lt;tt&gt;end&lt;/tt&gt; are iterators that denote a range of values that are keyvalue &lt;tt&gt;pair&lt;/tt&gt;s with the same type as &lt;tt&gt;m&lt;/tt&gt;&#39;s &lt;tt&gt;value_type&lt;/tt&gt;. For each element in the range, if the given key is not already in &lt;tt&gt;m&lt;/tt&gt;, it inserts the key and its associated value into &lt;tt&gt;m&lt;/tt&gt;. Returns &lt;tt&gt;void&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br&gt;&lt;br /&gt;&lt;tr&gt;&lt;br&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;m.insert(iter, e)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;e is&lt;/tt&gt; a value of the &lt;tt&gt;value_type&lt;/tt&gt; for &lt;tt&gt;m&lt;/tt&gt;. If the key(&lt;tt&gt;e.first)&lt;/tt&gt; is not in &lt;tt&gt;m&lt;/tt&gt;, inserts the new element using the iterator &lt;tt&gt;iter&lt;/tt&gt; as a hint for where to begin the search for where the new element should be stored. Returns an iterator that refers to the element in &lt;tt&gt;m&lt;/tt&gt; with given key.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;pre&gt;// if Anna not already in word_count, inserts new element with value 1&lt;br&gt;     word_count.insert(map&amp;lt;string, int&amp;gt;::value_type(&quot;Anna&quot;, 1));&lt;br&gt;&lt;/pre&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;Remember that &lt;tt&gt;value_type&lt;/tt&gt; is a synonym for the type &lt;tt&gt;pair&amp;lt;const K, V&amp;gt;&lt;/tt&gt;, where &lt;tt&gt;K&lt;/tt&gt; is the key type and &lt;tt&gt;V&lt;/tt&gt; is the type of the associated value. The argument to &lt;tt&gt;insert&lt;/tt&gt; constructs a new object of the appropriate &lt;tt&gt;pair&lt;/tt&gt; type to insert into the &lt;tt&gt;map&lt;/tt&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;By using &lt;tt&gt;insert&lt;/tt&gt;, we can avoid the extraneous initialization of the value that happens when we insert a new &lt;tt&gt;map&lt;/tt&gt; element as a side-effect of using a subscript&lt;br&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;Testing the Return from insert&lt;br&gt;&lt;br /&gt;&lt;ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;However, the version of &lt;tt&gt;insert&lt;/tt&gt; that takes a single keyvalue &lt;tt&gt;pair&lt;/tt&gt; does return a value. That value is a &lt;tt&gt;pair&lt;/tt&gt; that contains an iterator that refers to the element in the &lt;tt&gt;map&lt;/tt&gt; with the corresponding key, and a &lt;tt&gt;bool&lt;/tt&gt; that indicates whether the element was inserted&lt;/li&gt;&lt;/ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;Unwinding the Syntax&lt;/li&gt;&lt;/ol&gt;&lt;br&gt;&lt;br /&gt;&lt;li&gt;10.3.6. Finding and Retrieving a map Element &lt;br&gt;&lt;br /&gt;&lt;li&gt;10.3.7. Erasing Elements from a map &lt;br&gt;&lt;br /&gt;&lt;li&gt;10.3.8. Iterating across a map &lt;br&gt;&lt;br /&gt;&lt;li&gt;10.3.9. A Word Transformation Map&lt;/li&gt;&lt;/ol&gt;&lt;br&gt;&lt;br /&gt;&lt;h4&gt;&lt;b&gt;Section 10.4 The set Type&lt;/b&gt;&lt;/h4&gt;&lt;br /&gt;&lt;p&gt;1. In contrast, a set is simply a collection of keys &lt;br /&gt;&lt;p&gt;2. A set is most useful when we simply want to know whether a value is present &lt;br /&gt;&lt;p&gt;3. With two exceptions, set supports the same operations as map &lt;br /&gt;&lt;p&gt;o All the common container operations listed in &lt;a href=&quot;http://tech-thinker.blogspot.com/ch10lev1sec2.html#ch10lev1sec2&quot;&gt;Section 10.2&lt;/a&gt; (p. &lt;a href=&quot;http://tech-thinker.blogspot.com/ch10lev1sec2.html#ch10lev1sec2&quot;&gt;358&lt;/a&gt;). &lt;br /&gt;&lt;p&gt;o The constructors described in &lt;a href=&quot;http://tech-thinker.blogspot.com/ch10lev1sec3.html#ch10table03&quot;&gt;Table 10.3&lt;/a&gt; (p. &lt;a href=&quot;http://tech-thinker.blogspot.com/ch10lev1sec3.html#ch10table03&quot;&gt;360&lt;/a&gt;). &lt;br /&gt;&lt;p&gt;o The insert operations described in &lt;a href=&quot;http://tech-thinker.blogspot.com/ch10lev1sec3.html#ch10table05&quot;&gt;Table 10.5&lt;/a&gt; (p. &lt;a href=&quot;http://tech-thinker.blogspot.com/ch10lev1sec3.html#ch10table05&quot;&gt;365&lt;/a&gt;). &lt;br /&gt;&lt;p&gt;o The count and find operations described in &lt;a href=&quot;http://tech-thinker.blogspot.com/ch10lev1sec3.html#ch10table06&quot;&gt;Table 10.6&lt;/a&gt; (p. &lt;a href=&quot;http://tech-thinker.blogspot.com/ch10lev1sec3.html#ch10table06&quot;&gt;367&lt;/a&gt;). &lt;br /&gt;&lt;p&gt;o The erase operations described in &lt;a href=&quot;http://tech-thinker.blogspot.com/ch10lev1sec3.html#ch10table07&quot;&gt;Table 10.7&lt;/a&gt; (p. &lt;a href=&quot;http://tech-thinker.blogspot.com/ch10lev1sec3.html#ch10table07&quot;&gt;369&lt;/a&gt;). &lt;br /&gt;&lt;p&gt;4. The exceptions are that set does not provide a subscript operator and does not define mapped_type. In a set, the value_type is not a pair; instead it and key_type are the same type. They are each the type of the elements stored in the set &lt;br /&gt;&lt;p&gt;5. As with map, the keys of a set must be unique and may not be changed &lt;br /&gt;&lt;p&gt;6. 10.4.1. Defining and Using sets &lt;br /&gt;&lt;p&gt;1. &lt;b&gt;To use a &lt;/b&gt;&lt;b&gt;set&lt;/b&gt;&lt;b&gt;, we must include the &lt;/b&gt;&lt;b&gt;set&lt;/b&gt;&lt;b&gt; header. The operations on &lt;/b&gt;&lt;b&gt;set&lt;/b&gt;&lt;b&gt;s are essentially identical to those on &lt;/b&gt;&lt;b&gt;map&lt;/b&gt;&lt;b&gt;s&lt;/b&gt; &lt;br /&gt;&lt;p&gt;2. Adding Elements to a set &lt;br /&gt;&lt;p&gt;3. Fetching an Element from a set &lt;br /&gt;&lt;p&gt;1. There is no subscript operator on sets &lt;br /&gt;&lt;p&gt;2. To fetch an element from a set by its key, we use the find operation. If we just want to know whether the element is present, we could also use count, which returns the number of elements in the set with a given key &lt;br /&gt;&lt;p&gt;3. &lt;b&gt;Just as we cannot change the key part of a &lt;/b&gt;&lt;b&gt;map&lt;/b&gt;&lt;b&gt; element, the keys in a &lt;/b&gt;&lt;b&gt;set&lt;/b&gt;&lt;b&gt; are also &lt;/b&gt;&lt;b&gt;const&lt;/b&gt; &lt;br /&gt;&lt;p&gt;4. &lt;br /&gt;&lt;p&gt;4. &lt;br /&gt;&lt;p&gt;7. 10.4.2. Building a Word-Exclusion Set &lt;br /&gt;&lt;p&gt;8. &lt;br /&gt;&lt;h4&gt;&lt;b&gt;Section 10.5 The multimap and multiset Types&lt;/b&gt;&lt;b&gt;&lt;/b&gt;&lt;/h4&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;1. The types &lt;a name=&quot;ch10term7&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:C:\Users\v-pyin\Desktop\cprimeranswerbookidearesource\%5bBBservice%5d079.C＋＋_Primer.4th_Edition.SL-JL.%5bEN%5d.chm::/0201721481/ch10lev1sec8.html#gloss10_07&quot;&gt;&lt;u&gt;multiset&lt;/u&gt;&lt;/a&gt; and a &lt;a name=&quot;ch10term6&quot;&gt;&lt;/a&gt;&lt;a href=&quot;mk:@MSITStore:C:\Users\v-pyin\Desktop\cprimeranswerbookidearesource\%5bBBservice%5d079.C＋＋_Primer.4th_Edition.SL-JL.%5bEN%5d.chm::/0201721481/ch10lev1sec8.html#gloss10_06&quot;&gt;&lt;u&gt;multimap&lt;/u&gt;&lt;/a&gt; allow multiple instances of a key. &lt;br /&gt;&lt;li&gt;2. The multimap and multiset types are defined in the same headers as the corresponding single-element versions: the map and set headers, respectively &lt;br /&gt;&lt;li&gt;3. The operations supported by multimap and multiset are the same as those on map and set, respectively, with one exception: multimap does not support subscripting &lt;br /&gt;&lt;li&gt;4. We cannot subscript a multimap because there may be more than one value associated with a given key &lt;br /&gt;&lt;li&gt;&lt;tt&gt;5. &lt;/tt&gt;The &lt;tt&gt;insert&lt;/tt&gt; operations described in &lt;a href=&quot;mk:@MSITStore:C:\Users\v-pyin\Desktop\cprimeranswerbookidearesource\%5bBBservice%5d079.C＋＋_Primer.4th_Edition.SL-JL.%5bEN%5d.chm::/0201721481/ch10lev1sec3.html#ch10table05&quot;&gt;Table 10.5&lt;/a&gt; (p. &lt;a href=&quot;mk:@MSITStore:C:\Users\v-pyin\Desktop\cprimeranswerbookidearesource\%5bBBservice%5d079.C＋＋_Primer.4th_Edition.SL-JL.%5bEN%5d.chm::/0201721481/ch10lev1sec3.html#ch10table05&quot;&gt;365&lt;/a&gt;) and the &lt;tt&gt;erase&lt;/tt&gt; operations described in &lt;a href=&quot;mk:@MSITStore:C:\Users\v-pyin\Desktop\cprimeranswerbookidearesource\%5bBBservice%5d079.C＋＋_Primer.4th_Edition.SL-JL.%5bEN%5d.chm::/0201721481/ch10lev1sec3.html#ch10table07&quot;&gt;Table 10.7&lt;/a&gt; (p. &lt;a href=&quot;mk:@MSITStore:C:\Users\v-pyin\Desktop\cprimeranswerbookidearesource\%5bBBservice%5d079.C＋＋_Primer.4th_Edition.SL-JL.%5bEN%5d.chm::/0201721481/ch10lev1sec3.html#ch10table07&quot;&gt;369&lt;/a&gt;) are used to add and remove elements of a &lt;tt&gt;multimap&lt;/tt&gt; or &lt;tt&gt;multiset&lt;/tt&gt; &lt;br /&gt;&lt;li&gt;6. Because keys need not be unique, insert always adds an element &lt;br /&gt;&lt;li&gt;7. The version of erase that takes a key removes all elements with that key. It returns a count of how many elements were removed &lt;br /&gt;&lt;li&gt;8. The versions that take an iterator or an iterator pair remove only the indicated element(s). &lt;br /&gt;&lt;li&gt;10.5.1. Adding and Removing Elements &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;We noted that &lt;tt&gt;map&lt;/tt&gt;s and &lt;tt&gt;sets&lt;/tt&gt; store their elements in order &lt;br /&gt;&lt;li&gt;The &lt;tt&gt;multimap&lt;/tt&gt; and &lt;tt&gt;multiset&lt;/tt&gt; types do so as well. As a result, when a &lt;tt&gt;multimap&lt;/tt&gt; or &lt;tt&gt;multiset&lt;/tt&gt; has multiple instances of a given key, those instances will be adjacent elements within the container &lt;br /&gt;&lt;li&gt;We are guaranteed that iterating across a &lt;tt&gt;multimap&lt;/tt&gt; or &lt;tt&gt;multiset&lt;/tt&gt; returns all the elements with a given key in sequence&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;10.5.2. Finding Elements in a multimap or multiset &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;It turns out that there are three strategies we might use to find and print all the books by a given author. Each of these strategies relies on the fact that we know that all the entries for a given author will be adjacent within the &lt;tt&gt;multimap&lt;/tt&gt; &lt;br /&gt;&lt;li&gt;&lt;tt&gt;&lt;/tt&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;Using find and count &lt;br /&gt;&lt;li&gt;A Different, Iterator-Oriented Solution &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;They can be used with (plain) &lt;tt&gt;map&lt;/tt&gt;s or &lt;tt&gt;sets&lt;/tt&gt; but are most often used with &lt;tt&gt;multimap&lt;/tt&gt;s or &lt;tt&gt;multisets&lt;/tt&gt;. Each of these operations takes a key and returns an iterator &lt;br /&gt;&lt;li&gt;Calling &lt;tt&gt;lower_bound&lt;/tt&gt; and &lt;tt&gt;upper_bound&lt;/tt&gt; on the same key yields an iterator range (&lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch09lev1sec2.html#ch09lev2sec4&quot;&gt;Section 9.2.1&lt;/a&gt;, p. &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch09lev1sec2.html#ch09lev2sec4&quot;&gt;314&lt;/a&gt;) that denotes all the elements with that key. If the key is in the container, the iterators will differ: the one returned from &lt;tt&gt;lower_bound&lt;/tt&gt; will refer to the first instance of the key, whereas &lt;tt&gt;upper_bound&lt;/tt&gt; will return an iterator referring just after the last instance. If the element is not in the &lt;tt&gt;multimap&lt;/tt&gt;, then &lt;tt&gt;lower_bound&lt;/tt&gt; and &lt;tt&gt;upper_bound&lt;/tt&gt; will return equal iterators; both will refer to the point at which the key could be inserted without disrupting the order &lt;br /&gt;&lt;li&gt;The iterator returned from &lt;tt&gt;lower_bound&lt;/tt&gt; may or may not refer to an element with the given key. If the key is not in the container, then &lt;tt&gt;lower_bound&lt;/tt&gt; refers to the first point at which this key could be inserted while preserving the element order within the container &lt;br /&gt;&lt;li&gt;These operations say nothing about whether the key is present. The important point is that the return values act like an iterator range &lt;br /&gt;&lt;li&gt;If there is no element for this key, then &lt;tt&gt;lower_bound&lt;/tt&gt; and &lt;tt&gt;upper_bound&lt;/tt&gt; will be equal: They will both refer to the same element or they will both point one past the end of the &lt;tt&gt;multimap&lt;/tt&gt;. &lt;strong&gt;They both will refer to the point at which this key could be inserted while maintaining the container order&lt;/strong&gt; &lt;br /&gt;&lt;p style=&quot;line-height: normal&quot;&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;&lt;font color=&quot;#000000&quot; face=&quot;Times New Roman&quot;&gt;Table 10.8. Associative Container Operations Returning Iterators&lt;/font&gt;&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;200&quot;&gt;&lt;br /&gt;&lt;col width=&quot;300&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;200&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;a name=&quot;idd1e81984&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e81990&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e81997&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e82003&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e82009&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e82016&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e82023&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e82031&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e82041&quot;&gt;&lt;/a&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;m.lower_bound(k)&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;300&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;font color=&quot;#000000&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;Returns an iterator to the first element with key not less than &lt;/font&gt;&lt;tt&gt;k&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt;.&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;200&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;m.upper_bound(k)&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;300&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;font color=&quot;#000000&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;Returns an iterator to the first element with key greater than &lt;/font&gt;&lt;tt&gt;k&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt;.&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;200&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&lt;font color=&quot;#000000&quot;&gt;m.equal_range(k)&lt;/font&gt;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;300&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;font color=&quot;#000000&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;Returns a &lt;/font&gt;&lt;tt&gt;pair&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt; of iterators.&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;font color=&quot;#000000&quot;&gt;&lt;font face=&quot;Times New Roman&quot;&gt;The &lt;/font&gt;&lt;tt&gt;first&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt; member is equivalent to &lt;/font&gt;&lt;tt&gt;m.lower_bound(k)&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt; and &lt;/font&gt;&lt;tt&gt;second&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt; is equivalent to &lt;/font&gt;&lt;tt&gt;m.upper_bound(k)&lt;/tt&gt;&lt;font face=&quot;Times New Roman&quot;&gt;.&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;br&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;The equal_range Function &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Instead of calling &lt;tt&gt;upper_bound&lt;/tt&gt; and &lt;tt&gt;lower_bound&lt;/tt&gt;, we can call &lt;tt&gt;equal_range&lt;/tt&gt;. This function returns a &lt;tt&gt;pair&lt;/tt&gt; of iterators. If the value is present, then the first iterator refers to the first instance of the key and the second iterator refers one past the last instance of the key. If no matching element is found, then both the first and second iterator refer to the position where this key could be inserted &lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;h4&gt;&lt;b&gt;Section 10.6 Using Containers: Text-Query Program&lt;/b&gt;&lt;/h4&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;To conclude this chapter, we&#39;ll implement a simple text-query program &lt;br /&gt;&lt;li&gt;10.6.1. Design of the Query Program &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;A good way to start the design of a program is to list the program&#39;s operations. Knowing what operations we need to provide can then help us see what data structures we&#39;ll need and how we might implement those actions &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;It must allow the user to indicate the name of a file to process. The program will store the contents of the file so that we can display the original line in which each word appears &lt;br /&gt;&lt;li&gt;The program must break each line into words and remember all the lines in which each word appears. When it prints the line numbers, they should be presented in ascending order and contain no duplicates &lt;br /&gt;&lt;li&gt;The result of querying for a particular word should be the line numbers on which that word appeared &lt;br /&gt;&lt;li&gt;To print the text in which the word appeared, it must be possible to fetch the line from the input file corresponding to a given line number&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;10.6.2. TextQuery Class &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Our class will have two data members: a &lt;tt&gt;vector&lt;/tt&gt; to hold the input file and a &lt;tt&gt;map&lt;/tt&gt; to associate each input word with the &lt;tt&gt;set&lt;/tt&gt; of line numbers on which it appears &lt;br /&gt;&lt;li&gt;For the reasons described on page &lt;a href=&quot;mk:@MSITStore:D:%5CReadingList%5CC%20Primer%5C%5BBBservice%5D079.C??????_Primer.4th_Edition.SL-JL.%5BEN%5D.chm::/0201721481/ch03lev1sec1.html#ch03lev3sec1&quot;&gt;80&lt;/a&gt;, this class definition uses fully qualified &lt;tt&gt;std::&lt;/tt&gt; names for all references to library entities&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;10.6.3. Using the TextQuery Class &lt;br /&gt;&lt;li&gt;10.6.4. Writing the Member Functions&lt;/li&gt;&lt;/ol&gt;  </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/8302790814073711961/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/8302790814073711961?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/8302790814073711961'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/8302790814073711961'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2011/01/c-primer-fourth-editionnotes-chapter-10.html' title='C++ Primer, Fourth Edition:Notes Chapter 10 Associative Containers'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-2134777760324493455</id><published>2011-01-17T22:40:00.005-08:00</published><updated>2011-01-17T22:40:47.395-08:00</updated><title type='text'>C++ Primer, Fourth Edition Notes Chapter 18. Specialized Tools and Techniques</title><content type='html'>&lt;h4&gt;Section 18.1 Optimizing Memory Allocation&lt;/h4&gt; &lt;h4&gt;Section 18.2 Run-Time Type Identification&lt;/h4&gt; &lt;h4&gt;Section 18.3 Pointer to Class Member&lt;/h4&gt; &lt;h4&gt;Section 18.4 Nested Classes&lt;/h4&gt; &lt;h4&gt;Section 18.5 Union: A Space-Saving Class&lt;/h4&gt; &lt;h4&gt;Section 18.6 Local Classes&lt;/h4&gt; &lt;h4&gt;Section 18.7 Inherently Nonportable Features&lt;/h4&gt;  </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/2134777760324493455/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/2134777760324493455?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/2134777760324493455'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/2134777760324493455'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2011/01/c-primer-fourth-edition-notes-chapter_4303.html' title='C++ Primer, Fourth Edition Notes Chapter 18. Specialized Tools and Techniques'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-613530757528576447</id><published>2011-01-17T22:40:00.003-08:00</published><updated>2011-01-17T22:40:32.975-08:00</updated><title type='text'>C++ Primer, Fourth Edition Notes Chapter 17. Tools for Large Programs</title><content type='html'>&lt;p&gt;&lt;a href=&quot;#part05&quot;&gt;Part V&lt;/a&gt; covers additional features that, although useful in the right context, are not needed by every C++ programmer. These features divide into two clusters: those that are useful for large-scale problems and those that are applicable to specialized problems rather than general ones.  &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;h4&gt;Section 17.1 Exception Handling&lt;/h4&gt; &lt;h4&gt;Section 17.2 Namespaces&lt;/h4&gt; &lt;h4&gt;Section 17.3 Multiple and Virtual Inheritance&lt;/h4&gt;  </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/613530757528576447/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/613530757528576447?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/613530757528576447'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/613530757528576447'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2011/01/c-primer-fourth-edition-notes-chapter_17.html' title='C++ Primer, Fourth Edition Notes Chapter 17. Tools for Large Programs'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-5723318534996464354</id><published>2011-01-17T22:40:00.001-08:00</published><updated>2011-01-17T22:40:15.759-08:00</updated><title type='text'>C++ Primer, Fourth Edition Notes Chapter 15. Object-Oriented Programming</title><content type='html'>&lt;p&gt;Writing our own object-oriented or generic types requires a fairly good understanding of C++. Fortunately, we can use OO and generic types without understanding the details of how to build them. In fact, the standard library uses the facilities we&#39;ll study in &lt;a href=&quot;ch15.html#ch15&quot;&gt;Chapters 15&lt;/a&gt; and &lt;a href=&quot;ch16.html#ch16&quot;&gt;16&lt;/a&gt; extensively, and we&#39;ve used the library types and algorithms without needing to know how they are implemented. Readers, therefore, should understand that &lt;a href=&quot;#part04&quot;&gt;Part IV&lt;/a&gt; covers advanced topics. Writing templates or object-oriented classes requires a good understanding of the basics of C++ and a good grasp of how to define more basic classes&lt;/p&gt; &lt;h4&gt;Section 15.1 OOP: An Overview&lt;/h4&gt; &lt;h4&gt;Section 15.2 Defining Base and Derived Classes&lt;/h4&gt; &lt;h4&gt;Section 15.3 Conversions and Inheritance&lt;/h4&gt; &lt;h4&gt;Section 15.4 Constructors and Copy Control&lt;/h4&gt; &lt;h4&gt;Section 15.5 Class Scope under Inheritance&lt;/h4&gt; &lt;h4&gt;Section 15.6 Pure Virtual Functions&lt;/h4&gt; &lt;h4&gt;Section 15.7 Containers and Inheritance&lt;/h4&gt; &lt;h4&gt;Section 15.8 Handle Classes and Inheritance&lt;/h4&gt; &lt;h4&gt;Section 15.9 Text Queries Revisited&lt;/h4&gt;  </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/5723318534996464354/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/5723318534996464354?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/5723318534996464354'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/5723318534996464354'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2011/01/c-primer-fourth-edition-notes-chapter.html' title='C++ Primer, Fourth Edition Notes Chapter 15. Object-Oriented Programming'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-7445014328209420685</id><published>2011-01-17T22:38:00.001-08:00</published><updated>2011-01-17T22:38:27.108-08:00</updated><title type='text'>Reading Plan</title><content type='html'>&lt;p&gt;Reading plan  &lt;p&gt;C++ primer&lt;/p&gt; &lt;p&gt;&lt;br&gt;C++ - Data Structures And Algorithms With Object-oriented Design Patterns In C++3.Pratical Algorithms &lt;/p&gt; &lt;p&gt;Windows via C/C++, Fifth Edition &lt;/p&gt; &lt;p&gt;Practical.Algorithms.for.Programmers  &lt;p&gt;Writing Secure Code  &lt;p&gt;Programming.Pearls  &lt;p&gt;Mastering Algorithms with C  &lt;p&gt;Algorithms in a Nutshell &lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/7445014328209420685/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/7445014328209420685?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/7445014328209420685'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/7445014328209420685'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2011/01/reading-plan.html' title='Reading Plan'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-5386073027298314485</id><published>2011-01-17T21:55:00.001-08:00</published><updated>2011-01-17T21:55:07.618-08:00</updated><title type='text'>C++ Reserved or Non-graphic Characters</title><content type='html'>&lt;span style=&quot;text-align: ; widows: 2; text-transform: none; text-indent: 0px; letter-spacing: normal; border-collapse: separate; font: medium &#39;Times New Roman&#39;; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px&quot; class=&quot;Apple-style-span&quot;&gt; &lt;table border=&quot;1&quot;&gt; &lt;tbody&gt; &lt;tr valign=&quot;top&quot;&gt; &lt;td&gt;&lt;strong&gt;Character&lt;/strong&gt;&lt;/td&gt; &lt;td&gt;&lt;strong&gt;ASCII Representation&lt;/strong&gt;&lt;/td&gt; &lt;td&gt;&lt;strong&gt;ASCII Value&lt;/strong&gt;&lt;/td&gt; &lt;td&gt;&lt;strong&gt;Escape Sequence&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr valign=&quot;top&quot;&gt; &lt;td&gt;Newline&lt;/td&gt; &lt;td&gt;NL (LF)&lt;/td&gt; &lt;td&gt;10&lt;/td&gt; &lt;td&gt;\n&lt;/td&gt;&lt;/tr&gt; &lt;tr valign=&quot;top&quot;&gt; &lt;td&gt;Horizontal tab&lt;/td&gt; &lt;td&gt;HT&lt;/td&gt; &lt;td&gt;9&lt;/td&gt; &lt;td&gt;\t&lt;/td&gt;&lt;/tr&gt; &lt;tr valign=&quot;top&quot;&gt; &lt;td&gt;Vertical tab&lt;/td&gt; &lt;td&gt;VT&lt;/td&gt; &lt;td&gt;11&lt;/td&gt; &lt;td&gt;\v&lt;/td&gt;&lt;/tr&gt; &lt;tr valign=&quot;top&quot;&gt; &lt;td&gt;Backspace&lt;/td&gt; &lt;td&gt;BS&lt;/td&gt; &lt;td&gt;8&lt;/td&gt; &lt;td&gt;\b&lt;/td&gt;&lt;/tr&gt; &lt;tr valign=&quot;top&quot;&gt; &lt;td&gt;Carriage return&lt;/td&gt; &lt;td&gt;CR&lt;/td&gt; &lt;td&gt;13&lt;/td&gt; &lt;td&gt;\r&lt;/td&gt;&lt;/tr&gt; &lt;tr valign=&quot;top&quot;&gt; &lt;td&gt;Formfeed&lt;/td&gt; &lt;td&gt;FF&lt;/td&gt; &lt;td&gt;12&lt;/td&gt; &lt;td&gt;\f&lt;/td&gt;&lt;/tr&gt; &lt;tr valign=&quot;top&quot;&gt; &lt;td&gt;Alert&lt;/td&gt; &lt;td&gt;BEL&lt;/td&gt; &lt;td&gt;7&lt;/td&gt; &lt;td&gt;\a&lt;/td&gt;&lt;/tr&gt; &lt;tr valign=&quot;top&quot;&gt; &lt;td&gt;Backslash&lt;/td&gt; &lt;td&gt;\&lt;/td&gt; &lt;td&gt;92&lt;/td&gt; &lt;td&gt;\\&lt;/td&gt;&lt;/tr&gt; &lt;tr valign=&quot;top&quot;&gt; &lt;td&gt;Question mark&lt;/td&gt; &lt;td&gt;?&lt;/td&gt; &lt;td&gt;63&lt;/td&gt; &lt;td&gt;\?&lt;/td&gt;&lt;/tr&gt; &lt;tr valign=&quot;top&quot;&gt; &lt;td&gt;Single quotation mark&lt;/td&gt; &lt;td&gt;&#39;&lt;/td&gt; &lt;td&gt;39&lt;/td&gt; &lt;td&gt;\&#39;&lt;/td&gt;&lt;/tr&gt; &lt;tr valign=&quot;top&quot;&gt; &lt;td&gt;Double quotation mark&lt;/td&gt; &lt;td&gt;&quot;&lt;/td&gt; &lt;td&gt;34&lt;/td&gt; &lt;td&gt;\&quot;&lt;/td&gt;&lt;/tr&gt; &lt;tr valign=&quot;top&quot;&gt; &lt;td&gt;Octal number&lt;/td&gt; &lt;td&gt;&lt;em&gt;ooo&lt;/em&gt;&lt;/td&gt; &lt;td&gt;--&lt;/td&gt; &lt;td&gt;\&lt;em&gt;ooo&lt;/em&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr valign=&quot;top&quot;&gt; &lt;td&gt;Hexadecimal number&lt;/td&gt; &lt;td&gt;&lt;em&gt;hhh&lt;/em&gt;&lt;/td&gt; &lt;td&gt;--&lt;/td&gt; &lt;td&gt;\x&lt;em&gt;hhh&lt;/em&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr valign=&quot;top&quot;&gt; &lt;td&gt;Null character&lt;/td&gt; &lt;td&gt;NUL&lt;/td&gt; &lt;td&gt;0&lt;/td&gt; &lt;td&gt;\0&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/span&gt;  </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/5386073027298314485/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/5386073027298314485?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/5386073027298314485'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/5386073027298314485'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2011/01/c-reserved-or-non-graphic-characters.html' title='C++ Reserved or Non-graphic Characters'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-6939513248110463177</id><published>2011-01-17T21:53:00.001-08:00</published><updated>2011-01-17T21:53:57.025-08:00</updated><title type='text'>Pro.ASP.NET.4.in.CSharp.2010.4th-Introducing ASP.NET</title><content type='html'>&lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;The Seven Pillars of ASP.NET &lt;/p&gt; &lt;ol&gt; &lt;li&gt;ASP.NET Is Integrated with the .NET Framework  &lt;li&gt;ASP.NET Is Compiled, Not Interpreted  &lt;li&gt;ASP.NET Is Multilanguage  &lt;li&gt;ASP.NET Is Hosted by the Common Language Runtime  &lt;li&gt;ASP.NET Is Object-Oriented  &lt;li&gt;ASP.NET Supports all Browser  &lt;li&gt;ASP.NET Is Easy to Deploy and Configure &lt;/li&gt;&lt;/ol&gt; &lt;p&gt;The Evolution of ASP.NET &lt;/p&gt; &lt;ol&gt; &lt;li&gt;ASP.NET 1.0 and 1.1  &lt;li&gt;ASP.NET 2.0  &lt;li&gt;ASP.NET 3.5  &lt;li&gt;LINQ  &lt;li&gt;ASP.NET AJAX  &lt;li&gt;ASP.NET 4  &lt;li&gt;ASP.NET MVC  &lt;li&gt;ASP.NET Dynamic Data  &lt;li&gt;Silverlight &lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Visual Studio &lt;/p&gt; &lt;p&gt;Web Forms &lt;/p&gt; &lt;p&gt;Server Controls &lt;/p&gt; &lt;p&gt;ASP.NET Applications &lt;/p&gt; &lt;p&gt;State Management &lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/6939513248110463177/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/6939513248110463177?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/6939513248110463177'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/6939513248110463177'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2011/01/proaspnet4incsharp20104th-introducing.html' title='Pro.ASP.NET.4.in.CSharp.2010.4th-Introducing ASP.NET'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-4772590468918006711</id><published>2011-01-17T21:52:00.001-08:00</published><updated>2011-01-17T21:52:55.235-08:00</updated><title type='text'>Algorithms in a Nutshell-Note:Chapter 1: Algorithms Matter</title><content type='html'>&lt;p&gt;&lt;strong&gt;&lt;u&gt;It’s the question that drives us, Neo. It’s the question that brought you here.You know the question, just as I did.&lt;/u&gt;&lt;/strong&gt;  &lt;p&gt;We intend this book to be used frequently by experienced programmers looking for appropriate solutions to their problems. Here you will find solutions to the problems you must overcome as a programmer every day. You will learn what decisions lead to an improved performance of key algorithms that are essential for the success of your software applications. You will find real code that can be adapted to your needs and solution methods that you can learn.    </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/4772590468918006711/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/4772590468918006711?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/4772590468918006711'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/4772590468918006711'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2011/01/algorithms-in-nutshell-notechapter-1.html' title='Algorithms in a Nutshell-Note:Chapter 1: Algorithms Matter'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-5219690451896622320</id><published>2011-01-17T19:06:00.001-08:00</published><updated>2011-01-17T19:06:08.195-08:00</updated><title type='text'>C++ Primer, Fourth Edition Notes Chapter 9 Exercise</title><content type='html'>&lt;p&gt;&lt;a name=&quot;ch09qa1q1&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.1:&lt;/b&gt;  &lt;p&gt;Explain the following initializations. Indicate if any are in error, and if so, why.&lt;pre&gt;     int ia[7] = { 0, 1, 1, 2, 3, 5, 8 };&lt;br /&gt;     string sa[6] = {&lt;br /&gt;         &quot;Fort Sumter&quot;, &quot;Manassas&quot;, &quot;Perryville&quot;,&lt;br /&gt;         &quot;Vicksburg&quot;, &quot;Meridian&quot;, &quot;Chancellorsville&quot; };&lt;br /&gt;     (a) vector&amp;lt;string&amp;gt; svec(sa, sa+6);&lt;br /&gt;     (b) list&amp;lt;int&amp;gt; ilist( ia+4, ia+6);&lt;br /&gt;     (c) vector&amp;lt;int&amp;gt; ivec(ia, ia+8);&lt;br /&gt;     (d) list&amp;lt;string&amp;gt; slist(sa+6, sa);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa1q2&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.2:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Show an example of each of the four ways to create and initialize a &lt;tt&gt;vector&lt;/tt&gt;. Explain what values each &lt;tt&gt;vector&lt;/tt&gt; contains. &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa1q3&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.3:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Explain the differences between the constructor that takes a container to copy and the constructor that takes two iterators. &lt;br /&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa2q1&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.4:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Define a &lt;tt&gt;list&lt;/tt&gt; that holds elements that are &lt;tt&gt;deque&lt;/tt&gt;s that hold &lt;tt&gt;ints&lt;/tt&gt;. &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa2q2&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.5:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Why can we not have containers that hold &lt;tt&gt;iostream&lt;/tt&gt; objects? &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa2q3&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.6:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Given a class type named &lt;tt&gt;Foo&lt;/tt&gt; that does not define a default constructor but does define a constructor that takes &lt;tt&gt;int&lt;/tt&gt; values, define a &lt;tt&gt;list&lt;/tt&gt; of &lt;tt&gt;Foo&lt;/tt&gt; that holds 10 elements.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa3q1&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.7:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;idd1e64409&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e64416&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e64423&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e64430&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e64438&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e64443&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e64448&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e64454&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e64459&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e64462&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e64467&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e64477&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e64485&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e64498&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e64506&quot;&gt;&lt;/a&gt;What is wrong with the following program? How might you correct it?&lt;pre&gt;     list&amp;lt;int&amp;gt; lst1;&lt;br /&gt;     list&amp;lt;int&amp;gt;::iterator iter1 = lst1.begin(),&lt;br /&gt;                         iter2 = lst1.end();&lt;br /&gt;     while (iter1 &amp;lt; iter2) /* . . . */&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa3q2&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.8:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Assuming &lt;tt&gt;vec_iter&lt;/tt&gt; is bound to an element in a &lt;tt&gt;vector&lt;/tt&gt; that holds &lt;tt&gt;string&lt;/tt&gt;s, what does this statement do?&lt;pre&gt;     if (vec_iter-&amp;gt;empty()) /* . . . */&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa3q3&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.9:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Write a loop to write the elements of a &lt;tt&gt;list&lt;/tt&gt; in reverse order. &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa3q4&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.10:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Which, if any, of the following iterator uses are in error?&lt;pre&gt;     const vector&amp;lt; int &amp;gt; ivec(10);&lt;br /&gt;     vector&amp;lt; string &amp;gt;    svec(10);&lt;br /&gt;     list&amp;lt; int &amp;gt;         ilist(10);&lt;br /&gt;&lt;br /&gt;     (a) vector&amp;lt;int&amp;gt;::iterator    it = ivec.begin();&lt;br /&gt;     (b) list&amp;lt;int&amp;gt;::iterator      it = ilist.begin()+2;&lt;br /&gt;     (c) vector&amp;lt;string&amp;gt;::iterator it = &amp;amp;svec[0];&lt;br /&gt;     (d) for (vector&amp;lt;string&amp;gt;::iterator&lt;br /&gt;                  it = svec.begin(); it != 0; ++it)&lt;br /&gt;                     // ...&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa4q1&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.11:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;What are the constraints on the iterators that form iterator ranges? &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa4q2&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.12:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Write a function that takes a pair of iterators and an &lt;tt&gt;int&lt;/tt&gt; value. Look for that value in the range and return a &lt;tt&gt;bool&lt;/tt&gt; indicating whether it was found. &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa4q3&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.13:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Rewrite the program that finds a value to return an iterator that refers to the element. Be sure your function works correctly if the element does not exist. &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa4q4&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.14:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Using iterators, write a program to read a sequence of &lt;tt&gt;string&lt;/tt&gt;s from the standard input into a &lt;tt&gt;vector&lt;/tt&gt;. Print the elements in the &lt;tt&gt;vector&lt;/tt&gt;. &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa4q5&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.15:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Rewrite the program from the previous exercise to use a &lt;tt&gt;list&lt;/tt&gt;. List the changes you needed to change the container type. &lt;br /&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa5q1&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.16:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;What type should be used as the index into a &lt;tt&gt;vector&lt;/tt&gt; of &lt;tt&gt;int&lt;/tt&gt;s? &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa5q2&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.17:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;What type should be used to read the elments in a &lt;tt&gt;list&lt;/tt&gt; of &lt;tt&gt;string&lt;/tt&gt;s? &lt;br /&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa6q1&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.18:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;idd1e66969&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e66976&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e66983&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e66991&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e66999&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e67007&quot;&gt;&lt;/a&gt;Write a program to copy elements from a &lt;tt&gt;list&lt;/tt&gt; of &lt;tt&gt;int&lt;/tt&gt;s into two &lt;tt&gt;deque&lt;/tt&gt;s. The &lt;tt&gt;list&lt;/tt&gt; elements that are even should go into one &lt;tt&gt;deque&lt;/tt&gt; and those that are odd into the second. &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa6q2&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.19:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Assuming &lt;tt&gt;iv&lt;/tt&gt; is a &lt;tt&gt;vector&lt;/tt&gt; of &lt;tt&gt;int&lt;/tt&gt;s, what is wrong with the following program? How might you correct the problem(s)?&lt;pre&gt;     vector&amp;lt;int&amp;gt;::iterator mid = iv.begin() + iv.size()/2;&lt;br /&gt;     while (vector&amp;lt;int&amp;gt;::iterator iter != mid)&lt;br /&gt;         if (iter == some_val)&lt;br /&gt;             iv.insert(iter, 2 * some_val);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa7q3&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.20:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Write a program to compare whether a &lt;tt&gt;vector&amp;lt;int&amp;gt;&lt;/tt&gt; contains the same elements as a &lt;tt&gt;list&amp;lt;int&amp;gt;&lt;/tt&gt;. &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa7q4&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.21:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Assuming &lt;tt&gt;c1&lt;/tt&gt; and &lt;tt&gt;c2&lt;/tt&gt; are containers, what constraints does the following usage place on the element types in &lt;tt&gt;c1&lt;/tt&gt; and &lt;tt&gt;c2&lt;/tt&gt;?&lt;pre&gt;     if (c1 &amp;lt; c2)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;What, if any, constraints are there on &lt;tt&gt;c1&lt;/tt&gt; and &lt;tt&gt;c2&lt;/tt&gt;? &lt;br /&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa8q1&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.22:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Given that &lt;tt&gt;vec&lt;/tt&gt; holds 25 elements, what does &lt;tt&gt;vec.resize(100)&lt;/tt&gt; do? What if we next wrote &lt;tt&gt;vec.resize(10)?&lt;/tt&gt; &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa8q2&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.23:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;What, if any, restrictions does using &lt;tt&gt;resize&lt;/tt&gt; with a single size argument place on the element types? &lt;br /&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa9q1&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.24:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Write a program that fetches the first element in a &lt;tt&gt;vector&lt;/tt&gt;. Do so using &lt;tt&gt;at&lt;/tt&gt;, the subscript operator, &lt;tt&gt;front&lt;/tt&gt;, and &lt;tt&gt;begin&lt;/tt&gt;. Test the program on an empty &lt;tt&gt;vector&lt;/tt&gt;.&lt;/p&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa10q1&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.25:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;What happens in the program that erased a range of elements if &lt;tt&gt;val1&lt;/tt&gt; is equal to &lt;tt&gt;val2&lt;/tt&gt;. What happens if either &lt;tt&gt;val1&lt;/tt&gt; or &lt;tt&gt;val2&lt;/tt&gt; or both are not present. &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa10q2&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.26:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Using the following definition of &lt;tt&gt;ia&lt;/tt&gt;, copy &lt;tt&gt;ia&lt;/tt&gt; into a &lt;tt&gt;vector&lt;/tt&gt; and into a &lt;tt&gt;list&lt;/tt&gt;. Use the single iterator form of &lt;tt&gt;erase&lt;/tt&gt; to remove the elements with odd values from your &lt;tt&gt;list&lt;/tt&gt; and the even values from your &lt;tt&gt;vector&lt;/tt&gt;.&lt;pre&gt;     int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa10q3&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.27:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Write a program to process a &lt;tt&gt;list&lt;/tt&gt; of &lt;tt&gt;string&lt;/tt&gt;s. Look for a particular value and, if found, remove it. Repeat the program using a &lt;tt&gt;deque&lt;/tt&gt;. &lt;br /&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa11q1&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.28:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Write a program to assign the elements from a &lt;tt&gt;list&lt;/tt&gt; of &lt;tt&gt;char*&lt;/tt&gt; pointers to C-style character strings to a &lt;tt&gt;vector&lt;/tt&gt; of &lt;tt&gt;string&lt;/tt&gt;s. &lt;br /&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa12q1&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.29:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Explain the difference between a &lt;tt&gt;vector&lt;/tt&gt;&#39;s capacity and its size. Why is it necessary to support the notion of capacity in a container that stores elements contiguously but not, for example, in a &lt;tt&gt;list&lt;/tt&gt;? &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa12q2&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.30:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Write a program to explore the allocation stragegy followed by the library you use for &lt;tt&gt;vector&lt;/tt&gt; objects. &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa12q3&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.31:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Can a container have a capacity less than its size? Is a capacity equal to its size desirable? Initially? After an element is inserted? Why or why not? &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa12q4&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.32:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Explain what the following program does:&lt;pre&gt;     vector&amp;lt;string&amp;gt; svec;&lt;br /&gt;     svec.reserve(1024);&lt;br /&gt;     string text_word;&lt;br /&gt;     while (cin &amp;gt;&amp;gt; text_word)&lt;br /&gt;             svec.push_back(text_word);&lt;br /&gt;     svec.resize(svec.size()+svec.size()/2);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;If the program reads 256 words, what is its likely capacity after it is resized? What if it reads 512? 1,000? 1,048?&lt;/p&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa13q1&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.33:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;idd1e70845&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e70852&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e70859&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e70866&quot;&gt;&lt;/a&gt;Which is the most appropriatea &lt;tt&gt;vector&lt;/tt&gt;, a &lt;tt&gt;deque&lt;/tt&gt;, or a &lt;tt&gt;list&lt;/tt&gt;for the following program tasks? Explain the rationale for your choice. If there is no reason to prefer one or another container explain why not? &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;Read an unknown number of words from a file for the purpose of generating English language sentences.&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;Read a fixed number of words, inserting them in the container alphabetically as they are entered. We&#39;ll see in the next chapter that associative containers are better suited to this problem.&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;Read an unknown number of words. Always insert new words at the back. Remove the next value from the front.&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;Read an unknown number of integers from a file. Sort the numbers and then print them to standard output.&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa14q1&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.34:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Use iterators to change the characters in a &lt;tt&gt;string&lt;/tt&gt; to uppercase. &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa14q2&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.35:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Use iterators to find and to erase each capital letter from a &lt;tt&gt;string&lt;/tt&gt;. &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa14q3&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.36:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Write a program that initializes a &lt;tt&gt;string&lt;/tt&gt; from a &lt;tt&gt;vector&amp;lt;char&amp;gt;&lt;/tt&gt;. &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa14q4&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.37:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Given that you want to read a character at a time into a &lt;tt&gt;string&lt;/tt&gt;, and you know that the data you need to read is at least 100 characters long, how might you improve the performance of your program? &lt;br /&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa15q1&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.38:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;idd1e73744&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e73752&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e73760&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e73767&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e73776&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e73785&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e73794&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e73803&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e73810&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e73816&quot;&gt;&lt;/a&gt;Write a program that, given the &lt;tt&gt;string&lt;/tt&gt;&lt;pre&gt;     &quot;ab2c3d7R4E6&quot;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;finds each numeric character and then each alphabetic character. Write two versions of the program. The first should use &lt;tt&gt;find_first_of&lt;/tt&gt;, and the second &lt;tt&gt;find_first_not_of&lt;/tt&gt;. &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa15q2&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.39:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Write a program that, given the &lt;tt&gt;string&lt;/tt&gt;s&lt;pre&gt;     string line1 = &quot;We were her pride of 10 she named us:&quot;;&lt;br /&gt;     string line2 = &quot;Benjamin, Phoenix, the Prodigal&quot;&lt;br /&gt;     string line3 = &quot;and perspicacious pacific Suzanne&quot;;&lt;br /&gt;&lt;br /&gt;     string sentence = line1 + &#39; &#39; + line2 + &#39; &#39; + line3;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;counts the number of words in &lt;tt&gt;sentence&lt;/tt&gt; and identifies the largest and smallest words. If several words have the largest or smallest length, report all of them. &lt;br /&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa16q1&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.40:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;idd1e74460&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e74466&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e74472&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e74479&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e74485&quot;&gt;&lt;/a&gt;Write a program that accepts the following two &lt;tt&gt;string&lt;/tt&gt;s:&lt;pre&gt;     string q1(&quot;When lilacs last in the dooryard bloom&#39;d&quot;);&lt;br /&gt;     string q2(&quot;The child is father of the man&quot;);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Using the &lt;tt&gt;assign&lt;/tt&gt; and &lt;tt&gt;append&lt;/tt&gt; operations, create the &lt;tt&gt;string&lt;/tt&gt;&lt;pre&gt;     string sentence(&quot;The child is in the dooryard&quot;);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa16q2&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.41:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Write a program that, given the &lt;tt&gt;string&lt;/tt&gt;s&lt;pre&gt;     string generic1(&quot;Dear Ms Daisy:&quot;);&lt;br /&gt;     string generic2(&quot;MrsMsMissPeople&quot;);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;implements the function&lt;pre&gt;     string greet(string form, string lastname, string title,&lt;br /&gt;                  string::size_type pos, int length);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;using the &lt;tt&gt;replace&lt;/tt&gt; operations, where &lt;tt&gt;lastname&lt;/tt&gt; replaces &lt;tt&gt;Daisy&lt;/tt&gt; and &lt;tt&gt;pos&lt;/tt&gt; indexes into &lt;tt&gt;generic2&lt;/tt&gt; of &lt;tt&gt;length&lt;/tt&gt; characters replacing &lt;tt&gt;Ms&lt;/tt&gt;. For example, the following&lt;pre&gt;     string lastName(&quot;AnnaP&quot;);&lt;br /&gt;     string salute = greet(generic1, lastName, generic2, 5, 4);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;returns the &lt;tt&gt;string&lt;/tt&gt;&lt;pre&gt;     Dear Miss AnnaP:&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa17q1&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.42:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Write a program to read a series of words into a &lt;tt&gt;stack&lt;/tt&gt;. &lt;br /&gt;&lt;p&gt;&lt;a name=&quot;ch09qa17q2&quot;&gt;&lt;/a&gt;&lt;b&gt;Exercise 9.43:&lt;/b&gt; &lt;br /&gt;&lt;p&gt;Use a &lt;tt&gt;stack&lt;/tt&gt; to process parenthesized expressions. When you see an open parenthesis, note that it was seen. When you see a close parenthesis after an open parenthesis, &lt;tt&gt;pop&lt;/tt&gt; elements down to and including the open parenthesis off the &lt;tt&gt;stack&lt;/tt&gt;. &lt;tt&gt;push&lt;/tt&gt; a value onto the &lt;tt&gt;stack&lt;/tt&gt; to indicate that a parenthesized expression was replaced&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/5219690451896622320/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/5219690451896622320?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/5219690451896622320'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/5219690451896622320'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2011/01/c-primer-fourth-edition-notes-chapter-9_17.html' title='C++ Primer, Fourth Edition Notes Chapter 9 Exercise'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-2249632846686747551</id><published>2011-01-17T18:16:00.001-08:00</published><updated>2011-01-17T18:16:33.838-08:00</updated><title type='text'>C++ Primer, Fourth Edition Notes Chapter 9 Sequential Containers</title><content type='html'>&lt;p&gt;We&#39;ve said that C++ is about efficient programming with abstractions. The Standard Library is a good example: The library defines a number of container classes and a family of generic algorithms that let us write programs that are succinct, abstract, and efficient. The library worries about bookkeeping detailsin particular, taking care of memory managementso that our programs can worry about the actual problems we need to solve&lt;/p&gt; &lt;p&gt;The library is designed so that the container types provide a common interface: If two containers offer a similar operation, then that operation will be defined identically for both containers&lt;/p&gt; &lt;p&gt;Because the container operations and algorithms are defined consistently, learning the library becomes easier: Once you understand how an operation works, you can apply that same operation to other containers. More importantly, this commonality of interface leads to more flexible programs. It is often possible to take a program written to use one container type and change it to use a different container without having to rewrite code. As we&#39;ll see, the containers offer different performance tradeoffs, and the ability to change container types can be valuable when fine-tuning the performance of a system&lt;/p&gt; &lt;p&gt;Each container type offers a different set of time and functionality tradeoffs.&lt;/p&gt; &lt;p&gt;The library defines three kinds of sequential containers: &lt;strong&gt;&lt;tt&gt;vector, list&lt;/tt&gt;, and &lt;/strong&gt;&lt;a name=&quot;ch09term4&quot;&gt;&lt;/a&gt;&lt;a href=&quot;ch09lev1sec9.html#gloss09_04&quot;&gt;&lt;tt&gt;&lt;strong&gt;deque&lt;/strong&gt;&lt;/tt&gt;&lt;/a&gt; (short for &quot;double-ended queue&quot; and pronounced &quot;deck&quot;). These types differ in how elements are accessed and the relative run-time cost of adding or removing element. The library also provides three container adaptors. Effectively, an adaptor adapts an underlying container type by defining a new interface in terms of the operations provided by the original type. The sequential container adaptors are &lt;a name=&quot;ch09term14&quot;&gt;&lt;/a&gt;&lt;a href=&quot;ch09lev1sec9.html#gloss09_14&quot;&gt;&lt;tt&gt;&lt;strong&gt;stack&lt;/strong&gt;&lt;/tt&gt;&lt;/a&gt;&lt;strong&gt;, &lt;tt&gt;queue&lt;/tt&gt;, and &lt;tt&gt;priority_queue&lt;/tt&gt;.&lt;/strong&gt;&lt;/p&gt; &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td valign=&quot;top&quot;&gt; &lt;p&gt; &lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt; &lt;caption&gt; &lt;h5 class=&quot;docTableTitle&quot;&gt;Table 9.1. Sequential Container Types&lt;/h5&gt;&lt;/caption&gt; &lt;colgroup&gt; &lt;col width=&quot;200&quot;&gt; &lt;col width=&quot;300&quot;&gt;&lt;/colgroup&gt; &lt;thead&gt; &lt;tr&gt; &lt;th class=&quot;thead&quot; valign=&quot;top&quot; scope=&quot;col&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphasis&quot;&gt;Sequential Containers&lt;/span&gt;&lt;/p&gt;&lt;/th&gt; &lt;th class=&quot;thead&quot; valign=&quot;top&quot; scope=&quot;col&quot; align=&quot;left&quot;&gt;&amp;nbsp;&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;vector&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Supports fast random access&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;list&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Supports fast insertion/deletion&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;deque&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Double-ended queue&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; colspan=&quot;2&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphBoldItalic&quot;&gt;Sequential Container Adaptors&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;stack&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Last in/First out stack&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;queue&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;First in/First out queue&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;priority_queue&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Priority-managed queue&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt; &lt;h4&gt;Section 9.1 Defining a Sequential Container &lt;/h4&gt; &lt;ol&gt; &lt;li&gt;For reasons that shall become clear shortly, the most commonly used container constructor is the default constructor. In most programs, using the default constructor gives the best run-time performance and makes using the container easier.  &lt;li&gt;9.1.1. Initializing Container Elements  &lt;ol&gt; &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td valign=&quot;top&quot;&gt; &lt;p&gt; &lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt; &lt;caption&gt; &lt;h5 class=&quot;docTableTitle&quot;&gt;Table 9.2. Container Constructors&lt;/h5&gt;&lt;/caption&gt; &lt;colgroup&gt; &lt;col width=&quot;125&quot;&gt; &lt;col width=&quot;400&quot;&gt;&lt;/colgroup&gt; &lt;thead&gt;&lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;C&amp;lt;T&amp;gt; c;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Create an empty container named &lt;tt&gt;c&lt;/tt&gt;. &lt;tt&gt;C&lt;/tt&gt; is a container name, such as &lt;tt&gt;vector&lt;/tt&gt;, and &lt;tt&gt;T&lt;/tt&gt; is the element type, such as &lt;tt&gt;int&lt;/tt&gt; or &lt;tt&gt;string&lt;/tt&gt;. Valid for all containers.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;C c(c2);&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Create &lt;tt&gt;c&lt;/tt&gt; as a copy of container &lt;tt&gt;c2&lt;/tt&gt;; &lt;tt&gt;c&lt;/tt&gt; and &lt;tt&gt;c2&lt;/tt&gt; must be the same container type and hold values of the same type. Valid for all containers.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;C c(b, e);&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Create &lt;tt&gt;c&lt;/tt&gt; with a copy of the elements from the range denoted by iterators &lt;tt&gt;b&lt;/tt&gt; and &lt;tt&gt;e&lt;/tt&gt;. Valid for all containers.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;C c(n, t);&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Create &lt;tt&gt;c&lt;/tt&gt; with &lt;tt&gt;n&lt;/tt&gt; elements, each with value &lt;tt&gt;t&lt;/tt&gt;, which must be a value of the element type of &lt;tt&gt;C&lt;/tt&gt; or a type convertible to that type.&lt;/p&gt; &lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;Sequential containers only.&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;C c(n);&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Create &lt;tt&gt;c&lt;/tt&gt; with &lt;tt&gt;n&lt;/tt&gt; value-initialized (&lt;a class=&quot;docLink&quot; href=&quot;ch03lev1sec3.html#ch03lev2sec6&quot;&gt;Section 3.3.1&lt;/a&gt;, p. &lt;a class=&quot;docLink&quot; href=&quot;ch03lev1sec3.html#ch03lev2sec6&quot;&gt;92&lt;/a&gt;) elements.&lt;/p&gt; &lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;Sequential containers only.&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt; &lt;li&gt;Intializing a Container as a Copy of Another Container  &lt;ol&gt; &lt;li&gt;When we initialize a sequential container using any constructor other than the default constructor, we must indicate how many elements the container will have. We must also supply initial values for those elements  &lt;li&gt;&lt;strong&gt;When we copy one container into another, the types must match exactly: The container type and element type must be the same&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;Initializing as a Copy of a Range of Elements  &lt;ol&gt; &lt;li&gt;Although we cannot copy the elements from one kind of container to another directly, we can do so indirectly by passing a pair of iterators (&lt;a href=&quot;ch03lev1sec4.html#ch03lev1sec4&quot;&gt;Section 3.4&lt;/a&gt;, p. &lt;a href=&quot;ch03lev1sec4.html#ch03lev1sec4&quot;&gt;95&lt;/a&gt;). When we use iterators, there is no requirement that the container types be identical. The element types in the containers can differ as long as they are compatible. It must be possible to convert the element we copy into the type held by the container we are constructing  &lt;li&gt;The iterators denote a range of elements that we want to copy. These elements are used to initialize the elements of the new container. &lt;strong&gt;The iterators mark the first and one past the last element to be copied.&lt;/strong&gt; We can use this form of initialization to copy a container that we could not copy directly. More importantly, we can use it to copy only a subsequence of the other container  &lt;li&gt;Recall that pointers are iterators, so it should not be surprising that we can initialize a container from a pair of pointers into a built-in array  &lt;ol&gt;&lt;pre&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;char&lt;/span&gt; *words[] = {&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;stately&lt;/span&gt;&quot;, &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;plump&lt;/span&gt;&quot;, &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;buck&lt;/span&gt;&quot;, &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;mulligan&lt;/span&gt;&quot;};&lt;br /&gt;&lt;br /&gt;     &lt;span style=&quot;color: #008000&quot;&gt;// calculate how many elements in words&lt;/span&gt;&lt;br /&gt;     &lt;strong&gt;size_t words_size = &lt;span style=&quot;color: #0000ff&quot;&gt;sizeof&lt;/span&gt;(words)/&lt;span style=&quot;color: #0000ff&quot;&gt;sizeof&lt;/span&gt;(&lt;span style=&quot;color: #0000ff&quot;&gt;char&lt;/span&gt; *);&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;     &lt;span style=&quot;color: #008000&quot;&gt;// use entire array to initialize words2&lt;/span&gt;&lt;br /&gt;     list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; words2(words, words + words_size);&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/ol&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Allocating and Initializing a Specified Number of Elements &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;li&gt;The constructors that take a size are valid only for sequential containers; they are not supported for the associative containers&lt;/li&gt;&lt;/ol&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;li&gt;9.1.2. Constraints on Types that a Container Can Hold &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;While most types can be used as the element type of a container, there are two constraints that element types must meet:&lt;/strong&gt; &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;The element type must support assignment.&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;We must be able to copy objects of the element type.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;Most types meet these minimal element type requirements. All of the built-in or compound types, with the exception of references, can be used as the element &lt;/strong&gt;&lt;a name=&quot;idd1e63613&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e63619&quot;&gt;&lt;/a&gt;&lt;strong&gt;type&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;References do not support assignment in its ordinary meaning, so we cannot have containers of references &lt;br /&gt;&lt;li&gt;With the exception of the IO library types (and the &lt;tt&gt;auto_ptr&lt;/tt&gt; type, which we cover in &lt;a href=&quot;ch17lev1sec1.html#ch17lev2sec9&quot;&gt;Section 17.1.9&lt;/a&gt; (p. &lt;a href=&quot;ch17lev1sec1.html#ch17lev2sec9&quot;&gt;702&lt;/a&gt;)), all the library types are valid container element types &lt;br /&gt;&lt;li&gt;The IO library types do not support copy or assignment. Therefore, we cannot have a container that holds objects of the IO types &lt;br /&gt;&lt;li&gt;Container Operations May Impose Additional Requirements &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;In addition, some container operations impose additional requirements on the element type. If the element type doesn&#39;t support the additional requirement, then we cannot perform that operation: We can define a container of that type but may not use that particular operation&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Containers of Containers &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Note the spacing used when specifying a container element type as a container &lt;br /&gt;&lt;ol&gt;&lt;pre&gt; vector&amp;lt; vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; &amp;gt; lines; &lt;span style=&quot;color: #008000&quot;&gt;// ok: space required between close &amp;gt;&lt;/span&gt;&lt;br /&gt; vector&amp;lt; vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;&amp;gt; lines; &lt;span style=&quot;color: #008000&quot;&gt;// error: &amp;gt;&amp;gt; treated as shift operator&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;We must separate the two closing &lt;tt&gt;&amp;gt;&lt;/tt&gt; symbols with a space to indicate that these two characters represent two symbols. Without the space, &lt;tt&gt;&amp;gt;&amp;gt;&lt;/tt&gt; is treated as a single symbol, the right shift operator, and results in a compile-time error&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;pre&gt;&lt;span style=&quot;color: #008000&quot;&gt;// EXE-9.1.cpp : Defines the entry point for the console application.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #008000&quot;&gt;//&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;#include &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;stdafx.h&lt;/span&gt;&quot;&lt;br /&gt;#include &amp;lt;vector&amp;gt;&lt;br /&gt;#include &amp;lt;list&amp;gt;&lt;br /&gt;#include &amp;lt;deque&amp;gt;&lt;br /&gt;#include &amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;&lt;br /&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;namespace&lt;/span&gt; std;&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; Foo&lt;br /&gt;{&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt;:&lt;br /&gt;	Foo( &lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; size)&lt;br /&gt;	{&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;this&lt;/span&gt;-&amp;gt;size = size;&lt;br /&gt;	}&lt;br /&gt;	Foo():size(0){};&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; Foo::PrintOut();&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;protected&lt;/span&gt;:&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;private&lt;/span&gt;:&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; size;&lt;br /&gt;};&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; main(&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; argc, _TCHAR* argv[])&lt;br /&gt;{&lt;br /&gt;	vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;    svec;       &lt;span style=&quot;color: #008000&quot;&gt;// empty vector that can hold strings&lt;/span&gt;&lt;br /&gt;	list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt;         ilist;      &lt;span style=&quot;color: #008000&quot;&gt;// empty list that can hold ints&lt;/span&gt;&lt;br /&gt;	&lt;span style=&quot;color: #008000&quot;&gt;//deque&amp;lt;Sales_item&amp;gt; items;      // empty deque that holds Sales_items&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Intializing a Container as a Copy of Another Container&lt;/span&gt;&quot;)&lt;br /&gt;	{&lt;br /&gt;		vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; ivec;&lt;br /&gt;		vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; ivec2(ivec);   &lt;span style=&quot;color: #008000&quot;&gt;// ok: ivec is vector&amp;lt;int&amp;gt;&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//list&amp;lt;int&amp;gt;   ilist(ivec);   // error: ivec is not list&amp;lt;int&amp;gt;&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//vector&amp;lt;double&amp;gt; dvec(ivec); // error: ivec holds int not double&lt;/span&gt;&lt;br /&gt;		&lt;br /&gt;	}&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Initializing as a Copy of a Range of Elements&lt;/span&gt;&quot;)&lt;br /&gt;	{&lt;br /&gt;		vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; svec(10,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;*&lt;/span&gt;&quot;);&lt;br /&gt;		vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;::iterator it = svec.begin();&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt;(it != svec.end())&lt;br /&gt;		{&lt;br /&gt;			cout &amp;lt;&amp;lt; *it++ &amp;lt;&amp;lt; endl;&lt;br /&gt;		}&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// initialize slist with copy of each element of svec&lt;/span&gt;&lt;br /&gt;		list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; slist(svec.begin(), svec.end());&lt;br /&gt;&lt;br /&gt;		cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;The list size is:&lt;/span&gt;&quot; &amp;lt;&amp;lt; slist.size() &amp;lt;&amp;lt; endl;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// find midpoint in the vector&lt;/span&gt;&lt;br /&gt;		vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;::iterator mid = svec.begin() + svec.size()/2;&lt;br /&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// initialize front with first half of svec: The elements up to but not including *mid&lt;/span&gt;&lt;br /&gt;		deque&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; front(svec.begin(), mid);&lt;br /&gt;		cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;The deque is copied from begin to mid and its size is:&lt;/span&gt;&quot; &amp;lt;&amp;lt; front.size() &amp;lt;&amp;lt; endl;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// initialize back with second half of svec: The elements *mid through end of svec&lt;/span&gt;&lt;br /&gt;		deque&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; back(mid, svec.end());&lt;br /&gt;		cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;The deque is copied from mid to end and its size is:&lt;/span&gt;&quot; &amp;lt;&amp;lt; back.size() &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;char&lt;/span&gt; *words[] = {&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;stately&lt;/span&gt;&quot;, &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;plump&lt;/span&gt;&quot;, &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;buck&lt;/span&gt;&quot;, &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;mulligan&lt;/span&gt;&quot;,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;a&lt;/span&gt;&quot;,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;ab&lt;/span&gt;&quot;};&lt;br /&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// calculate how many elements in words&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// to get the size of the string array&lt;/span&gt;&lt;br /&gt;		size_t words_size = &lt;span style=&quot;color: #0000ff&quot;&gt;sizeof&lt;/span&gt;(words)/&lt;span style=&quot;color: #0000ff&quot;&gt;sizeof&lt;/span&gt;(&lt;span style=&quot;color: #0000ff&quot;&gt;char&lt;/span&gt; *);&lt;br /&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// use entire array to initialize words2&lt;/span&gt;&lt;br /&gt;		list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; words2(words, words + words_size);&lt;br /&gt;		list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;::iterator itL = words2.begin();&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt;( itL != words2.end())&lt;br /&gt;		{&lt;br /&gt;			cout &amp;lt;&amp;lt; *itL++ &amp;lt;&amp;lt; endl;&lt;br /&gt;		}&lt;br /&gt;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Allocating and Initializing a Specified Number of Elements&lt;/span&gt;&quot;)&lt;br /&gt;	{&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt;::size_type list_size = 64;&lt;br /&gt;		list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; slist(list_size, &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;eh?&lt;/span&gt;&quot;); &lt;span style=&quot;color: #008000&quot;&gt;// 64 strings, each is eh?&lt;/span&gt;&lt;br /&gt;		list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;::iterator it_slist = slist.begin();&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt;(it_slist != slist.end())&lt;br /&gt;			cout &amp;lt;&amp;lt; *it_slist ++ ;&lt;br /&gt;		cout &amp;lt;&amp;lt; endl;&lt;br /&gt;		&lt;br /&gt;		list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; ilist(list_size); &lt;span style=&quot;color: #008000&quot;&gt;// 64 elements, each initialized to 0&lt;/span&gt;&lt;br /&gt;		list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt;::iterator it_ilist = ilist.begin();&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt;( it_ilist != ilist.end())&lt;br /&gt;			cout &amp;lt;&amp;lt; *it_ilist++ ;&lt;br /&gt;		cout &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// svec has as many elements as the return value from get_word_count&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;extern&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;unsigned&lt;/span&gt; get_word_count(&lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; &amp;amp;file_name);&lt;br /&gt;		vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; svec(get_word_count(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Chimera&lt;/span&gt;&quot;));&lt;br /&gt;		vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;::iterator it_svec = svec.begin();&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt;( it_svec != svec.end())&lt;br /&gt;			cout &amp;lt;&amp;lt; *it_svec ++ ;&lt;br /&gt;		cout &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;br /&gt;	}&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Container Operations May Impose Additional Requirements&lt;/span&gt;&quot;)&lt;br /&gt;	{&lt;br /&gt;		vector&amp;lt;Foo&amp;gt; empty;     &lt;span style=&quot;color: #008000&quot;&gt;// ok: no need for element default constructor&lt;/span&gt;&lt;br /&gt;		vector&amp;lt;Foo&amp;gt; bad(10);   &lt;span style=&quot;color: #008000&quot;&gt;// it&#39;s ok to call now with the default constructor // error: no default constructor for Foo&lt;/span&gt;&lt;br /&gt;		vector&amp;lt;Foo&amp;gt; ok(10, 1); &lt;span style=&quot;color: #008000&quot;&gt;// ok: each element initialized to 1&lt;/span&gt;&lt;br /&gt;		vector&amp;lt;Foo&amp;gt;::iterator it = ok.begin();&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt;( it != ok.end())&lt;br /&gt;		{&lt;br /&gt;			it-&amp;gt;PrintOut();&lt;br /&gt;			++it;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Containers of Containers&lt;/span&gt;&quot;)&lt;br /&gt;	{&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// note spacing: use &quot;&amp;gt;&amp;gt;&quot; not &quot;&amp;gt;&amp;gt;&quot; when specifying a container element type&lt;/span&gt;&lt;br /&gt;		vector&amp;lt; vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; &amp;gt; lines0; &lt;span style=&quot;color: #008000&quot;&gt;// vector of vectors&lt;/span&gt;&lt;br /&gt;		vector&amp;lt; vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; &amp;gt; lines1; &lt;span style=&quot;color: #008000&quot;&gt;// ok: space required between close &amp;gt;&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// actually not comple error in VSTS 2010&lt;/span&gt;&lt;br /&gt;		vector&amp;lt;vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;&amp;gt; lines2; &lt;span style=&quot;color: #008000&quot;&gt;// error: &amp;gt;&amp;gt; treated as shift operator&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; i;&lt;br /&gt;	cin &amp;gt;&amp;gt; i;&lt;br /&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;unsigned&lt;/span&gt; get_word_count(&lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; &amp;amp;file_name)&lt;br /&gt;{&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; file_name.size();&lt;br /&gt;}&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;inline&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; Foo::PrintOut()&lt;br /&gt;{&lt;br /&gt;	cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;The fool&#39;s size is:&lt;/span&gt;&quot; &amp;lt;&amp;lt; &lt;span style=&quot;color: #0000ff&quot;&gt;this&lt;/span&gt;-&amp;gt;size &amp;lt;&amp;lt; endl;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;h4&gt;Section 9.2 Iterators and Iterator Ranges&lt;/h4&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 9.3. Common Iterator Operations&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;150&quot;&gt;&lt;br /&gt;&lt;col width=&quot;375&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;*iter&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Return a reference to the element referred to by the iterator &lt;tt&gt;iter&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;iter-&amp;gt;mem&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Dereference &lt;tt&gt;iter&lt;/tt&gt; and fetch the member named &lt;tt&gt;mem&lt;/tt&gt; from the underlying element. Equivalent to &lt;tt&gt;(*iter).mem&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;++iter iter++&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Increment &lt;tt&gt;iter&lt;/tt&gt; to refer to the next element in the container.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;--iter iter--&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Decrement &lt;tt&gt;iter&lt;/tt&gt; to refer to the previous element in the container.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;iter1 == iter2 &lt;br&gt;iter1 != iter2&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Compare two iterators for equality (inequality). Two iterators are equal if they refer to the same element of the same container or if they are the off-the-end iterator (&lt;/strong&gt;&lt;a class=&quot;docLink&quot; href=&quot;ch03lev1sec4.html#ch03lev1sec4&quot;&gt;&lt;strong&gt;Section 3.4&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;, p. &lt;/strong&gt;&lt;a class=&quot;docLink&quot; href=&quot;ch03lev1sec4.html#ch03lev1sec4&quot;&gt;&lt;strong&gt;97&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;) for the same container.&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;li&gt;Iterators on vector and deque Support Additional Operations &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;There are two important sets of operations that only &lt;tt&gt;vector&lt;/tt&gt; and &lt;tt&gt;deque&lt;/tt&gt; support: iterator arithmetic (&lt;a href=&quot;ch03lev1sec4.html#ch03lev2sec14&quot;&gt;Section 3.4.1&lt;/a&gt;, p. &lt;a href=&quot;ch03lev1sec4.html#ch03lev2sec14&quot;&gt;100&lt;/a&gt;) and the use of the relational operators (in addition to &lt;tt&gt;==&lt;/tt&gt; and &lt;tt&gt;!=&lt;/tt&gt;) to compare two iterators &lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 9.4. Operations Supported by &lt;tt&gt;vector&lt;/tt&gt; and &lt;tt&gt;deque&lt;/tt&gt; Iterators&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;150&quot;&gt;&lt;br /&gt;&lt;col width=&quot;350&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;iter + n &lt;br&gt;iter - n&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Adding (subtracting) an integral value &lt;tt&gt;n&lt;/tt&gt; to (from) an iterator yields an iterator that many elements forward (backward) within the container. &lt;strong&gt;The resulting iterator must refer to an element in the container or one past the end of the container.&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;iter1 += iter2 &lt;br&gt;iter1 -= iter2&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Compound-assignment versions of iterator addition and subtraction. Assigns the value of adding or subtracting &lt;tt&gt;iter1&lt;/tt&gt; and &lt;tt&gt;iter2&lt;/tt&gt; into &lt;tt&gt;iter1&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;iter1 - iter2&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Subtracting two iterators yields the number that when added to the right-hand iterator yields the left-hand iterator.&lt;/strong&gt; The iterators must refer to elements in the same container or one past the end of the container.&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;Supported only for&lt;/span&gt; &lt;tt&gt;vector&lt;/tt&gt; &lt;span class=&quot;docEmphStrong&quot;&gt;and&lt;/span&gt; &lt;tt&gt;deque&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;&amp;gt;, &amp;gt;=, &amp;lt;, &amp;lt;=&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Relational operators on iterators. One iterator is less than another if it refers to an element whose position in the container is ahead of the one referred to by the other iterator.&lt;/strong&gt; The iterators must refer to elements in the same container or one past the end of the container.&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;Supported only for&lt;/span&gt; &lt;tt&gt;vector&lt;/tt&gt; &lt;span class=&quot;docEmphStrong&quot;&gt;and&lt;/span&gt; &lt;tt&gt;deque&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;li&gt;The reason that only &lt;tt&gt;vector&lt;/tt&gt; and &lt;tt&gt;deque&lt;/tt&gt; support the relational operators is that only &lt;tt&gt;vector&lt;/tt&gt; and &lt;tt&gt;deque&lt;/tt&gt; offer fast, random access to their elements &lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;9.2.1. Iterator Ranges &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;The concept of an iterator range is fundamental to the standard library.&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;An iterator range is denoted by a pair of iterators that refer to two elements, or to one past the last element, in the same container. These two iterators, often referred to as first and last, or beg and end, mark a range of elements from the container. &lt;br /&gt;&lt;li&gt;&lt;strong&gt;The second iterator never refers to the last element of the range. Instead, it refers to a point one past the last element.&lt;/strong&gt; &lt;strong&gt;The elements in the range include the element referred to by &lt;tt&gt;first&lt;/tt&gt; and every element from &lt;tt&gt;first&lt;/tt&gt; tHRough the element just before &lt;tt&gt;last&lt;/tt&gt;. If the iterators are equal, then the range is empty&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;This element range is called a &lt;b&gt;&lt;a name=&quot;ch09term9&quot;&gt;&lt;/a&gt;&lt;a href=&quot;ch09lev1sec9.html#gloss09_09&quot;&gt;left-inclusive interval&lt;/a&gt;&lt;/b&gt; &lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table border=&quot;1&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot; width=&quot;90%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td&gt;&lt;br /&gt;&lt;h2 class=&quot;docSidebarTitle&quot;&gt;Requirements on Iterators Forming an Iterator Range&lt;/h2&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;Two iterators, &lt;tt&gt;first&lt;/tt&gt; and &lt;tt&gt;last&lt;/tt&gt;&lt;/span&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;, form an iterator range, if&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p class=&quot;docList&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;They refer to elements of, or one-past-the-end of, the same container.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p class=&quot;docList&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;If the iterators are not equal, then it must be possible to reach &lt;tt&gt;last&lt;/tt&gt; by repeatedly incrementing &lt;tt&gt;first&lt;/tt&gt;&lt;/span&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;. In other words, &lt;tt&gt;last&lt;/tt&gt; must not precede &lt;tt&gt;first&lt;/tt&gt; in the container.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;The compiler cannot itself enforce these requirements. It does not know to which container an iterator is bound, nor does it know how many elements are in a container. Failing to meet these requirements results in undefined run-time behavior&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;Programming Implications of Using Left-Inclusive Ranges &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;p&gt;The library uses left-inclusive ranges because such ranges have two convenient properties. Assuming &lt;tt&gt;first&lt;/tt&gt; and &lt;tt&gt;last&lt;/tt&gt; denote a valid iterator range, then &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;When &lt;tt&gt;first&lt;/tt&gt; equals &lt;tt&gt;last&lt;/tt&gt;, the range is empty.&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;When &lt;tt&gt;first&lt;/tt&gt; is not equal to &lt;tt&gt;last&lt;/tt&gt;, there is at least one element in the range, and &lt;tt&gt;first&lt;/tt&gt; refers to the first element in that range. Moreover, we can advance &lt;tt&gt;first&lt;/tt&gt; by incrementing it some number of times until &lt;tt&gt;first == last&lt;/tt&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;9.2.2. Some Container Operations Invalidate Iterators &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;In the sections that follow, we&#39;ll see that some container operations change the internal state of a container or cause the elements in the container to be moved. Such operations &lt;b&gt;&lt;a name=&quot;ch09term6&quot;&gt;&lt;/a&gt;&lt;a href=&quot;ch09lev1sec9.html#gloss09_06&quot;&gt;invalidate&lt;/a&gt;&lt;/b&gt; all iterators that refer to the elements that are moved and may invalidate other iterators as well. Using an invalidated iterator is undefined, and likely to lead to the same kinds of problems as using a dangling pointer &lt;br /&gt;&lt;li&gt;&lt;strong&gt;When writing programs that use iterators, we must be aware of which operations can invalidate the iterators. It is a serious run-time error to use an iterator that has been invalidated&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;There is no way to examine an iterator to determine whether it has been invalidated. There is no test we can perform to detect that it has gone bad. Any use of an invalidated iterator is likely to yield a run-time error, but there is no guarantee that the program will crash or otherwise make it easy to detect the problem &lt;br /&gt;&lt;li&gt;When using iterators, it is usually possible to write the program so that the range of code over which an iterator must stay valid is relatively short. It is important to examine each statement in this range to determine whether elements are added or removed and adjust iterator values accordingly&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;h4&gt;Section 9.3 Sequence Container Operations&lt;/h4&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;p&gt;Each sequential container defines a set of useful typedefs and supports operations that let us &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;Add elements to the container&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;Delete elements from the container&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;Determine the size of the container&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;Fetch the first and last elements from the container, if any&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;9.3.1. Container Typedefs &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 9.5. Container-Defined Typedefs&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;200&quot;&gt;&lt;br /&gt;&lt;col width=&quot;300&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;size_type&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Unsigned integral type large enough to hold size of largest possible container of this container type&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;iterator&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Type of the iterator for this container type&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;const_iterator&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Type of the iterator that can read but not write the elements&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;reverse_iterator&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Iterator that addresses elements in reverse order&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;const_reverse_iterator&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Reverse iterator that can read but not write the elements&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;difference_type&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Signed integral type large enough to hold the difference, which might be negative, between two iterators&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;value_type&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Element type&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;reference&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Element&#39;s lvalue type; synonym for &lt;tt&gt;value_type&amp;amp;&lt;/tt&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;const_reference&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Element&#39;s const lvalue type; same as &lt;tt&gt;const value_type&amp;amp;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;9.3.2. begin and end Members &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;The &lt;a name=&quot;ch09term2&quot;&gt;&lt;/a&gt;&lt;a href=&quot;ch09lev1sec9.html#gloss09_02&quot;&gt;&lt;tt&gt;begin&lt;/tt&gt;&lt;/a&gt; and &lt;tt&gt;end&lt;/tt&gt; operations yield iterators that refer to the first and one past the last element in the container. &lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 9.6. Container &lt;tt&gt;begin&lt;/tt&gt; and &lt;tt&gt;end&lt;/tt&gt; Operations&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;200&quot;&gt;&lt;br /&gt;&lt;col width=&quot;300&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.begin()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Yields an iterator referring to the first element in &lt;tt&gt;c&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.end()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Yields an iterator referring to the one past the last element in &lt;tt&gt;c&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.rbegin()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Yields a reverse iterator referring to the last element in &lt;tt&gt;c&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.rend()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Yields a reverse iterator referring one past (i.e., before) the first element in &lt;tt&gt;c&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;9.3.3. Adding Elements to a Sequential Container &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Every sequential container supports &lt;tt&gt;push_back&lt;/tt&gt;, which appends an element to the back of the container &lt;br /&gt;&lt;li&gt;The call to &lt;tt&gt;push_back&lt;/tt&gt; creates a new element at the end of &lt;a name=&quot;ch09term3&quot;&gt;&lt;/a&gt;&lt;a href=&quot;ch09lev1sec9.html#gloss09_03&quot;&gt;&lt;tt&gt;container&lt;/tt&gt;&lt;/a&gt;, increasing the &lt;tt&gt;size&lt;/tt&gt; of &lt;tt&gt;container&lt;/tt&gt; by one. &lt;strong&gt;The value of that element is a copy of &lt;tt&gt;text_word&lt;/tt&gt;.&lt;/strong&gt; The type of &lt;tt&gt;container&lt;/tt&gt; can be any of &lt;tt&gt;list, vector&lt;/tt&gt;, or &lt;tt&gt;deque&lt;/tt&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;In addition to &lt;tt&gt;push_back&lt;/tt&gt;, the &lt;tt&gt;list&lt;/tt&gt; and &lt;tt&gt;deque&lt;/tt&gt; containers support an analogous operation named &lt;tt&gt;push_front&lt;/tt&gt;. This operation inserts a new element at the front of the container&lt;/strong&gt; &lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table border=&quot;1&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot; width=&quot;90%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td&gt;&lt;br /&gt;&lt;h2 class=&quot;docSidebarTitle&quot;&gt;Key Concept: Container Elements Are Copies&lt;/h2&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;&lt;strong&gt;When we add an element to a container, we do so by copying the element value into the container. Similarly, when we initialize a container by providing a range of elements, the new container contains copies of the original range of elements. There is no relationship between the element in the container and the value from which it was copied. Subsequent changes to the element in the container have no effect on the value that was copied, and vice versa.&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 9.7. Operations that Add Elements to a Sequential Container&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;150&quot;&gt;&lt;br /&gt;&lt;col width=&quot;350&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.push_back(t)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Adds element with value &lt;tt&gt;t&lt;/tt&gt; to the end of &lt;tt&gt;c&lt;/tt&gt;. Returns &lt;tt&gt;void&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.push_front(t)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Adds element with value &lt;tt&gt;t&lt;/tt&gt; to front of &lt;tt&gt;c&lt;/tt&gt;. Returns &lt;tt&gt;void&lt;/tt&gt;.&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;Valid only for &lt;tt&gt;list&lt;/tt&gt; or &lt;tt&gt;deque&lt;/tt&gt;&lt;/span&gt;.&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.insert(p,t)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Inserts element with value &lt;tt&gt;t&lt;/tt&gt; before the element referred to by iterator &lt;tt&gt;p&lt;/tt&gt;. Returns an iterator referring to the element that was added&lt;/strong&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.insert(p,n,t)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Inserts &lt;tt&gt;n&lt;/tt&gt; elements with value &lt;tt&gt;t&lt;/tt&gt; before the element referred to by iterator &lt;tt&gt;p&lt;/tt&gt;. Returns &lt;tt&gt;void&lt;/tt&gt;&lt;/strong&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.insert(p,b,e)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Inserts elements in the range denoted by iterators &lt;tt&gt;b&lt;/tt&gt; and &lt;tt&gt;e&lt;/tt&gt; before the element referred to by iterator &lt;tt&gt;p&lt;/tt&gt;. Returns &lt;tt&gt;void&lt;/tt&gt;.&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;li&gt;Adding Elements at a Specified Point in the Container &lt;br /&gt;&lt;li&gt;Inserting a Range of Elements &lt;br /&gt;&lt;li&gt;Inserting Elements Can Invalidate Iterators &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;As we&#39;ll see in &lt;/strong&gt;&lt;a href=&quot;ch09lev1sec4.html#ch09lev1sec4&quot;&gt;&lt;strong&gt;Section 9.4&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt; (p. &lt;/strong&gt;&lt;a href=&quot;ch09lev1sec4.html#ch09lev1sec4&quot;&gt;&lt;strong&gt;330&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;), adding elements to a &lt;tt&gt;vector&lt;/tt&gt; can cause the entire container to be relocated. If the container is relocated, then all iterators into the container are invalidated. Even if the &lt;tt&gt;vector&lt;/tt&gt; does not have to be relocated, any iterator to an element after the one inserted is invalidated&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;Iterators may be invalidated after doing any &lt;tt&gt;insert&lt;/tt&gt; or &lt;tt&gt;push&lt;/tt&gt; operation on a &lt;tt&gt;vector&lt;/tt&gt; or &lt;tt&gt;deque&lt;/tt&gt;. When writing loops that &lt;tt&gt;insert&lt;/tt&gt; elements into a &lt;tt&gt;vector&lt;/tt&gt; or a &lt;tt&gt;deque&lt;/tt&gt;, the program must ensure that the iterator is refreshed on each trip through the loop&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Avoid Storing the Iterator Returned from end &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;When we add elements to a &lt;tt&gt;vector&lt;/tt&gt; or &lt;tt&gt;deque&lt;/tt&gt;, some or all of the iterators may be invalidated. It is safest to assume that all iterators are invalid. This advice is particularly true for the iterator returned by &lt;tt&gt;end&lt;/tt&gt;. That iterator is always invalidated by any insertion anywhere in the container&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;Don&#39;t cache the iterator returned from &lt;tt&gt;end&lt;/tt&gt;. Inserting or deleting elements in a &lt;tt&gt;deque&lt;/tt&gt; or &lt;tt&gt;vector&lt;/tt&gt; will invalidate the cached iterator&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;9.3.4. Relational Operators &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;The containers must be the same kind of container and must hold elements of the same type &lt;br /&gt;&lt;li&gt;Comparing two containers is based on a pairwise comparison of the elements of the containers. The comparison uses the same relational operator as defined by the element type: Comparing two containers using &lt;tt&gt;!=&lt;/tt&gt; uses the &lt;tt&gt;!=&lt;/tt&gt; operator for the element type &lt;br /&gt;&lt;p&gt;&lt;strong&gt;These operators work similarly to the &lt;tt&gt;string&lt;/tt&gt; relationals (&lt;/strong&gt;&lt;a href=&quot;ch03lev1sec2.html#ch03lev2sec4&quot;&gt;&lt;strong&gt;Section 3.2.3&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;, p. &lt;/strong&gt;&lt;a href=&quot;ch03lev1sec2.html#ch03lev2sec4&quot;&gt;&lt;strong&gt;85&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;):&lt;/strong&gt; &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;If both containers are the same size and all the elements are equal, then the two containers are equal; otherwise, they are unequal.&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;If the containers have different sizes but every element of the shorter one is equal to the corresponding element of the longer one, then the shorter one is considered to be less than the other.&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;If neither container is an initial subsequence of the other, then the comparison depends on comparing the first unequal elements&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;Relational Operators Use Their Element&#39;s Relational Operator &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;We can compare two containers only if the same relational operator defined for the element types &lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;9.3.5. Container Size Operations &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 9.8. Sequential Container Size Operations&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;150&quot;&gt;&lt;br /&gt;&lt;col width=&quot;350&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.size()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Returns the number of elements in &lt;tt&gt;c&lt;/tt&gt;. Return type is &lt;tt&gt;c::size_type&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.max_size()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Returns maximum number of elements &lt;tt&gt;c&lt;/tt&gt; can contain. Return type is &lt;tt&gt;c::size_type&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.empty()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Returns a &lt;tt&gt;bool&lt;/tt&gt; that indicates whether &lt;tt&gt;size&lt;/tt&gt; is 0 or not.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.resize(n)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Resize &lt;tt&gt;c&lt;/tt&gt; so that it has &lt;tt&gt;n&lt;/tt&gt; elements. If &lt;tt&gt;N &amp;lt; c.size()&lt;/tt&gt;, the excess elements are discarded. If new elements must be added, they are value initialized&lt;/strong&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.resize(n,t)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Resize &lt;tt&gt;c&lt;/tt&gt; to have &lt;tt&gt;n&lt;/tt&gt; elements. Any elements added have value &lt;tt&gt;t&lt;/tt&gt;&lt;/strong&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;&lt;tt&gt;resize&lt;/tt&gt; can invalidate iterators. A &lt;tt&gt;resize&lt;/tt&gt; operation on a &lt;tt&gt;vector&lt;/tt&gt; or &lt;tt&gt;deque&lt;/tt&gt; potentially invalidates all iterators&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;For any container type, if &lt;tt&gt;resize&lt;/tt&gt; shrinks the container, then any iterator to an element that is deleted is invalidated&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;9.3.6. Accessing Elements &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;If a container is not empty, then the &lt;tt&gt;front&lt;/tt&gt; and &lt;tt&gt;back&lt;/tt&gt; members return references bound to the first or last elements in the container &lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 9.9. Operations to Access Elements in a Sequential Container&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;150&quot;&gt;&lt;br /&gt;&lt;col width=&quot;350&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.back()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Returns a reference to the last element in &lt;tt&gt;c&lt;/tt&gt;. Undefined if &lt;tt&gt;c&lt;/tt&gt; is empty&lt;/strong&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.front()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Returns a reference to the first element in &lt;tt&gt;c&lt;/tt&gt;. Undefined if &lt;tt&gt;c&lt;/tt&gt; is empty&lt;/strong&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c[n]&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Returns a reference to the element indexed by &lt;tt&gt;n&lt;/tt&gt;&lt;/strong&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Undefined if &lt;tt&gt;n &amp;lt;0&lt;/tt&gt; or &lt;tt&gt;n &amp;gt;= c.size()&lt;/tt&gt;.&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;Valid only for &lt;tt&gt;vector&lt;/tt&gt; and &lt;tt&gt;deque&lt;/tt&gt;&lt;/span&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.at(n)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Returns a reference to the element indexed by &lt;tt&gt;n&lt;/tt&gt;. If index is out of range, throws &lt;tt&gt;out_of_range&lt;/tt&gt; exception.&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;Valid only for &lt;tt&gt;vector&lt;/tt&gt; and &lt;tt&gt;deque&lt;/tt&gt;&lt;/span&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;li&gt;This program uses two different approaches to fetch a reference to the first and last elements in &lt;tt&gt;ilist&lt;/tt&gt;. The direct approach is to call &lt;tt&gt;front&lt;/tt&gt; or &lt;tt&gt;back&lt;/tt&gt;. Indirectly, we can obtain a reference to the same element by dereferencing the iterator returned by &lt;tt&gt;begin&lt;/tt&gt; or the element one before the iterator returned by &lt;tt&gt;end&lt;/tt&gt;. Two things are noteworthy in this program: The &lt;tt&gt;end&lt;/tt&gt; iterator refers &quot;one past the end&quot; of the container so to fetch the last element we must first decrement that iterator. The other important point is that before calling &lt;tt&gt;front&lt;/tt&gt; or &lt;tt&gt;back&lt;/tt&gt; or dereferencing the iterators from &lt;tt&gt;begin&lt;/tt&gt; or &lt;tt&gt;end&lt;/tt&gt; we check that &lt;tt&gt;ilist&lt;/tt&gt; isn&#39;t empty. If the list were empty all of the operations in the &lt;tt&gt;if&lt;/tt&gt; would be undefined&lt;pre&gt;            &lt;span style=&quot;color: #008000&quot;&gt;// check that there are elements before dereferencing an iterator&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// or calling front or back&lt;/span&gt;&lt;br /&gt;		list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; ilist;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt; (!ilist.empty()) {&lt;br /&gt;			&lt;span style=&quot;color: #008000&quot;&gt;// val and val2 refer to the same element&lt;/span&gt;&lt;br /&gt;			list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt;::reference val = *ilist.begin();&lt;br /&gt;			list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt;::reference val2 = ilist.front();&lt;br /&gt;&lt;br /&gt;			&lt;span style=&quot;color: #008000&quot;&gt;// last and last2 refer to the same element&lt;/span&gt;&lt;br /&gt;			list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt;::reference last = *--ilist.end();&lt;br /&gt;			list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt;::reference last2 = ilist.back(); &lt;br /&gt;		}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;li&gt;When we introduced subscripting in &lt;a href=&quot;ch03lev1sec3.html#ch03lev2sec7&quot;&gt;Section 3.3.2&lt;/a&gt; (p. &lt;a href=&quot;ch03lev1sec3.html#ch03lev2sec7&quot;&gt;94&lt;/a&gt;), we noted that the programmer must ensure that an element exists at the indicated subscript location. The subscript operator itself does not check. The same caution applies to using the &lt;tt&gt;front&lt;/tt&gt; or &lt;tt&gt;back&lt;/tt&gt; operations. If the container is empty, these operations yield an undefined result &lt;br /&gt;&lt;li&gt;&lt;strong&gt;Using a subscript that is out-of-range or calling &lt;tt&gt;front&lt;/tt&gt; or &lt;tt&gt;back&lt;/tt&gt; on an empty container are serious programming errors&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;An alternative to subscripting is to use the &lt;tt&gt;at&lt;/tt&gt; member. This function acts like the subscript operation but if the index is invalid, &lt;tt&gt;at&lt;/tt&gt; throws an &lt;tt&gt;out_of_range&lt;/tt&gt; exception&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;9.3.7. Erasing Elements &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;specific &lt;tt&gt;push_front&lt;/tt&gt; and &lt;tt&gt;push_back&lt;/tt&gt; operations to add elements only at the front or back. Similarly, there is a general &lt;tt&gt;erase&lt;/tt&gt; and specific &lt;tt&gt;pop_front&lt;/tt&gt; and &lt;tt&gt;pop_back&lt;/tt&gt; operations to remove elements &lt;br /&gt;&lt;li&gt;Removing the First or Last Element &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;The &lt;tt&gt;pop_front&lt;/tt&gt; and &lt;tt&gt;pop_back&lt;/tt&gt; functions remove the first and last elements in the container. There is no &lt;tt&gt;pop_front&lt;/tt&gt; operation for &lt;tt&gt;vector&lt;/tt&gt;s.These operations remove the indicated element and return &lt;tt&gt;void&lt;/tt&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;The &lt;tt&gt;pop_front&lt;/tt&gt; and &lt;tt&gt;pop_back&lt;/tt&gt; return &lt;tt&gt;void&lt;/tt&gt;; they do not return the value of the element popped. To examine that value, it is necessary to call &lt;tt&gt;front&lt;/tt&gt; or &lt;tt&gt;back&lt;/tt&gt; prior to popping the element&lt;/strong&gt; &lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 9.10. Operations to Remove Elements from a Sequential Container&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;150&quot;&gt;&lt;br /&gt;&lt;col width=&quot;350&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.erase(p)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Removes element referred to by the iterator &lt;tt&gt;p&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Returns an iterator referring to the element after the one deleted, or an off-the-end iterator if &lt;tt&gt;p&lt;/tt&gt; referred to the last element. Undefined if &lt;tt&gt;p&lt;/tt&gt; is an off-the-end iterator.&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.erase(b,e)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Removes the range of elements denoted by the iterators &lt;tt&gt;b&lt;/tt&gt; and &lt;tt&gt;e&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Returns an iterator referring after the last one in the range that was deleted, or an off-the-end iterator if &lt;tt&gt;e&lt;/tt&gt; is itself an off-the-end iterator.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.clear()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Removes all the elements in &lt;tt&gt;c&lt;/tt&gt;. Returns &lt;tt&gt;void&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.pop_back()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Removes the last element in &lt;tt&gt;c&lt;/tt&gt;. Returns &lt;tt&gt;void&lt;/tt&gt;. Undefined if &lt;tt&gt;c&lt;/tt&gt; is empty.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.pop_front()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Removes the first element in &lt;tt&gt;c&lt;/tt&gt;. Returns &lt;tt&gt;void&lt;/tt&gt;. Undefined if &lt;tt&gt;c&lt;/tt&gt; is empty.&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;Valid only for &lt;tt&gt;list&lt;/tt&gt; or &lt;tt&gt;deque&lt;/tt&gt;&lt;/span&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;a&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Removing an Element From within the Container &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;The more general way to remove an element, or range of elements, is through &lt;tt&gt;erase&lt;/tt&gt;. &lt;br /&gt;&lt;li&gt;&lt;strong&gt;As usual, the &lt;tt&gt;erase&lt;/tt&gt; operations don&#39;t check their argument(s). It is up to the programmer to ensure that the iterator or iterator range is valid&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;The easiest way to find a given element is to use the library &lt;tt&gt;find&lt;/tt&gt; algorithm. We&#39;ll see more about &lt;tt&gt;find&lt;/tt&gt; in &lt;/strong&gt;&lt;a href=&quot;ch11lev1sec1.html#ch11lev1sec1&quot;&gt;&lt;strong&gt;Section 11.1&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt; (p. &lt;/strong&gt;&lt;a href=&quot;ch11lev1sec1.html#ch11lev1sec1&quot;&gt;&lt;strong&gt;392&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;). To use &lt;tt&gt;find&lt;/tt&gt; or any other generic algorithm, we must include the &lt;tt&gt;algorithm&lt;/tt&gt; header. The &lt;tt&gt;find&lt;/tt&gt; function takes a pair of iterators that denote a range in which to look, and a value to look for within that range. &lt;tt&gt;find&lt;/tt&gt; returns an iterator referring to the first element with that value or the off-the-end iterator&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;Note that we check that the iterator is not the &lt;tt&gt;end&lt;/tt&gt; iterator before erasing the element. When we ask &lt;tt&gt;erase&lt;/tt&gt; to erase a single element, the element must existthe behavior of &lt;tt&gt;erase&lt;/tt&gt; is undefined if we ask it to &lt;tt&gt;erase&lt;/tt&gt; an off-the-end iterator&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Removing All the Elements in a Container &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;To delete all the elements in a container, we could either call &lt;tt&gt;clear&lt;/tt&gt; or pass the iterators from &lt;tt&gt;begin&lt;/tt&gt; and &lt;tt&gt;end&lt;/tt&gt; to &lt;tt&gt;erase&lt;/tt&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;The &lt;tt&gt;erase, pop_front&lt;/tt&gt;, and &lt;tt&gt;pop_back&lt;/tt&gt; functions invalidate any iterators that refer to the removed elements. For &lt;tt&gt;vector&lt;/tt&gt;s, iterators to elements after the erasure point are also invalidated. For &lt;tt&gt;deque&lt;/tt&gt;, if the &lt;tt&gt;erase&lt;/tt&gt; does not include either the first or last element, all iterators into the &lt;tt&gt;deque&lt;/tt&gt; are invalidated&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;9.3.8. Assignment and swap &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;The assignment-related operators act on the entire container. Except for &lt;tt&gt;swap&lt;/tt&gt;, they can be expressed in terms of &lt;tt&gt;erase&lt;/tt&gt; and &lt;tt&gt;insert&lt;/tt&gt; operations. The assignment operator erases the entire range of elements in the left-hand container and then inserts the elements of the right-hand container object into the left-hand container&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;Assignment and the &lt;tt&gt;assign&lt;/tt&gt; operations invalidate all iterators into the left-hand container. &lt;tt&gt;swap&lt;/tt&gt; does not invalidate iterators. After &lt;tt&gt;swap&lt;/tt&gt;, iterators continue to refer to the same elements, although those elements are now in a different container&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;Using assign &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;The &lt;tt&gt;assign&lt;/tt&gt; operation deletes all the elements in the container and then inserts new elements as specified by the arguments. Like the constructor that copies elements from a container, the assignment operator (&lt;tt&gt;=&lt;/tt&gt;) can be used to assign one &lt;a name=&quot;idd1e69304&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e69310&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e69316&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e69323&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e69330&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e69337&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e69344&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e69351&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e69357&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e69364&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e69368&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e69374&quot;&gt;&lt;/a&gt;container to another only if the container and element type are the same. If we want to assign elements of a different but compatible element type and/or from a different container type, then we must use the &lt;tt&gt;assign&lt;/tt&gt; operation. &lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 9.11. Sequential Container Assignment Operations&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;150&quot;&gt;&lt;br /&gt;&lt;col width=&quot;350&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c1 = c2&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Deletes elements in &lt;tt&gt;c1&lt;/tt&gt; and copies elements from &lt;tt&gt;c2&lt;/tt&gt; into &lt;tt&gt;c1&lt;/tt&gt;. &lt;tt&gt;c1&lt;/tt&gt; and &lt;tt&gt;c2&lt;/tt&gt; must be the same type.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c1.swap(c2)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Swaps contents: After the call &lt;tt&gt;c1&lt;/tt&gt; has elements that were in &lt;tt&gt;c2&lt;/tt&gt;, and &lt;tt&gt;c2&lt;/tt&gt; has elements that were in &lt;tt&gt;c1&lt;/tt&gt;. &lt;tt&gt;c1&lt;/tt&gt; and &lt;tt&gt;c2&lt;/tt&gt; must be the same type. Execution time usually &lt;span class=&quot;docEmphasis&quot;&gt;much&lt;/span&gt; faster than copying elements from &lt;tt&gt;c2&lt;/tt&gt; to &lt;tt&gt;c1&lt;/tt&gt;&lt;/strong&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.assign(b,e)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Replaces the elements in &lt;tt&gt;c&lt;/tt&gt; by those in the range denoted by iterators &lt;tt&gt;b&lt;/tt&gt; and &lt;tt&gt;e&lt;/tt&gt;. The iterators &lt;tt&gt;b&lt;/tt&gt; and &lt;tt&gt;e&lt;/tt&gt; must not refer to elements in &lt;tt&gt;c&lt;/tt&gt;&lt;/strong&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c.assign(n,t)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Replaces the elements in &lt;tt&gt;c&lt;/tt&gt; by &lt;tt&gt;n&lt;/tt&gt; elements with value &lt;tt&gt;t&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;Because the original elements are deleted, the iterators passed to &lt;tt&gt;assign&lt;/tt&gt; must not refer to elements in the container on which &lt;tt&gt;assign&lt;/tt&gt; is called&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;The &lt;tt&gt;assign&lt;/tt&gt; operator that takes an iterator pair lets us assign elements of one container type to another&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Using swap to Avoid the Cost of Deleting Elements &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;The &lt;tt&gt;swap&lt;/tt&gt; operation swaps the values of its two operands. The types of the containers must match: The operands must be the same kind of container, and they &lt;a name=&quot;idd1e69644&quot;&gt;&lt;/a&gt;must hold values of the same type. After the call to &lt;tt&gt;swap&lt;/tt&gt;, the elements that had been in the right-hand operand are in the left, and vice versa &lt;br /&gt;&lt;li&gt;&lt;strong&gt;The important thing about &lt;tt&gt;swap&lt;/tt&gt; is that it does not delete or insert any elements and is guaranteed to run in constant time. No elements are moved, and so iterators are not invalidated&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;The fact that elements are not moved means that iterators are not invalidated. They refer to the same elements as they did before the swap. However, after the &lt;tt&gt;swap&lt;/tt&gt;, those elements are in a different container. For example, had &lt;tt&gt;iter&lt;/tt&gt; referred to the &lt;tt&gt;string&lt;/tt&gt; at position &lt;tt&gt;svec1[3]&lt;/tt&gt; before the &lt;tt&gt;swap&lt;/tt&gt; it will refer to the element at position &lt;tt&gt;svec2[3]&lt;/tt&gt; after the &lt;tt&gt;swap&lt;/tt&gt;&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;pre&gt;&lt;span style=&quot;color: #008000&quot;&gt;// EXE-9.3.cpp : Defines the entry point for the console application.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #008000&quot;&gt;//&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;#include &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;stdafx.h&lt;/span&gt;&quot;&lt;br /&gt;#include &amp;lt;vector&amp;gt;&lt;br /&gt;#include &amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;&lt;br /&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;#include &amp;lt;list&amp;gt;&lt;br /&gt;#include &amp;lt;deque&amp;gt;&lt;br /&gt;#include &amp;lt;exception&amp;gt;&lt;br /&gt;#include &amp;lt;cassert&amp;gt;&lt;br /&gt;#include &amp;lt;algorithm&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;namespace&lt;/span&gt; std;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; PrintOut(list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; &amp;amp; l);&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; clearstream(istream &amp;amp;&lt;span style=&quot;color: #0000ff&quot;&gt;in&lt;/span&gt;);&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; PrintOut(vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; &amp;amp;svect);&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; PrintOut(list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; &amp;amp; slst);&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; process(&lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt;::reference re);&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; PrintOut(vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; &amp;amp; ivect);&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; main(&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; argc, _TCHAR* argv[])&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;9.3.1. Container Typedefs&lt;/span&gt;&quot;)&lt;br /&gt;	{&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// iter is the iterator type defined by list&amp;lt;string&amp;gt;&lt;/span&gt;&lt;br /&gt;		list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;::iterator iter;&lt;br /&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// cnt is the difference_type type defined by vector&amp;lt;int&amp;gt;&lt;/span&gt;&lt;br /&gt;		vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt;::difference_type cnt;&lt;br /&gt;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;9.3.3. Adding Elements to a Sequential Container&lt;/span&gt;&quot;)&lt;br /&gt;	{&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// read from standard input putting each word onto the end of container&lt;/span&gt;&lt;br /&gt;		deque&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; container;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; text_word;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;/*while (cin &amp;gt;&amp;gt; text_word)&lt;br /&gt;		container.push_back(text_word);*/&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;		clearstream(cin);&lt;br /&gt;&lt;br /&gt;		list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; ilist;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// add elements at the end of ilist&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;for&lt;/span&gt; (size_t ix = 0; ix != 4; ++ix)&lt;br /&gt;			ilist.push_back(ix);&lt;br /&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// add elements to the start of ilist&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;for&lt;/span&gt; (size_t ix = 0; ix != 4; ++ix)&lt;br /&gt;			ilist.push_front(ix);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Adding Elements at a Specified Point in the Container&lt;/span&gt;&quot;)&lt;br /&gt;		{&lt;br /&gt;			vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; svec;&lt;br /&gt;			list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; slist;&lt;br /&gt;			&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; spouse(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Beth&lt;/span&gt;&quot;);&lt;br /&gt;&lt;br /&gt;			&lt;span style=&quot;color: #008000&quot;&gt;// equivalent to calling slist.push_front (spouse);&lt;/span&gt;&lt;br /&gt;			slist.insert(slist.begin(), spouse);&lt;br /&gt;&lt;br /&gt;			&lt;span style=&quot;color: #008000&quot;&gt;// no push_front on vector but we can insert before begin()&lt;/span&gt;&lt;br /&gt;			&lt;span style=&quot;color: #008000&quot;&gt;// warning: inserting anywhere but at the end of a vector is an expensive operation&lt;/span&gt;&lt;br /&gt;			svec.insert(svec.begin(), spouse);&lt;br /&gt;&lt;br /&gt;			list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; lst;&lt;br /&gt;			list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;::iterator iter = lst.begin();&lt;br /&gt;			&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; word;&lt;br /&gt;			&lt;span style=&quot;color: #008000&quot;&gt;/*while (cin &amp;gt;&amp;gt; text_word)&lt;br /&gt;			container.push_back(text_word);*/&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;			&lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt; (cin &amp;gt;&amp;gt; word)&lt;br /&gt;			{&lt;br /&gt;				iter = lst.insert(iter, word); &lt;span style=&quot;color: #008000&quot;&gt;// same as calling push_front&lt;/span&gt;&lt;br /&gt;			}&lt;br /&gt;			clearstream(cin);&lt;br /&gt;&lt;br /&gt;			PrintOut(lst);&lt;br /&gt;		}&lt;br /&gt;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Inserting a Range of Elements&lt;/span&gt;&quot;)&lt;br /&gt;		{&lt;br /&gt;			vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; svec;&lt;br /&gt;			list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; slist;&lt;br /&gt;&lt;br /&gt;			svec.insert(svec.end(), 10, &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Anna&lt;/span&gt;&quot;);&lt;br /&gt;&lt;br /&gt;			PrintOut(svec);&lt;br /&gt;&lt;br /&gt;			&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; sarray[4] = {&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;quasi&lt;/span&gt;&quot;, &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;simba&lt;/span&gt;&quot;, &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;frollo&lt;/span&gt;&quot;, &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;scar&lt;/span&gt;&quot;};&lt;br /&gt;			&lt;span style=&quot;color: #008000&quot;&gt;// insert all the elements in sarray at end of slist&lt;/span&gt;&lt;br /&gt;			slist.insert(slist.end(), sarray, sarray+4);&lt;br /&gt;			PrintOut(slist);&lt;br /&gt;&lt;br /&gt;			list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;::iterator slist_iter = slist.begin();&lt;br /&gt;			&lt;span style=&quot;color: #008000&quot;&gt;// insert last two elements of sarray before slist_iter&lt;/span&gt;&lt;br /&gt;			slist.insert(slist_iter, sarray+2, sarray+4);&lt;br /&gt;			PrintOut(slist);&lt;br /&gt;		}&lt;br /&gt;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Avoid Storing the Iterator Returned from end&lt;/span&gt;&quot;)&lt;br /&gt;		{&lt;br /&gt;			vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; v(1,0);&lt;br /&gt;&lt;br /&gt;			vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt;::iterator first = v.begin(), last = v.end(); &lt;span style=&quot;color: #008000&quot;&gt;// cache end iterator&lt;/span&gt;&lt;br /&gt;			&lt;span style=&quot;color: #008000&quot;&gt;// diaster: behavior of this loop is undefined&lt;/span&gt;&lt;br /&gt;			&lt;span style=&quot;color: #008000&quot;&gt;//while (first != last) {&lt;/span&gt;&lt;br /&gt;			&lt;span style=&quot;color: #008000&quot;&gt;//	// do some processing&lt;/span&gt;&lt;br /&gt;			&lt;span style=&quot;color: #008000&quot;&gt;//	// insert new value and reassign first, which otherwise would be invalid&lt;/span&gt;&lt;br /&gt;			&lt;span style=&quot;color: #008000&quot;&gt;//	first = v.insert(first, 42);&lt;/span&gt;&lt;br /&gt;			&lt;span style=&quot;color: #008000&quot;&gt;//	++first;  // advance first just past the element we added&lt;/span&gt;&lt;br /&gt;			&lt;span style=&quot;color: #008000&quot;&gt;//}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;			&lt;span style=&quot;color: #008000&quot;&gt;// safer: recalculate end on each trip whenever the loop adds/erases elements&lt;/span&gt;&lt;br /&gt;			&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; k(0);&lt;br /&gt;			&lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt; (first != v.end() &amp;amp;&amp;amp; k != 42) {&lt;br /&gt;				&lt;span style=&quot;color: #008000&quot;&gt;// do some processing&lt;/span&gt;&lt;br /&gt;				first = v.insert(first, ++k); &lt;span style=&quot;color: #008000&quot;&gt;// insert new value&lt;/span&gt;&lt;br /&gt;				++first; &lt;span style=&quot;color: #008000&quot;&gt;// advance first just past the element we added&lt;/span&gt;&lt;br /&gt;			}&lt;br /&gt;		}&lt;br /&gt;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;9.3.4. Relational Operators&lt;/span&gt;&quot;)&lt;br /&gt;	{&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//ivec1: 1 3 5 7 9 12&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; ivec1_array[] = {1,3, 5, 7 ,9 ,12};&lt;br /&gt;		vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; ivec1(ivec1_array,ivec1_array + &lt;span style=&quot;color: #0000ff&quot;&gt;sizeof&lt;/span&gt;(ivec1_array)/ &lt;span style=&quot;color: #0000ff&quot;&gt;sizeof&lt;/span&gt;(&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;));&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//ivec2: 0 2 4 6 8 10 12&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; ivec2_array[] = {0,2,4,6,8,10,12};&lt;br /&gt;		vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; ivec2(ivec2_array, ivec2_array + &lt;span style=&quot;color: #0000ff&quot;&gt;sizeof&lt;/span&gt;(ivec2_array)/&lt;span style=&quot;color: #0000ff&quot;&gt;sizeof&lt;/span&gt;(&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;));&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//ivec3: 1 3 9&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; ivec3_array[] = {1,3,9};&lt;br /&gt;		vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; ivec3(ivec3_array,ivec3_array + &lt;span style=&quot;color: #0000ff&quot;&gt;sizeof&lt;/span&gt;(ivec3_array)/&lt;span style=&quot;color: #0000ff&quot;&gt;sizeof&lt;/span&gt;(&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;));&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//ivec4: 1 3 5 7&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; ivec4_array[] = {1,3,5,7};&lt;br /&gt;		vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; ivec4(ivec4_array,ivec4_array + &lt;span style=&quot;color: #0000ff&quot;&gt;sizeof&lt;/span&gt;(ivec4_array)/&lt;span style=&quot;color: #0000ff&quot;&gt;sizeof&lt;/span&gt;(&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;));&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//ivec5: 1 3 5 7 9 12&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; ivec5_array[] = {1,3,5,7,9,12};&lt;br /&gt;		vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; ivec5(ivec5_array,ivec5_array + &lt;span style=&quot;color: #0000ff&quot;&gt;sizeof&lt;/span&gt;(ivec5_array)/&lt;span style=&quot;color: #0000ff&quot;&gt;sizeof&lt;/span&gt;(&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;));&lt;br /&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// ivec1 and ivec2 differ at element[0]: ivec1 greater than ivec2&lt;/span&gt;&lt;br /&gt;		cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;ivec1 &amp;lt; ivec2 = &lt;/span&gt;&quot; &amp;lt;&amp;lt; (ivec1 &amp;lt; ivec2) &amp;lt;&amp;lt; endl; &lt;span style=&quot;color: #008000&quot;&gt;// false&lt;/span&gt;&lt;br /&gt;		cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;ivec2 &amp;lt; ivec1 = &lt;/span&gt;&quot; &amp;lt;&amp;lt; (ivec2 &amp;lt; ivec1) &amp;lt;&amp;lt; endl; &lt;span style=&quot;color: #008000&quot;&gt;// true&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// ivec1 and ivec3 differ at element[2]: ivec1 less than ivec3&lt;/span&gt;&lt;br /&gt;		cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;ivec1 &amp;lt; ivec3 = &lt;/span&gt;&quot; &amp;lt;&amp;lt; (ivec1 &amp;lt; ivec3 ) &amp;lt;&amp;lt; endl; &lt;span style=&quot;color: #008000&quot;&gt;// true&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// all elements equal, but ivec4 has fewer elements, so ivec1 is greater than ivec4&lt;/span&gt;&lt;br /&gt;		cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;ivec1 &amp;lt; ivec4 = &lt;/span&gt;&quot; &amp;lt;&amp;lt; (ivec1 &amp;lt; ivec4 )&amp;lt;&amp;lt; endl; &lt;span style=&quot;color: #008000&quot;&gt;// false&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;		cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;ivec1 == ivec5 = &lt;/span&gt;&quot; &amp;lt;&amp;lt; (ivec1 == ivec5 )&amp;lt;&amp;lt; endl; &lt;span style=&quot;color: #008000&quot;&gt;// true; each element equal and same number of elements&lt;/span&gt;&lt;br /&gt;		cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;ivec1 == ivec4 = &lt;/span&gt;&quot; &amp;lt;&amp;lt; (ivec1 == ivec4 )&amp;lt;&amp;lt; endl; &lt;span style=&quot;color: #008000&quot;&gt;// false; ivec4 has fewer elements than ivec1&lt;/span&gt;&lt;br /&gt;		cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;ivec1 != ivec4 = &lt;/span&gt;&quot; &amp;lt;&amp;lt; (ivec1 != ivec4 )&amp;lt;&amp;lt; endl; &lt;span style=&quot;color: #008000&quot;&gt;// true; ivec4 has fewer elements than ivec1&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;9.3.5. Container Size Operations&lt;/span&gt;&quot;)&lt;br /&gt;	{&lt;br /&gt;		list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; ilist(10, 42);   &lt;span style=&quot;color: #008000&quot;&gt;// 10 ints: each has value 42&lt;/span&gt;&lt;br /&gt;		PrintOut(ilist);&lt;br /&gt;		ilist.resize(15);          &lt;span style=&quot;color: #008000&quot;&gt;// adds 5 elements of value 0 to back of ilist&lt;/span&gt;&lt;br /&gt;		PrintOut(ilist);&lt;br /&gt;		ilist.resize(25, -1);      &lt;span style=&quot;color: #008000&quot;&gt;// adds 10 elements of value -1 to back of ilist&lt;/span&gt;&lt;br /&gt;		PrintOut(ilist);&lt;br /&gt;		ilist.resize(5);           &lt;span style=&quot;color: #008000&quot;&gt;// erases 20 elements from the back of ilist&lt;/span&gt;&lt;br /&gt;		PrintOut(ilist);&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;9.3.6. Accessing Elements&lt;/span&gt;&quot;)&lt;br /&gt;	{&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// check that there are elements before dereferencing an iterator&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// or calling front or back&lt;/span&gt;&lt;br /&gt;		list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; ilist;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; i(10);&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt;( i != 0)&lt;br /&gt;			ilist.push_front(--i);&lt;br /&gt;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt; (!ilist.empty()) {&lt;br /&gt;			&lt;span style=&quot;color: #008000&quot;&gt;// val and val2 refer to the same element&lt;/span&gt;&lt;br /&gt;			list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt;::reference val = *ilist.begin();&lt;br /&gt;			list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt;::reference val2 = ilist.front();&lt;br /&gt;			assert(val == val2);&lt;br /&gt;			&lt;span style=&quot;color: #008000&quot;&gt;// last and last2 refer to the same element&lt;/span&gt;&lt;br /&gt;			list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt;::reference last = *--ilist.end();&lt;br /&gt;			list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt;::reference last2 = ilist.back(); &lt;br /&gt;			assert(last == last2);&lt;br /&gt;		}&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// empty vector&lt;/span&gt;&lt;br /&gt;		vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; svec;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//try&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//{&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//	cout &amp;lt;&amp;lt; svec[0];         // run-time error: There are no elements in svec!&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//}		&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//catch (runtime_error* e)&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//{&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//	cout &amp;lt;&amp;lt; &quot;the index access failed!&quot; &amp;lt;&amp;lt; endl;&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//	//goto atmethod;&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//}&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//catch (out_of_range* e)&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//{&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//	cout &amp;lt;&amp;lt; &quot;the at method access failed!&quot; &amp;lt;&amp;lt; endl;&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//}&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//catch (exception&amp;amp; e)&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//{&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//	cout &amp;lt;&amp;lt; e.what()&amp;lt;&amp;lt; endl;&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//}&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//try&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//{&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//	cout &amp;lt;&amp;lt; svec.at(0);      // throws out_of_range exception&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//}&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//catch (out_of_range* e)&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//{&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//	// do nothing and only print message&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//	cout &amp;lt;&amp;lt; &quot;the at method access failed!&quot; &amp;lt;&amp;lt; endl;&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//}&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//catch (exception&amp;amp; e)&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//{&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//	cout &amp;lt;&amp;lt; e.what()&amp;lt;&amp;lt; endl;&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//}	&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;	}&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;9.3.7. Erasing Elements&lt;/span&gt;&quot;)&lt;br /&gt;	{&lt;br /&gt;		list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; ilist;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; i(10);&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt;( i!=0)&lt;br /&gt;			ilist.push_back(--i);&lt;br /&gt;&lt;br /&gt;		PrintOut(ilist);&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt; (!ilist.empty()) {			&lt;br /&gt;			process(ilist.front()); &lt;span style=&quot;color: #008000&quot;&gt;// do something with the current top of ilist&lt;/span&gt;&lt;br /&gt;			ilist.pop_front();      &lt;span style=&quot;color: #008000&quot;&gt;// done; remove first element&lt;/span&gt;&lt;br /&gt;			PrintOut(ilist);&lt;br /&gt;		}&lt;br /&gt;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Removing an Element From within the Container&lt;/span&gt;&quot;)&lt;br /&gt;		{&lt;br /&gt;			&lt;span style=&quot;color: #0000ff&quot;&gt;char&lt;/span&gt; * words[] = {&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;a&lt;/span&gt;&quot;,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;b&lt;/span&gt;&quot;,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;c&lt;/span&gt;&quot;,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;d&lt;/span&gt;&quot;,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;e&lt;/span&gt;&quot;,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;f&lt;/span&gt;&quot;,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Quasimodo&lt;/span&gt;&quot;,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;g&lt;/span&gt;&quot;,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Quasimodo&lt;/span&gt;&quot;,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;h&lt;/span&gt;&quot;};&lt;br /&gt;			list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; slist(words, words + &lt;span style=&quot;color: #0000ff&quot;&gt;sizeof&lt;/span&gt;(words)/&lt;span style=&quot;color: #0000ff&quot;&gt;sizeof&lt;/span&gt;(&lt;span style=&quot;color: #0000ff&quot;&gt;char&lt;/span&gt; *));&lt;br /&gt;			PrintOut(slist);&lt;br /&gt;&lt;br /&gt;			&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; searchValue(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Quasimodo&lt;/span&gt;&quot;);&lt;br /&gt;			list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;::iterator iter;&lt;br /&gt;			&lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt;( iter = find(slist.begin(), slist.end(), searchValue),iter != slist.end())&lt;br /&gt;			{&lt;br /&gt;				slist.erase(iter);&lt;br /&gt;				PrintOut(slist);&lt;br /&gt;			}&lt;br /&gt;&lt;br /&gt;		}&lt;br /&gt;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Removing All the Elements in a Container&lt;/span&gt;&quot;)&lt;br /&gt;		{&lt;br /&gt;			&lt;span style=&quot;color: #0000ff&quot;&gt;char&lt;/span&gt; * words[] = {&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;a&lt;/span&gt;&quot;,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;b&lt;/span&gt;&quot;,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;c&lt;/span&gt;&quot;,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;d&lt;/span&gt;&quot;,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;e&lt;/span&gt;&quot;,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;f&lt;/span&gt;&quot;,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Quasimodo&lt;/span&gt;&quot;,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;g&lt;/span&gt;&quot;,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Quasimodo&lt;/span&gt;&quot;,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;h&lt;/span&gt;&quot;};&lt;br /&gt;			list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; slist(words, words + &lt;span style=&quot;color: #0000ff&quot;&gt;sizeof&lt;/span&gt;(words)/&lt;span style=&quot;color: #0000ff&quot;&gt;sizeof&lt;/span&gt;(&lt;span style=&quot;color: #0000ff&quot;&gt;char&lt;/span&gt; *));&lt;br /&gt;			PrintOut(slist);&lt;br /&gt;			slist.clear(); &lt;span style=&quot;color: #008000&quot;&gt;// delete all the elements within the container&lt;/span&gt;&lt;br /&gt;			PrintOut(slist);&lt;br /&gt;			slist.erase(slist.begin(), slist.end()); &lt;span style=&quot;color: #008000&quot;&gt;// equivalent&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;		}&lt;br /&gt;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;9.3.8. Assignment and swap&lt;/span&gt;&quot;)&lt;br /&gt;	{&lt;br /&gt;		vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; ivect1(10,1);&lt;br /&gt;		PrintOut(ivect1);&lt;br /&gt;		vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; ivect2(20,2);&lt;br /&gt;		PrintOut(ivect2);&lt;br /&gt;&lt;br /&gt;		ivect1 = ivect2; &lt;span style=&quot;color: #008000&quot;&gt;// replace contents of c1 with a copy of elements in ivect2&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// equivalent operation using erase and insert&lt;/span&gt;&lt;br /&gt;		ivect1.erase(ivect1.begin(), ivect1.end()); &lt;span style=&quot;color: #008000&quot;&gt;// delete all elements in c1&lt;/span&gt;&lt;br /&gt;		ivect1.insert(ivect1.begin(), ivect2.begin(), ivect2.end()); &lt;span style=&quot;color: #008000&quot;&gt;// insert ivect2&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Using assign&lt;/span&gt;&quot;)&lt;br /&gt;		{&lt;br /&gt;			list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; slist1(10,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;1&lt;/span&gt;&quot;);&lt;br /&gt;			list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; slist2(20,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;2&lt;/span&gt;&quot;);&lt;br /&gt;			PrintOut(slist1);&lt;br /&gt;			PrintOut(slist2);&lt;br /&gt;			&lt;span style=&quot;color: #008000&quot;&gt;// equivalent to slist1 = slist2&lt;/span&gt;&lt;br /&gt;			slist1.assign(slist2.begin(), slist2.end());&lt;br /&gt;			PrintOut(slist1);&lt;br /&gt;&lt;br /&gt;			&lt;span style=&quot;color: #008000&quot;&gt;// equivalent to: slist1.clear();&lt;/span&gt;&lt;br /&gt;			&lt;span style=&quot;color: #008000&quot;&gt;// followed by slist1.insert(slist1.begin(), 10, &quot;Hiya!&quot;);&lt;/span&gt;&lt;br /&gt;			slist1.assign(10, &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Hiya!&lt;/span&gt;&quot;); &lt;span style=&quot;color: #008000&quot;&gt;// 10 elements; each one is Hiya!&lt;/span&gt;&lt;br /&gt;			PrintOut(slist1);&lt;br /&gt;&lt;br /&gt;		}&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Using swap to Avoid the Cost of Deleting Elements&lt;/span&gt;&quot;)&lt;br /&gt;		{&lt;br /&gt;			vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; svec1(10,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;1&lt;/span&gt;&quot;); &lt;span style=&quot;color: #008000&quot;&gt;// vector with 10 elements&lt;/span&gt;&lt;br /&gt;			vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; svec2(24,&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;2&lt;/span&gt;&quot;); &lt;span style=&quot;color: #008000&quot;&gt;// vector with 24 elements&lt;/span&gt;&lt;br /&gt;			vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;::iterator it_svec1 = svec1.begin() + 2;&lt;br /&gt;			vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;::iterator it_svec2 = svec2.begin() + 3;&lt;br /&gt;			assert(it_svec1 == svec1.begin()  + 2 );&lt;br /&gt;			assert(it_svec2 == svec2.begin() + 3 );&lt;br /&gt;			PrintOut(svec1);&lt;br /&gt;			PrintOut(svec2);&lt;br /&gt;			svec1.swap(svec2);&lt;br /&gt;			assert(it_svec1 == svec2.begin()  + 2 );&lt;br /&gt;			assert(it_svec2 == svec1.begin() + 3 );&lt;br /&gt;			PrintOut(svec1);&lt;br /&gt;			PrintOut(svec2);&lt;br /&gt;&lt;br /&gt;		}&lt;br /&gt;&lt;br /&gt;	}&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; i;&lt;br /&gt;	cin &amp;gt;&amp;gt; i;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; 0;&lt;br /&gt;}&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; process(&lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt;::reference re)&lt;br /&gt;{&lt;br /&gt;	&lt;span style=&quot;color: #008000&quot;&gt;// print out value&lt;/span&gt;&lt;br /&gt;	cout &amp;lt;&amp;lt; re &amp;lt;&amp;lt; endl;&lt;br /&gt;}&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; clearstream(istream &amp;amp;&lt;span style=&quot;color: #0000ff&quot;&gt;in&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;	&lt;span style=&quot;color: #008000&quot;&gt;//in.clear(istream::goodbit);&lt;/span&gt;&lt;br /&gt;	&lt;span style=&quot;color: #008000&quot;&gt;//in.clear(istream::eofbit);&lt;/span&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;in&lt;/span&gt;.clear();&lt;br /&gt;	&lt;span style=&quot;color: #008000&quot;&gt;//in.seekg(0,ios_base::beg);&lt;/span&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;in&lt;/span&gt;.seekg(0);&lt;br /&gt;}&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; PrintOut(list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; &amp;amp; slst)&lt;br /&gt;{&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; a(30,&#39;*&#39;);&lt;br /&gt;	cout &amp;lt;&amp;lt; a &amp;lt;&amp;lt; endl;&lt;br /&gt;	cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;list size:&lt;/span&gt;&quot; &amp;lt;&amp;lt; slst.size() &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt; element: &lt;/span&gt;&quot;;&lt;br /&gt;&lt;br /&gt;	list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;::iterator it = slst.begin();&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt;( it != slst.end())&lt;br /&gt;		cout &amp;lt;&amp;lt; *it++ &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;, &lt;/span&gt;&quot; ;&lt;br /&gt;&lt;br /&gt;	cout &amp;lt;&amp;lt; endl;&lt;br /&gt;}&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; PrintOut(vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; &amp;amp; ivect)&lt;br /&gt;{&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; a(30,&#39;*&#39;);&lt;br /&gt;	cout &amp;lt;&amp;lt; a &amp;lt;&amp;lt; endl;&lt;br /&gt;	cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;vector size:&lt;/span&gt;&quot; &amp;lt;&amp;lt; ivect.size() &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt; element: &lt;/span&gt;&quot;;&lt;br /&gt;&lt;br /&gt;	vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt;::iterator it = ivect.begin();&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt;( it != ivect.end())&lt;br /&gt;		cout &amp;lt;&amp;lt; *it++ &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;, &lt;/span&gt;&quot; ;&lt;br /&gt;&lt;br /&gt;	cout &amp;lt;&amp;lt; endl;&lt;br /&gt;}&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; PrintOut(list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; &amp;amp; ilst)&lt;br /&gt;{&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; a(30,&#39;*&#39;);&lt;br /&gt;	cout &amp;lt;&amp;lt; a &amp;lt;&amp;lt; endl;&lt;br /&gt;	cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;list size:&lt;/span&gt;&quot; &amp;lt;&amp;lt; ilst.size() &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt; element: &lt;/span&gt;&quot;;&lt;br /&gt;&lt;br /&gt;	list&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt;::iterator it = ilst.begin();&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt;( it != ilst.end())&lt;br /&gt;		cout &amp;lt;&amp;lt; *it++ &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;, &lt;/span&gt;&quot; ;&lt;br /&gt;&lt;br /&gt;	cout &amp;lt;&amp;lt; endl;&lt;br /&gt;}&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; PrintOut(vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; &amp;amp;sivect)&lt;br /&gt;{&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; a(30,&#39;*&#39;);&lt;br /&gt;	cout &amp;lt;&amp;lt; a &amp;lt;&amp;lt; endl;&lt;br /&gt;	cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;vector size:&lt;/span&gt;&quot; &amp;lt;&amp;lt; sivect.size() &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt; element: &lt;/span&gt;&quot;;&lt;br /&gt;&lt;br /&gt;	vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;::iterator it = sivect.begin();&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt;( it != sivect.end())&lt;br /&gt;		cout &amp;lt;&amp;lt; *it++ &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;, &lt;/span&gt;&quot; ;&lt;br /&gt;&lt;br /&gt;	cout &amp;lt;&amp;lt; endl;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;h4&gt;Section 9.4 How a vector Grows&lt;/h4&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Ordinarily, we should not care about how a library type works: All we should care about is how to use it. &lt;strong&gt;However, in the case of &lt;tt&gt;vector&lt;/tt&gt;s, a bit of the implementation leaks into its interface&lt;/strong&gt;. To support fast random access, &lt;tt&gt;vector&lt;/tt&gt; elements are stored contiguouslyeach element is adjacent to the previous element &lt;br /&gt;&lt;li&gt;Given that elements are contiguous, let&#39;s think about what happens when we add an element to a &lt;tt&gt;vector&lt;/tt&gt;: If there is no room in the &lt;tt&gt;vector&lt;/tt&gt; for the new element, it cannot just add an element somewhere else in memory because the elements must be contiguous for indexing to work. Instead, the &lt;tt&gt;vector&lt;/tt&gt; must allocate new memory to hold the existing elements plus the new one, copy the elements from the old location into the new space, add the new element, and deallocate the old memory. If &lt;tt&gt;vector&lt;/tt&gt; did this memory allocation and deallocation each time we added an element, then performance would be unacceptably slow &lt;br /&gt;&lt;li&gt;There is no comparable allocation issue for containers that do not hold their elements contiguously &lt;br /&gt;&lt;li&gt;&lt;strong&gt;One might conclude, therefore, that in general it is a good idea to use a &lt;tt&gt;list&lt;/tt&gt; rather than a &lt;tt&gt;vector&lt;/tt&gt;. However, the contrary is usually the case: For most applications the best container to use is a &lt;tt&gt;vector&lt;/tt&gt;. The reason is that library implementors use allocation strategies that minimize the costs of storing elements contiguously. That cost is usually offset by the advantages in accessing elements that contiguous storage allows&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;The way &lt;tt&gt;vector&lt;/tt&gt;s achieve fast allocation is by allocating capacity beyond what is immediately needed. The &lt;tt&gt;vector&lt;/tt&gt; holds this storage in reserve and uses it to allocate new elements as they are added. Thus, there is no need to reallocate the container for each new element. The exact amount of additional capacity allocated varies across different implementations of the library. This allocation strategy is dramatically more efficient than reallocating the container each time an element is added. In fact, its performance is good enough that in practice a &lt;tt&gt;vector&lt;/tt&gt; usually grows more efficiently than a &lt;tt&gt;list&lt;/tt&gt; or a &lt;tt&gt;deque&lt;/tt&gt;&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;9.4.1. capacity and reserve Members &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;The &lt;tt&gt;vector&lt;/tt&gt; class includes two members, &lt;tt&gt;capacity&lt;/tt&gt; and &lt;tt&gt;reserve&lt;/tt&gt;, that let us interact with the memory-allocation part of &lt;tt&gt;vector&lt;/tt&gt;&#39;s implementation. The &lt;tt&gt;capacity&lt;/tt&gt; operation tells us how many elements the container could hold before it must allocate more space. The &lt;tt&gt;reserve&lt;/tt&gt; operation lets us tell the &lt;tt&gt;vector&lt;/tt&gt; how many elements it should be prepared to hold &lt;br /&gt;&lt;li&gt;&lt;strong&gt;It is important to understand the difference between &lt;tt&gt;capacity&lt;/tt&gt; and &lt;tt&gt;size&lt;/tt&gt;. The &lt;tt&gt;size&lt;/tt&gt; is the number of elements in the &lt;tt&gt;vector&lt;/tt&gt;; &lt;tt&gt;capacity&lt;/tt&gt; is how many it could hold before new space must be allocated&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;We know that the &lt;tt&gt;size&lt;/tt&gt; of an empty &lt;tt&gt;vector&lt;/tt&gt; is zero, and evidently our library also sets &lt;tt&gt;capacity&lt;/tt&gt; of an empty &lt;tt&gt;vector&lt;/tt&gt; to zero&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;Because we used only reserved capacity, there is no need for the &lt;tt&gt;vector&lt;/tt&gt; to do any allocation. In fact, as long as there is excess capacity, the &lt;tt&gt;vector&lt;/tt&gt; must not reallocate its elements&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;this &lt;tt&gt;vector&lt;/tt&gt; implementation appears to follow a strategy of doubling the current capacity each time it has to allocate new storage &lt;br /&gt;&lt;li&gt;&lt;strong&gt;Each implementation of &lt;tt&gt;vector&lt;/tt&gt; is free to choose its own allocation strategy. However, it must provide the &lt;tt&gt;reserve&lt;/tt&gt; and &lt;tt&gt;capacity&lt;/tt&gt; functions, and it must not allocate new memory until it is forced to do so. How much memory it allocates is up to the implementation. Different libraries will implement different strategies&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;Moreover, every implementation is required to follow a strategy that ensures that it is efficient to use &lt;tt&gt;push_back&lt;/tt&gt; to populate a &lt;tt&gt;vector&lt;/tt&gt;. Technically speaking, the execution time of creating an n-element &lt;tt&gt;vector&lt;/tt&gt; by calling &lt;tt&gt;push_back&lt;/tt&gt; n times on an initially empty &lt;tt&gt;vector&lt;/tt&gt; is never more than a constant multiple of n&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;pre&gt;&lt;span style=&quot;color: #008000&quot;&gt;// EXE-9.4.cpp : Defines the entry point for the console application.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #008000&quot;&gt;//&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;#include &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;stdafx.h&lt;/span&gt;&quot;&lt;br /&gt;#include &amp;lt;vector&amp;gt;&lt;br /&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;#include &amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;namespace&lt;/span&gt; std;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; main(&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; argc, _TCHAR* argv[])&lt;br /&gt;{&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;9.4.1. capacity and reserve Members&lt;/span&gt;&quot;)&lt;br /&gt;	{&lt;br /&gt;		vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt; ivec;&lt;br /&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// size should be zero; capacity is implementation defined&lt;/span&gt;&lt;br /&gt;		cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;ivec: size: &lt;/span&gt;&quot; &amp;lt;&amp;lt; ivec.size()&lt;br /&gt;			&amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt; capacity: &lt;/span&gt;&quot;  &amp;lt;&amp;lt; ivec.capacity() &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// give ivec 24 elements&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;for&lt;/span&gt; (vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt;&amp;gt;::size_type ix = 0; ix != 24; ++ix)&lt;br /&gt;			ivec.push_back(ix);&lt;br /&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// size should be 24; capacity will be &amp;gt;= 24 and is implementation defined&lt;/span&gt;&lt;br /&gt;		cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;ivec: size: &lt;/span&gt;&quot; &amp;lt;&amp;lt; ivec.size()&lt;br /&gt;			&amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt; capacity: &lt;/span&gt;&quot;  &amp;lt;&amp;lt; ivec.capacity() &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;br /&gt;		ivec.reserve(50);  &lt;span style=&quot;color: #008000&quot;&gt;// sets capacity to at least 50; might be more&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// size should be 24; capacity will be &amp;gt;= 50 and is implementation defined&lt;/span&gt;&lt;br /&gt;		cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;ivec: size: &lt;/span&gt;&quot; &amp;lt;&amp;lt; ivec.size()&lt;br /&gt;			&amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt; capacity: &lt;/span&gt;&quot;  &amp;lt;&amp;lt; ivec.capacity() &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// add elements to use up the excess capacity&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt; (ivec.size() != ivec.capacity())&lt;br /&gt;			ivec.push_back(0);&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// size should be 50; capacity should be unchanged&lt;/span&gt;&lt;br /&gt;		cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;ivec: size: &lt;/span&gt;&quot; &amp;lt;&amp;lt; ivec.size()&lt;br /&gt;			&amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt; capacity: &lt;/span&gt;&quot;  &amp;lt;&amp;lt; ivec.capacity() &amp;lt;&amp;lt; endl;&lt;br /&gt;		&lt;br /&gt;		ivec.push_back(42); &lt;span style=&quot;color: #008000&quot;&gt;// add one more element&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// size should be 51; capacity will be &amp;gt;= 51 and is implementation defined&lt;/span&gt;&lt;br /&gt;		cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;ivec: size: &lt;/span&gt;&quot; &amp;lt;&amp;lt; ivec.size()&lt;br /&gt;			&amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt; capacity: &lt;/span&gt;&quot;  &amp;lt;&amp;lt; ivec.capacity() &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;br /&gt;	}&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; i;&lt;br /&gt;	cin &amp;gt;&amp;gt; i;&lt;br /&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;h4&gt;Section 9.5 Deciding Which Container to Use&lt;/h4&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Whether elements are stored contiguously has other significant impacts on:&lt;/strong&gt; &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;The costs to add or delete elements from the middle of the container&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;The costs to perform nonsequential access to elements of the container&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;The degree to which a program does these operations should be used to determine which type of container to use. The &lt;tt&gt;vector&lt;/tt&gt; and &lt;tt&gt;deque&lt;/tt&gt; types provide fast non-sequential access to elements at the cost of making it expensive to add or remove elements anywhere other than the ends of the container. The &lt;tt&gt;list&lt;/tt&gt; type supports fast insertion and deletion anywhere but at the cost of making nonsequential access to elements expensive &lt;br /&gt;&lt;li&gt;How Insertion Affects Choice of Container &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;A &lt;tt&gt;list&lt;/tt&gt; represents noncontiguous memory and allows for both forward and backward traversal one element at a time. It is efficient to &lt;tt&gt;insert&lt;/tt&gt; or &lt;tt&gt;erase&lt;/tt&gt; an element at any point. Inserting or removing an element in a &lt;tt&gt;list&lt;/tt&gt; does not move any other elements. Random access, on the other hand, is not supported. Accessing an element requires traversing the intervening elements &lt;br /&gt;&lt;p&gt;A &lt;tt&gt;deque&lt;/tt&gt; is a more complicated data structure. We are guaranteed that adding or removing elements from either end of the &lt;tt&gt;deque&lt;/tt&gt; is a fast operation. Adding or removing from the middle will be more expensive. A &lt;tt&gt;deque&lt;/tt&gt; offers some properties of both &lt;tt&gt;list&lt;/tt&gt; and &lt;tt&gt;vector&lt;/tt&gt;: &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;Like &lt;tt&gt;vector&lt;/tt&gt;, it is inefficient to &lt;tt&gt;insert&lt;/tt&gt; or &lt;tt&gt;erase&lt;/tt&gt; elements in the middle of the &lt;tt&gt;deque&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;Unlike &lt;tt&gt;vector&lt;/tt&gt;, a &lt;tt&gt;deque&lt;/tt&gt; offers efficient &lt;tt&gt;insert&lt;/tt&gt; and &lt;tt&gt;erase&lt;/tt&gt; at the front as well as at the back.&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;Unlike &lt;tt&gt;list&lt;/tt&gt; and like &lt;tt&gt;vector&lt;/tt&gt;, a &lt;tt&gt;deque&lt;/tt&gt; supports fast random access to any element.&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;Inserting elements at the front or back of a &lt;tt&gt;deque&lt;/tt&gt; does not invalidate any iterators. Erasing the front or back element invalidates only iterators referring to the element(s) erased. Inserting or erasing anywhere else in the &lt;tt&gt;deque&lt;/tt&gt; invalidates all iterators referring to elements of the &lt;tt&gt;deque&lt;/tt&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;How Access to the Elements Affects Choice of Container &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;It is much slower to jump around in a &lt;tt&gt;list&lt;/tt&gt;. the only way to move between the elements of a &lt;tt&gt;list&lt;/tt&gt; is to sequentially follow the pointers. Moving from the 5th to the 15th element requires visiting every element between them &lt;br /&gt;&lt;li&gt;&lt;strong&gt;In general, unless there is a good reason to prefer another container, &lt;tt&gt;vector&lt;/tt&gt; is usually the right one to use&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Hints on Selecting Which Container to Use &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;h6&gt;Hints on Selecting Which Container to Use&lt;/h6&gt;&lt;br /&gt;&lt;p&gt;There are a few rules of thumb that apply to selecting which container to use: &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;If the program requires random access to elements, use a &lt;tt&gt;vector&lt;/tt&gt; or a &lt;tt&gt;deque&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;If the program needs to insert or delete elements in the middle of the container, use a &lt;tt&gt;list&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;If the program needs to insert or delete elements at the front and the back, but not in the middle, of the container, use a &lt;tt&gt;deque&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;If we need to insert elements in the middle of the container only while reading input and then need random access to the elements, consider reading them into a &lt;tt&gt;list&lt;/tt&gt; and then reordering the &lt;tt&gt;list&lt;/tt&gt; as appropriate for subsequent access and copying the reordered &lt;tt&gt;list&lt;/tt&gt; into a &lt;tt&gt;vector&lt;/tt&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Deciding which container to use may require profiling the performance of each container type doing the kinds of operations the application requires &lt;br /&gt;&lt;li&gt;&lt;strong&gt;When you are not certain which container the application should use, try to write your code so that it uses only operations common to both &lt;tt&gt;vector&lt;/tt&gt;s and &lt;tt&gt;lists:&lt;/tt&gt; Use iterators, not subscripts, and avoid random access to elements. By writing your programs this way, it will be easier to change the container from a &lt;tt&gt;vector&lt;/tt&gt; to a &lt;tt&gt;list&lt;/tt&gt; if necessary&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;h4&gt;Section 9.6 strings Revisited&lt;/h4&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 9.12. &lt;tt&gt;string&lt;/tt&gt; Operations Introduced in &lt;a class=&quot;docLink&quot; href=&quot;ch03lev1sec2.html#ch03lev1sec2&quot;&gt;Section 3.2&lt;/a&gt;&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;150&quot;&gt;&lt;br /&gt;&lt;col width=&quot;350&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;string s;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Defines a new, empty &lt;tt&gt;string&lt;/tt&gt; named &lt;tt&gt;s&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;string s(cp);&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Defines a new &lt;tt&gt;string&lt;/tt&gt; initialized from the null-terminated C-style string pointed to by &lt;tt&gt;cp&lt;/tt&gt;.&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;string s(s2);&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Defines a new &lt;tt&gt;string&lt;/tt&gt; initialized as a copy of &lt;tt&gt;s2&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;is &amp;gt;&amp;gt; s;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Reads a whitespace-separated string from the input stream &lt;tt&gt;is&lt;/tt&gt; into &lt;tt&gt;s&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;os &amp;lt;&amp;lt; s;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Writes &lt;tt&gt;s&lt;/tt&gt; to the output stream &lt;tt&gt;os&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;getline(is, s)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Reads characters up to the first newline from input stream &lt;tt&gt;is&lt;/tt&gt; into &lt;tt&gt;s&lt;/tt&gt;&lt;/strong&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s1 + s2&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Concatenates &lt;tt&gt;s1&lt;/tt&gt; and &lt;tt&gt;s2&lt;/tt&gt;, yielding a new &lt;tt&gt;string&lt;/tt&gt;&lt;/strong&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s1 += s2&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;Appends &lt;tt&gt;s2&lt;/tt&gt; to &lt;tt&gt;s1&lt;/tt&gt;&lt;/strong&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Relational Operators&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;The equality (&lt;tt&gt;==&lt;/tt&gt; and &lt;tt&gt;!=&lt;/tt&gt;) and relational (&lt;tt&gt;&amp;lt;, &amp;lt;=, &amp;gt;&lt;/tt&gt;, and &lt;tt&gt;&amp;gt;=)&lt;/tt&gt; can be used to compare &lt;tt&gt;string&lt;/tt&gt;s. &lt;tt&gt;string&lt;/tt&gt; comparison is equivalent to (case-sensitive) dictionary ordering&lt;/strong&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;In addition to the operations we&#39;ve already used, &lt;tt&gt;string&lt;/tt&gt;s also supports most of the sequential container operations. &lt;strong&gt;In some ways, we can think of a &lt;tt&gt;string&lt;/tt&gt; as a container of characters&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;The container operations that &lt;tt&gt;string&lt;/tt&gt; supports are: &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;The typedefs, including the iterator types, listed in &lt;a href=&quot;ch09lev1sec3.html#ch09table05&quot;&gt;Table 9.5&lt;/a&gt; (p. &lt;a href=&quot;ch09lev1sec3.html#ch09table05&quot;&gt;316&lt;/a&gt;).&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;The constructors listed in &lt;a href=&quot;ch09lev1sec1.html#ch09table02&quot;&gt;Table 9.2&lt;/a&gt; (p. &lt;a href=&quot;ch09lev1sec1.html#ch09table02&quot;&gt;307&lt;/a&gt;) &lt;strong&gt;except for the constructor that takes a single size parameter.&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;The operations to add elements listed in &lt;a href=&quot;ch09lev1sec3.html#ch09table07&quot;&gt;Table 9.7&lt;/a&gt; (p. &lt;a href=&quot;ch09lev1sec3.html#ch09table07&quot;&gt;319&lt;/a&gt;) that &lt;tt&gt;vector&lt;/tt&gt; supports. Note: Neither &lt;tt&gt;vector&lt;/tt&gt; nor &lt;tt&gt;string&lt;/tt&gt; supports &lt;tt&gt;push_front&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;The size operations in &lt;a href=&quot;ch09lev1sec3.html#ch09table08&quot;&gt;Table 9.8&lt;/a&gt; (p. &lt;a href=&quot;ch09lev1sec3.html#ch09table08&quot;&gt;324&lt;/a&gt;).&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;The subscript and &lt;tt&gt;at&lt;/tt&gt; operations listed in &lt;a href=&quot;ch09lev1sec3.html#ch09table09&quot;&gt;Table 9.9&lt;/a&gt; (p. &lt;a href=&quot;ch09lev1sec3.html#ch09table09&quot;&gt;325&lt;/a&gt;); &lt;tt&gt;string&lt;/tt&gt; does not provide &lt;tt&gt;back&lt;/tt&gt; or &lt;tt&gt;front&lt;/tt&gt; operations listed in that table.&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;The &lt;tt&gt;begin&lt;/tt&gt; and &lt;tt&gt;end&lt;/tt&gt; operations of &lt;a href=&quot;ch09lev1sec3.html#ch09table06&quot;&gt;Table 9.6&lt;/a&gt; (p. &lt;a href=&quot;ch09lev1sec3.html#ch09table06&quot;&gt;317&lt;/a&gt;).&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;The &lt;tt&gt;erase&lt;/tt&gt; and &lt;tt&gt;clear&lt;/tt&gt; operations of &lt;a href=&quot;ch09lev1sec3.html#ch09table10&quot;&gt;Table 9.10&lt;/a&gt; (p. &lt;a href=&quot;ch09lev1sec3.html#ch09table10&quot;&gt;326&lt;/a&gt;); &lt;tt&gt;string&lt;/tt&gt; does not support either &lt;tt&gt;pop_back&lt;/tt&gt; or &lt;tt&gt;pop_front&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;The assignment operations in &lt;a href=&quot;ch09lev1sec3.html#ch09table11&quot;&gt;Table 9.11&lt;/a&gt; (p. &lt;a href=&quot;ch09lev1sec3.html#ch09table11&quot;&gt;329&lt;/a&gt;).&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;Like the elements in a &lt;tt&gt;vector&lt;/tt&gt;, the characters of a &lt;tt&gt;string&lt;/tt&gt; are stored contiguously. Therefore, &lt;tt&gt;string&lt;/tt&gt; supports the &lt;tt&gt;capacity&lt;/tt&gt; and &lt;tt&gt;reserve&lt;/tt&gt; operations described in &lt;a href=&quot;ch09lev1sec4.html#ch09lev1sec4&quot;&gt;Section 9.4&lt;/a&gt; (p. &lt;a href=&quot;ch09lev1sec4.html#ch09lev1sec4&quot;&gt;330&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;The &lt;tt&gt;string&lt;/tt&gt; library defines a great number of functions, which use repeated patterns. Given the number of functions supported, this section can be mind-numbing on first reading&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;9.6.1. Other Ways to Construct strings&lt;/p&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;The definition of &lt;tt&gt;s4&lt;/tt&gt; is an error. This form of initialization may be called only with a null-terminated array. Passing an array that does not contain a null is a serious error (&lt;a href=&quot;ch04lev1sec3.html#ch04lev1sec3&quot;&gt;Section 4.3&lt;/a&gt;, p. &lt;a href=&quot;ch04lev1sec3.html#ch04lev1sec3&quot;&gt;130&lt;/a&gt;), although it is an error that the compiler cannot detect. What happens at run time is undefined&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 9.13. Additional Ways to Construct &lt;tt&gt;string&lt;/tt&gt;s&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;150&quot;&gt;&lt;br /&gt;&lt;col width=&quot;350&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;string s(cp, n)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Create &lt;tt&gt;s&lt;/tt&gt; as a copy of &lt;tt&gt;n&lt;/tt&gt; characters from array pointed to by &lt;tt&gt;cp&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;string s(s2, pos2)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Create &lt;tt&gt;s&lt;/tt&gt; as a copy of characters in the &lt;tt&gt;string s2&lt;/tt&gt; starting at index &lt;tt&gt;pos2&lt;/tt&gt;. Undefined if &lt;tt&gt;pos2 &amp;gt; s2.size()&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; colspan=&quot;2&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;string s(s2, pos2, len2)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Create &lt;tt&gt;s&lt;/tt&gt; as a copy of &lt;tt&gt;len2&lt;/tt&gt; characters from &lt;tt&gt;s2&lt;/tt&gt; starting at index &lt;tt&gt;pos2&lt;/tt&gt;. Undefined if &lt;tt&gt;pos2 &amp;gt; s2.size()&lt;/tt&gt;. Regardless of the value of &lt;tt&gt;len2&lt;/tt&gt;, copies at most &lt;tt&gt;s2.size() - pos2&lt;/tt&gt; characters.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableFooter&quot; valign=&quot;top&quot; colspan=&quot;2&quot; align=&quot;center&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphBoldItalic&quot;&gt;Note:&lt;/span&gt; &lt;tt&gt;n, len2&lt;/tt&gt; &lt;span class=&quot;docEmphBoldItalic&quot;&gt;and&lt;/span&gt; &lt;tt&gt;pos2&lt;/tt&gt; &lt;span class=&quot;docEmphBoldItalic&quot;&gt;are all&lt;/span&gt; &lt;tt&gt;unsigned&lt;/tt&gt; &lt;span class=&quot;docEmphBoldItalic&quot;&gt;values&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;Using a Substring as the Initializer&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;9.6.2. Other Ways to Change a string&lt;/p&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 9.14. &lt;tt&gt;string&lt;/tt&gt; Operations in Common with the Containers&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;150&quot;&gt;&lt;br /&gt;&lt;col width=&quot;375&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.insert(p, t)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Insert copy of value &lt;tt&gt;t&lt;/tt&gt; before element referred to by iterator &lt;tt&gt;p&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Returns an iterator referring to the inserted element.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.insert(p, n, t)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Insert &lt;tt&gt;n&lt;/tt&gt; copies of &lt;tt&gt;t&lt;/tt&gt; before &lt;tt&gt;p&lt;/tt&gt;. Returns &lt;tt&gt;void&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.insert(p, b, e)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Insert elements in range denoted by iterators &lt;tt&gt;b&lt;/tt&gt; and &lt;tt&gt;e&lt;/tt&gt; before &lt;tt&gt;p&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Returns &lt;tt&gt;void&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.assign(b, e)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Replace &lt;tt&gt;s&lt;/tt&gt; by elements in range denoted by &lt;tt&gt;b&lt;/tt&gt; and &lt;tt&gt;e&lt;/tt&gt;. &lt;strong&gt;For &lt;tt&gt;string&lt;/tt&gt;, returns &lt;tt&gt;s&lt;/tt&gt;, for the containers, returns &lt;tt&gt;void&lt;/tt&gt;.&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.assign(n, t)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Replace &lt;tt&gt;s&lt;/tt&gt; by &lt;tt&gt;n&lt;/tt&gt; copies of value &lt;tt&gt;t&lt;/tt&gt;. For &lt;tt&gt;string&lt;/tt&gt;, returns &lt;tt&gt;s&lt;/tt&gt;, for the containers, returns &lt;tt&gt;void&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.erase(p)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Erase element referred to by iteartor &lt;tt&gt;p&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Returns an iterator to the element after the one deleted.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.erase(b, e)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Remove elements in range denoted by &lt;tt&gt;b&lt;/tt&gt; and &lt;tt&gt;e&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Returns an iterator to the first element after the range deleted.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 9.15. &lt;tt&gt;string&lt;/tt&gt;-Specific Versions&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;200&quot;&gt;&lt;br /&gt;&lt;col width=&quot;300&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.insert(pos, n, c)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Insert &lt;tt&gt;n&lt;/tt&gt; copies of character &lt;tt&gt;c&lt;/tt&gt; before element at index &lt;tt&gt;pos&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.insert(pos, s2)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Insert copy of &lt;tt&gt;string s2&lt;/tt&gt; before &lt;tt&gt;pos&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; colspan=&quot;2&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.insert(pos, s2, pos2, len)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Insert &lt;tt&gt;len&lt;/tt&gt; characters from &lt;tt&gt;s2&lt;/tt&gt; starting at &lt;tt&gt;pos2&lt;/tt&gt; before &lt;tt&gt;pos&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.insert(pos, cp, len)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Insert &lt;tt&gt;len&lt;/tt&gt; characters from array pointed to by &lt;tt&gt;cp&lt;/tt&gt; before &lt;tt&gt;pos&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.insert(pos, cp)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Insert copy of null-terminated string pointed to by &lt;tt&gt;cp&lt;/tt&gt; before &lt;tt&gt;pos&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.assign(s2)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Replace &lt;tt&gt;s&lt;/tt&gt; by a copy of &lt;tt&gt;s2&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.assign(s2, pos2, len)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Replace &lt;tt&gt;s&lt;/tt&gt; by a copy of &lt;tt&gt;len&lt;/tt&gt; characters from &lt;tt&gt;s2&lt;/tt&gt; starting at index &lt;tt&gt;pos2&lt;/tt&gt; in &lt;tt&gt;s2&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.assign(cp, len)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Replace &lt;tt&gt;s&lt;/tt&gt; by &lt;tt&gt;len&lt;/tt&gt; characters from array pointed to by &lt;tt&gt;cp&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.assign(cp)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Replace &lt;tt&gt;s&lt;/tt&gt; by null-terminated array pointed to by &lt;tt&gt;cp&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.erase(pos, len)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Erase &lt;tt&gt;len&lt;/tt&gt; characters starting at &lt;tt&gt;pos&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableFooter&quot; valign=&quot;top&quot; colspan=&quot;2&quot; align=&quot;center&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;strong&gt;&lt;span class=&quot;docEmphBoldItalic&quot;&gt;Unless noted otherwise, all operations return a reference to &lt;tt&gt;s&lt;/tt&gt;&lt;/span&gt;.&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;Position-Based Arguments&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;Specifying the New Contents&lt;/p&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;The characters to insert or assign into the string can be taken from a character array or another string&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;9.6.3. string-Only Operations&lt;/p&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;The &lt;tt&gt;string&lt;/tt&gt; type provides several other operations that the containers do not: &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;The &lt;tt&gt;substr&lt;/tt&gt; function that returns a substring of the current &lt;tt&gt;string&lt;/tt&gt;&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;The &lt;tt&gt;append&lt;/tt&gt; and &lt;tt&gt;replace&lt;/tt&gt; functions that modify the &lt;tt&gt;string&lt;/tt&gt;&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;A family of &lt;tt&gt;find&lt;/tt&gt; functions that search the &lt;tt&gt;string&lt;/tt&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;The substr Operation&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;The append and replace Functions&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 9.17. Operations to Modify &lt;tt&gt;string&lt;/tt&gt;s (&lt;span class=&quot;docEmphasis&quot;&gt;args&lt;/span&gt; defined in &lt;a class=&quot;docLink&quot; href=&quot;#ch09table18&quot;&gt;Table 9.18&lt;/a&gt;)&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;200&quot;&gt;&lt;br /&gt;&lt;col width=&quot;325&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.append(&lt;/tt&gt; &lt;span class=&quot;docEmphBoldItalic&quot;&gt;args&lt;/span&gt;&lt;tt&gt;)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Append &lt;span class=&quot;docEmphasis&quot;&gt;args&lt;/span&gt; to &lt;tt&gt;s&lt;/tt&gt;. Returns reference to &lt;tt&gt;s&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.replace(pos, len&lt;/tt&gt;, &lt;span class=&quot;docEmphBoldItalic&quot;&gt;args&lt;/span&gt;&lt;tt&gt;)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Remove &lt;tt&gt;len&lt;/tt&gt; characters from &lt;tt&gt;s&lt;/tt&gt; starting at &lt;tt&gt;pos&lt;/tt&gt; and replace them by characters formed by &lt;span class=&quot;docEmphasis&quot;&gt;args&lt;/span&gt;. Returns reference to &lt;tt&gt;s&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;This version does not take &lt;span class=&quot;docEmphasis&quot;&gt;args&lt;/span&gt; equal to &lt;tt&gt;b2&lt;/tt&gt;&lt;/span&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;, &lt;tt&gt;e2&lt;/tt&gt;&lt;/span&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.replace(b, e&lt;/tt&gt;, &lt;span class=&quot;docEmphBoldItalic&quot;&gt;args&lt;/span&gt;&lt;tt&gt;)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Remove characters in the range denoted by iterators &lt;tt&gt;b&lt;/tt&gt; and &lt;tt&gt;e&lt;/tt&gt; and replace them by &lt;span class=&quot;docEmphasis&quot;&gt;args&lt;/span&gt;. Returns reference to &lt;tt&gt;s&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;This version does not take &lt;span class=&quot;docEmphasis&quot;&gt;args&lt;/span&gt; equal to &lt;tt&gt;s2, pos2, len2&lt;/tt&gt;.&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;There is no requirement that the size of the text removed and inserted be the same&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 9.18. Arguments to &lt;tt&gt;append&lt;/tt&gt; and &lt;tt&gt;replace&lt;/tt&gt;&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;150&quot;&gt;&lt;br /&gt;&lt;col width=&quot;370&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s2&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;The &lt;tt&gt;string s2&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s2, pos2, len2&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;up to &lt;tt&gt;len2&lt;/tt&gt; characters from &lt;tt&gt;s2&lt;/tt&gt; starting at &lt;tt&gt;pos2&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;cp&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Null-terminated array pointed to by pointer &lt;tt&gt;cp&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;cp, len2&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;up to &lt;tt&gt;len2&lt;/tt&gt; characters from character array pointed to by &lt;tt&gt;cp&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;n, c&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;n&lt;/tt&gt; copies of character &lt;tt&gt;c&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;b2, e2&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Characters in the range formed by iterators &lt;tt&gt;b2&lt;/tt&gt; and &lt;tt&gt;e2&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;9.6.4. string Search Operations&lt;/p&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 9.19. &lt;tt&gt;string&lt;/tt&gt; Search Operations (Arguments in &lt;a class=&quot;docLink&quot; href=&quot;#ch09table20&quot;&gt;Table 9.20&lt;/a&gt;)&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;200&quot;&gt;&lt;br /&gt;&lt;col width=&quot;325&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.find(&lt;/tt&gt; &lt;span class=&quot;docEmphBoldItalic&quot;&gt;args&lt;/span&gt;&lt;tt&gt;)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Find first occurrence of &lt;span class=&quot;docEmphasis&quot;&gt;args&lt;/span&gt; in &lt;tt&gt;s&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.rfind(&lt;/tt&gt; &lt;span class=&quot;docEmphBoldItalic&quot;&gt;args&lt;/span&gt;&lt;tt&gt;)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Find last occurrence of &lt;span class=&quot;docEmphasis&quot;&gt;args&lt;/span&gt; in &lt;tt&gt;s&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.find_first_of(&lt;/tt&gt; &lt;span class=&quot;docEmphBoldItalic&quot;&gt;args&lt;/span&gt;&lt;tt&gt;)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Find first occurrence of any character from &lt;span class=&quot;docEmphasis&quot;&gt;args&lt;/span&gt; in &lt;tt&gt;s&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.find_last_of(&lt;/tt&gt; &lt;span class=&quot;docEmphBoldItalic&quot;&gt;args&lt;/span&gt;&lt;tt&gt;)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Find last occurrence of any character from &lt;span class=&quot;docEmphasis&quot;&gt;args&lt;/span&gt; in &lt;tt&gt;s&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.find_first_not_of(&lt;/tt&gt; &lt;span class=&quot;docEmphBoldItalic&quot;&gt;args&lt;/span&gt;&lt;tt&gt;)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Find first character in &lt;tt&gt;s&lt;/tt&gt; that is not in &lt;span class=&quot;docEmphasis&quot;&gt;args&lt;/span&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.find_last_not_of(&lt;/tt&gt; &lt;span class=&quot;docEmphBoldItalic&quot;&gt;args&lt;/span&gt;&lt;tt&gt;)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Find last character in &lt;tt&gt;s&lt;/tt&gt; that is not in &lt;span class=&quot;docEmphasis&quot;&gt;args&lt;/span&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 9.20. Arguments to &lt;tt&gt;string find&lt;/tt&gt; Operations&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;100&quot;&gt;&lt;br /&gt;&lt;col width=&quot;400&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;c, pos&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Look for the character &lt;tt&gt;c&lt;/tt&gt; starting at position &lt;tt&gt;pos&lt;/tt&gt; in &lt;tt&gt;s&lt;/tt&gt;. &lt;tt&gt;pos&lt;/tt&gt; defaults to 0.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s2, pos&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Look for the &lt;tt&gt;string s2&lt;/tt&gt; starting at position &lt;tt&gt;pos&lt;/tt&gt; in &lt;tt&gt;s&lt;/tt&gt;. &lt;tt&gt;pos&lt;/tt&gt; defaults to 0.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;cp, pos&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Look for the C-style null-terminated string pointed to by the pointer &lt;tt&gt;cp&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Start looking starting at position &lt;tt&gt;pos&lt;/tt&gt; in &lt;tt&gt;s&lt;/tt&gt;. &lt;tt&gt;pos&lt;/tt&gt; defaults to 0.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;cp, pos, n&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Look for the first &lt;tt&gt;n&lt;/tt&gt; characters in the array pointed to by the pointer &lt;tt&gt;cp&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Start looking starting at position &lt;tt&gt;pos&lt;/tt&gt; in &lt;tt&gt;s&lt;/tt&gt;. No default for &lt;tt&gt;pos&lt;/tt&gt; or &lt;tt&gt;n&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;Finding an Exact Match&lt;/p&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;By default, the find operations (and other &lt;tt&gt;string&lt;/tt&gt; operations that deal with characters) use the built-in operators to compare characters in the &lt;tt&gt;string&lt;/tt&gt;. As a result, these operations (and other &lt;tt&gt;string&lt;/tt&gt; operations) are case sensitive&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;The &lt;tt&gt;find&lt;/tt&gt; operations return a &lt;tt&gt;string::size_type&lt;/tt&gt;. Use an object of that type to hold the return from &lt;tt&gt;find&lt;/tt&gt;&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;It is essential that we increment &lt;tt&gt;pos&lt;/tt&gt;. Doing so ensures that we start looking for the next number at a point after the number we just found&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;&lt;font face=&quot;Courier New&quot;&gt;&lt;/font&gt;&lt;/strong&gt;&amp;nbsp;&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;Find Any Character&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;Specifying Where to Start the Search&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;Looking for a Nonmatch&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;Searching Backward&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;The find_last Functions&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;9.6.5. Comparing strings&lt;/p&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;the &lt;tt&gt;string&lt;/tt&gt; type defines all the relational operators so that we can compare two &lt;tt&gt;string&lt;/tt&gt;s for equality (&lt;tt&gt;==&lt;/tt&gt;), inequality (&lt;tt&gt;!=&lt;/tt&gt;), and the less- or greater-than operations (&lt;tt&gt;&amp;lt;, &amp;lt;=, &amp;gt;, &amp;gt;=&lt;/tt&gt;). Comparison between &lt;tt&gt;string&lt;/tt&gt;s is lexicographicalthat is, &lt;tt&gt;string&lt;/tt&gt; comparison is the same as a case-sensitive, dictionary ordering&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;The compare Functions&lt;/p&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;In addition to the relational operators, &lt;tt&gt;string&lt;/tt&gt; provides a set of &lt;tt&gt;compare&lt;/tt&gt; operations that perform lexicographical comparions. The results of these operations are similar to the C library &lt;tt&gt;strcmp&lt;/tt&gt; function&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;&lt;tt&gt;compare&lt;/tt&gt; returns one of three possible values: &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;A positive value if &lt;tt&gt;s1&lt;/tt&gt; is greater than the &lt;tt&gt;string&lt;/tt&gt; represented by args&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;A negative value if &lt;tt&gt;s1&lt;/tt&gt; is less than the &lt;tt&gt;string&lt;/tt&gt; represented by args&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;0 if &lt;tt&gt;s1&lt;/tt&gt; is equal to the &lt;tt&gt;string&lt;/tt&gt; represented by args&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 9.21. &lt;tt&gt;string compare&lt;/tt&gt; Operations&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;150&quot;&gt;&lt;br /&gt;&lt;col width=&quot;350&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.compare(s2)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Compare &lt;tt&gt;s&lt;/tt&gt; to &lt;tt&gt;s2&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; colspan=&quot;2&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.compare(pos1, n1, s2)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Compares &lt;tt&gt;n1&lt;/tt&gt; characters starting at &lt;tt&gt;pos1&lt;/tt&gt; from &lt;tt&gt;s&lt;/tt&gt; to &lt;tt&gt;s2&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; colspan=&quot;2&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.compare(pos1, n1, s2, pos2, n2)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Compares &lt;tt&gt;n1&lt;/tt&gt; characters starting at &lt;tt&gt;pos1&lt;/tt&gt; from &lt;tt&gt;s&lt;/tt&gt; to the &lt;tt&gt;n2&lt;/tt&gt; characters starting at &lt;tt&gt;pos2&lt;/tt&gt; in &lt;tt&gt;s2&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.compare(cp)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Compares &lt;tt&gt;s&lt;/tt&gt; to the null-terminated string pointed to by &lt;tt&gt;cp&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; colspan=&quot;2&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.compare(pos1, n1, cp)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Compares &lt;tt&gt;n1&lt;/tt&gt; characters starting at &lt;tt&gt;pos1&lt;/tt&gt; from &lt;tt&gt;s&lt;/tt&gt; to &lt;tt&gt;cp&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; colspan=&quot;2&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.compare(pos1, n1, cp, n2)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Compares &lt;tt&gt;n1&lt;/tt&gt; characters starting at &lt;tt&gt;pos1&lt;/tt&gt; from &lt;tt&gt;s&lt;/tt&gt; to &lt;tt&gt;n2&lt;/tt&gt; characters starting from the pointer &lt;tt&gt;cp&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;h4&gt;Section 9.7 Container Adaptor&lt;/h4&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;In addition to the sequential containers, the library provides three sequential container adaptors: &lt;tt&gt;queue&lt;/tt&gt;, &lt;a name=&quot;ch09term11&quot;&gt;&lt;/a&gt;&lt;a href=&quot;ch09lev1sec9.html#gloss09_11&quot;&gt;&lt;tt&gt;priority_queue&lt;/tt&gt;,&lt;/a&gt; and &lt;tt&gt;stack&lt;/tt&gt; &lt;br /&gt;&lt;li&gt;An &lt;b&gt;&lt;a name=&quot;ch09term1&quot;&gt;&lt;/a&gt;&lt;a href=&quot;ch09lev1sec9.html#gloss09_01&quot;&gt;adaptor&lt;/a&gt;&lt;/b&gt; is a general concept in the library. There are container, iterator, and function adaptors. Essentially, an adaptor is a mechanism for making one thing act like another. A container adaptor takes an existing container type and makes it act like a different abstract type &lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 9.22. Common Adaptor Operations and Types&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;150&quot;&gt;&lt;br /&gt;&lt;col width=&quot;350&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;size_type&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Type large enough to hold size of largest object of this type.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;value_type&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Element type.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;container_type&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Type of the underlying container on which the adaptor is implemented.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;A a;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Create a new empty adaptor named &lt;tt&gt;a&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;A a(c);&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Create a new adaptor named &lt;tt&gt;a&lt;/tt&gt; with a copy of the container &lt;tt&gt;c&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphasis&quot;&gt;Relational Operators&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Each adaptor supports all the relational operators: &lt;tt&gt;==, !=, &amp;lt;, &amp;lt;=, &amp;gt;, &amp;gt;=&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;li&gt;Initializing an Adapator &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Each adaptor defines two constructors: the default constructor that creates an empty object and a constructor that takes a container and makes a copy of that container as its underlying value&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Overriding the Underlying Container Type &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;By default both &lt;tt&gt;stack&lt;/tt&gt; and &lt;a name=&quot;ch09term12&quot;&gt;&lt;/a&gt;&lt;a href=&quot;ch09lev1sec9.html#gloss09_12&quot;&gt;&lt;tt&gt;queue&lt;/tt&gt;&lt;/a&gt; are implemented in terms of &lt;tt&gt;deque&lt;/tt&gt;, and a &lt;tt&gt;priority_queue&lt;/tt&gt; is implemented on a &lt;tt&gt;vector&lt;/tt&gt;. We can override the default container type by naming a sequential container as a second type argument when creating the adaptor &lt;br /&gt;&lt;li&gt;There are constraints on which containers can be used for a given adapator. We can use any of the sequential containers as the underlying container for a &lt;tt&gt;stack&lt;/tt&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Relational Operations on Adaptors &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Two adaptors of the same type can be compared for equality, inequality, less-than, greater-than, less-than-equal, and greater-than-equal relationships, provided that the underlying element type supports the equality and less-than operators&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;9.7.1. Stack Adaptor &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 9.23. Operations Supported by the Stack Container Adaptor&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;100&quot;&gt;&lt;br /&gt;&lt;col width=&quot;400&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.empty()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Returns &lt;tt&gt;TRue&lt;/tt&gt; if the &lt;tt&gt;stack&lt;/tt&gt; is empty; false otherwise.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.size()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Returns a count of the number of elements on the &lt;tt&gt;stack&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.pop()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Removes, but does not return, the top element from the &lt;tt&gt;stack&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.top()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Returns, but does not remove, the top element on the &lt;tt&gt;stack&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.push(item)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Places a new top element on the &lt;tt&gt;stack&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;li&gt;a&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;9.7.2. Queue and Priority Queue &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;The library &lt;tt&gt;queue&lt;/tt&gt; uses a first-in, first-out (FIFO) storage and retrieval policy. Objects entering the queue are placed in the back. The next object retrieved is taken from the front of the queue. There are two kinds of queues: the FIFO queue, which we will speak of simply as a &lt;tt&gt;queue&lt;/tt&gt;, and a priority queue &lt;br /&gt;&lt;li&gt;A &lt;tt&gt;priority_queue&lt;/tt&gt; lets us establish a priority among the elements held in the queue. Rather than place a newly entered item at the back of the queue, the item is placed ahead of all those items with a lower priority. By default, the library uses the &lt;tt&gt;&amp;lt;&lt;/tt&gt; operator on the element type to determine relative priorities &lt;br /&gt;&lt;li&gt;To use either &lt;tt&gt;queue&lt;/tt&gt; or &lt;tt&gt;priority_queue&lt;/tt&gt;, we must include the &lt;tt&gt;queue&lt;/tt&gt; header &lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 9.24. Operations Supported by Queues and Priority Queues&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;100&quot;&gt;&lt;br /&gt;&lt;col width=&quot;400&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;q.empty()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Returns &lt;tt&gt;TRue&lt;/tt&gt; if the &lt;tt&gt;queue&lt;/tt&gt; is empty; false otherwise.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;q.size()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Returns a count of the number of elements on the &lt;tt&gt;queue&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;q.pop()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Removes, but does not return, the front element from the &lt;tt&gt;queue&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;q.front()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Returns, but does not remove, the front element on the &lt;tt&gt;queue&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;This operation can be applied only to a &lt;tt&gt;queue&lt;/tt&gt;&lt;/span&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;q.back()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Returns, but does not remove, the back element on the &lt;tt&gt;queue&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;This operation can be applied only to a &lt;tt&gt;queue&lt;/tt&gt;&lt;/span&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;q.top()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Returns, but does not remove, the highest-priority element.&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;This operation can be applied only to a &lt;tt&gt;priority_queue&lt;/tt&gt;.&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;q.push(item)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Places a new element at the end of the &lt;tt&gt;queue&lt;/tt&gt; or at its appropriate position based on priority in a &lt;tt&gt;priority_queue&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;a&lt;/li&gt;&lt;/ol&gt;  </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/2249632846686747551/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/2249632846686747551?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/2249632846686747551'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/2249632846686747551'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2011/01/c-primer-fourth-edition-notes-chapter-9.html' title='C++ Primer, Fourth Edition Notes Chapter 9 Sequential Containers'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-6871234133946851659</id><published>2011-01-17T01:26:00.001-08:00</published><updated>2011-01-17T01:26:50.047-08:00</updated><title type='text'>C++ Primer Answer book for 4rd edition Exercise 8-3</title><content type='html'>// Exercise 8.3.cpp : Defines the entry point for the console application.&lt;br&gt;//&lt;br&gt;&lt;br&gt;#include &amp;quot;stdafx.h&amp;quot;&lt;br&gt;#include &amp;lt;iostream&amp;gt;&lt;br&gt;#include &amp;lt;fstream&amp;gt;&lt;br&gt;#include &amp;lt;string&amp;gt;&lt;br&gt;&lt;br&gt;using namespace std;&lt;br&gt;ifstream &amp;amp;printOut( ifstream  &amp;amp;in);&lt;br&gt;istream &amp;amp;print( istream  &amp;amp;in);&lt;br&gt;int main(int argc, _TCHAR* argv[])&lt;br&gt;{&lt;br&gt;	ifstream in(&amp;quot;ReadMe.txt&amp;quot;);	&lt;br&gt;&lt;br&gt;	//ifstream &amp;amp;inf = printOut(in);&lt;br&gt;&lt;br&gt;	//if(inf)&lt;br&gt;	//{&lt;br&gt;	//	cout &amp;lt;&amp;lt; &amp;quot;Testing the stream input;&amp;quot; &amp;lt;&amp;lt; endl;&lt;br&gt;	//	string s;&lt;br&gt;	//	//Issue here: already clear the stream, but the stream still can&amp;#39;t be reused&lt;br&gt;	//	while(inf &amp;gt;&amp;gt; s , !inf.eof())&lt;br&gt;	//	{&lt;br&gt;	//		cout &amp;lt;&amp;lt; s &amp;lt;&amp;lt; endl;&lt;br&gt;	//	}&lt;br&gt;	//	inf.close();&lt;br&gt;	//}&lt;br&gt;	&lt;br&gt;	ifstream inf1(&amp;quot;ReadMe.txt&amp;quot;);&lt;br&gt;	print( inf1);&lt;br&gt;	&lt;br&gt;&lt;br&gt;	print(cin);&lt;br&gt;	// test the cleared eofbit mark&lt;br&gt;	if(cin.eof())&lt;br&gt;	{&lt;br&gt;		//why it always show it is not cleared here?&lt;br&gt;		cout &amp;lt;&amp;lt; &amp;quot;the eof mark is not cleared!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br&gt;	}&lt;br&gt;	else&lt;br&gt;		cout &amp;lt;&amp;lt; &amp;quot;the eof mark is cleared!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br&gt;	int i(0);&lt;br&gt;	cin &amp;gt;&amp;gt; i;&lt;br&gt;	return 0;&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;br&gt;ifstream &amp;amp;printOut( ifstream  &amp;amp;in)&lt;br&gt;{&lt;br&gt;	string s;&lt;br&gt;	while(in &amp;gt;&amp;gt; s , !in.eof())&lt;br&gt;	{&lt;br&gt;		//print out all to standard output&lt;br&gt;		cout &amp;lt;&amp;lt; s &amp;lt;&amp;lt; endl;&lt;br&gt;	}&lt;br&gt;	//reset the stream&lt;br&gt;	in.clear( istream::eofbit);&lt;br&gt;	//in.setstate( istream::eofbit);&lt;br&gt;&lt;br&gt;	return in;&lt;br&gt;}&lt;br&gt;istream &amp;amp;print( istream  &amp;amp;in)&lt;br&gt;{&lt;br&gt;	string s;&lt;br&gt;	while(in &amp;gt;&amp;gt; s , !in.eof())&lt;br&gt;	{&lt;br&gt;		//print out all to standard output&lt;br&gt;		cout &amp;lt;&amp;lt; s &amp;lt;&amp;lt; endl;&lt;br&gt;	}&lt;br&gt;	if(in.eof())&lt;br&gt;	{&lt;br&gt;		cout &amp;lt;&amp;lt; &amp;quot;The end of file!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br&gt;	}&lt;br&gt;	// clear the eofbit mark&lt;br&gt;	in.clear( istream::eofbit);&lt;br&gt;&lt;br&gt;	return in;&lt;br&gt;}&lt;br clear=&quot;all&quot;&gt;&lt;font color=&quot;#000066&quot;&gt;Angelo Yin&lt;/font&gt;&lt;div&gt;&lt;font color=&quot;#000066&quot;&gt;&lt;br&gt;Keep moving forward!&lt;/font&gt;&lt;/div&gt;&lt;br&gt; </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/6871234133946851659/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/6871234133946851659?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/6871234133946851659'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/6871234133946851659'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2011/01/c-primer-answer-book-for-4rd-edition.html' title='C++ Primer Answer book for 4rd edition Exercise 8-3'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-2071353725599111691</id><published>2011-01-17T01:09:00.001-08:00</published><updated>2011-01-17T01:15:35.089-08:00</updated><title type='text'>C++ Primer Answer book for 4rd edition-Exercise 8.16</title><content type='html'>// Exercise 8.16.cpp : Defines the entry point for the console application. &lt;br /&gt;
// &lt;br /&gt;
&lt;br /&gt;
#include &quot;stdafx.h&quot; &lt;br /&gt;
#include &amp;lt;string&amp;gt; &lt;br /&gt;
#include &amp;lt;vector&amp;gt; &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
#include &amp;lt;sstream&amp;gt; &lt;br /&gt;
&lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int _tmain(int argc, _TCHAR* argv[]) &lt;br /&gt;
{ &lt;br /&gt;
return 0; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #000066;&quot;&gt;Angelo Yin&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000066;&quot;&gt;&lt;br /&gt;
Keep moving forward!&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/2071353725599111691/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/2071353725599111691?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/2071353725599111691'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/2071353725599111691'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2011/01/exercise-8.html' title='C++ Primer Answer book for 4rd edition-Exercise 8.16'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8409310322589078134.post-1556364777770129748</id><published>2011-01-10T21:55:00.003-08:00</published><updated>2011-01-10T21:55:40.564-08:00</updated><title type='text'>C++ Primer, Fourth Edition Notes Chapter 8. The IO Library</title><content type='html'>&lt;h4&gt;Section 8.1 An Object-Oriented Library&lt;/h4&gt; &lt;ol&gt; &lt;li&gt;Conceptually, neither the kind of device nor the character size affect the IO operations we want to perform.  &lt;li&gt;To manage the complexity, the library uses &lt;b&gt;&lt;a name=&quot;ch08term6&quot;&gt;&lt;/a&gt;&lt;a href=&quot;ch08lev1sec7.html#gloss08_06&quot;&gt;inheritance&lt;/a&gt;&lt;/b&gt; to define a set &lt;a name=&quot;idd1e57839&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e57844&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e57851&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e57858&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e57863&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e57873&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e57879&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e57885&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e57891&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e57897&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e57904&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e57911&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e57915&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e57921&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e57925&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e57932&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e57935&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e57942&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e57946&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e57953&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e57958&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e57964&quot;&gt;&lt;/a&gt;of &lt;b&gt;&lt;a name=&quot;ch08term7&quot;&gt;&lt;/a&gt;&lt;a href=&quot;ch08lev1sec7.html#gloss08_07&quot;&gt;object-oriented&lt;/a&gt;&lt;/b&gt; classes.  &lt;li&gt;&lt;strong&gt;The IO types are defined in three separate headers: &lt;tt&gt;iostream&lt;/tt&gt; defines the types used to read and write to a console window, &lt;/strong&gt;&lt;a name=&quot;ch08term5&quot;&gt;&lt;/a&gt;&lt;a href=&quot;ch08lev1sec7.html#gloss08_05&quot;&gt;&lt;tt&gt;&lt;strong&gt;fstream&lt;/strong&gt;&lt;/tt&gt;&lt;/a&gt;&lt;strong&gt; defines the types used to read and write named files, and &lt;tt&gt;sstream&lt;/tt&gt; defines the types used to read and write in-memory &lt;tt&gt;string&lt;/tt&gt;s&lt;/strong&gt;  &lt;li&gt;&lt;strong&gt;Each of the types in &lt;tt&gt;fstream&lt;/tt&gt; and &lt;tt&gt;sstream&lt;/tt&gt; is derived from a corresponding type defined in the &lt;tt&gt;iostream&lt;/tt&gt; header&lt;/strong&gt;  &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td valign=&quot;top&quot;&gt; &lt;p&gt; &lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt; &lt;caption&gt; &lt;h5 class=&quot;docTableTitle&quot;&gt;Table 8.1. IO Library Types and Headers&lt;/h5&gt;&lt;/caption&gt; &lt;colgroup&gt; &lt;col width=&quot;50&quot;&gt; &lt;col width=&quot;475&quot;&gt;&lt;/colgroup&gt; &lt;thead&gt; &lt;tr&gt; &lt;th class=&quot;thead&quot; valign=&quot;top&quot; width=&quot;93&quot; scope=&quot;col&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Header&lt;/p&gt;&lt;/th&gt; &lt;th class=&quot;thead&quot; valign=&quot;top&quot; width=&quot;459&quot; scope=&quot;col&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Type&lt;/p&gt;&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;93&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;iostream&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;459&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;istream&lt;/tt&gt; reads from a stream&lt;/p&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;ostream&lt;/tt&gt; writes to a stream&lt;/p&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;iostream&lt;/tt&gt; reads and writes a stream; derived from &lt;tt&gt;istream&lt;/tt&gt; and &lt;tt&gt;ostream&lt;/tt&gt;,&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;93&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;fstream&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;459&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;ifstream&lt;/tt&gt;, reads from a file; derived from &lt;tt&gt;istream&lt;/tt&gt;&lt;/p&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;ofstream&lt;/tt&gt; writes to a file; derived from &lt;tt&gt;ostream&lt;/tt&gt;&lt;/p&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;fstream&lt;/tt&gt;, reads and writes a file; derived from &lt;tt&gt;iostream&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;93&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;sstream&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; width=&quot;459&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;istringstream&lt;/tt&gt; reads from a &lt;tt&gt;string&lt;/tt&gt;; derived from &lt;tt&gt;istream&lt;/tt&gt;&lt;/p&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;ostringstream&lt;/tt&gt; writes to a &lt;tt&gt;string&lt;/tt&gt;; derived from &lt;tt&gt;ostream&lt;/tt&gt;&lt;/p&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;stringstream&lt;/tt&gt; reads and writes a &lt;tt&gt;string&lt;/tt&gt;; derived from &lt;tt&gt;iostream&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjU6uVZrFxdSfB4OwKjOxvKlZVPn8S-GkMreklMFocrOhX1VNPpFyGPCAWe0Rbf1U9rUsb35kZo8srI6BI8Pkf4RAMC1uMvBxd7k-wPMdySJgQBJHAyzFcItnJeIAAYJOjs4OLkpJJLdCU/s1600-h/image3.png&quot;&gt;&lt;img style=&quot;border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px&quot; title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLkGuIMhCCXUtWqvA2ZlbTB4rahXqC6GcRl98lVeZvGCXkrLHIrcaZkX02DiC6HfxyGNYn9gR3GViNlAvIQq2FVAbZICKGFQH9bYr_Rox9FNf-feKi9m_VlWvGRYz3-YZ4qTMF_MXEVuQ/?imgmax=800&quot; width=&quot;504&quot; height=&quot;306&quot;&gt;&lt;/a&gt; &lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt; &lt;li&gt;Using inheritance for the IO types has another important implication: As we&#39;ll see in &lt;a href=&quot;ch15.html#ch15&quot;&gt;Chapter 15&lt;/a&gt;, when we have a function that takes a reference to a base-class type, we can pass an object of a derived type to that function  &lt;li&gt;International Character Support  &lt;ol&gt; &lt;li&gt;The stream classes described thus far read and write streams composed of type &lt;tt&gt;char&lt;/tt&gt;. The library defines a corresponding set of types supporting the &lt;tt&gt;wchar_t&lt;/tt&gt; type. Each class is distinguished from its &lt;tt&gt;char&lt;/tt&gt; counterpart by a &quot;&lt;tt&gt;w&lt;/tt&gt;&quot; prefix. Thus, the types &lt;tt&gt;wostream, wistream&lt;/tt&gt;, and &lt;tt&gt;wiostream&lt;/tt&gt; read and write &lt;tt&gt;wchar_t&lt;/tt&gt; data to or from a console window. The file input and output classes are &lt;tt&gt;wifstream, wofstream&lt;/tt&gt;, and &lt;tt&gt;wfstream&lt;/tt&gt;. The &lt;tt&gt;wchar_t&lt;/tt&gt; versions of &lt;tt&gt;string&lt;/tt&gt; stream input and output are &lt;tt&gt;wistringstream, wostringstream&lt;/tt&gt;, and &lt;tt&gt;wstringstream&lt;/tt&gt;. The library also defines objects to read and write wide characters from the standard input and standard output. These objects are distinguished from the &lt;tt&gt;char&lt;/tt&gt; counterparts by a &quot;&lt;tt&gt;w&lt;/tt&gt;&quot; prefix: The &lt;tt&gt;wchar_t&lt;/tt&gt; standard input object is named &lt;tt&gt;wcin;&lt;/tt&gt; standard output is &lt;tt&gt;wcout&lt;/tt&gt;; and standard error is &lt;tt&gt;wcerr&lt;/tt&gt;  &lt;li&gt;Each of the IO headers defines both the char and wchar_t classes and standard input/output objects. The stream-based wchar_t classes and objects are defined in iostream, the wide character file stream types in fstream, and the wide character stringstreams in sstream.&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;No Copy or Assign for IO Objects  &lt;ol&gt; &lt;li&gt;the library types do not allow allow copy or assignment:  &lt;li&gt;&lt;strong&gt;This requirement has two particularly important implications. As we&#39;ll see in &lt;/strong&gt;&lt;a href=&quot;ch09.html#ch09&quot;&gt;&lt;strong&gt;Chapter 9&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;, only element types that support copy can be stored in &lt;tt&gt;vector&lt;/tt&gt;s or other container types. Because we cannot copy stream objects, we cannot have a &lt;tt&gt;vector&lt;/tt&gt; (or other container) that holds stream objects&lt;/strong&gt;  &lt;li&gt;&lt;strong&gt;The second implication is that we cannot have a parameter or return type that is one of the stream types. If we need to pass or return an IO object, it must be passed or returned as a pointer or reference&lt;/strong&gt;  &lt;li&gt;&lt;strong&gt;Typically, we pass a stream as a nonconst reference because we pass an IO object intending to read from it or write to it. Reading or writing an IO object changes its state, so the reference must be nonconst.&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;&lt;/li&gt;&lt;/ol&gt; &lt;h4&gt;Section 8.2 Condition States&lt;/h4&gt; &lt;ol&gt; &lt;li&gt;Keep in mind that the material we cover in this section and the next applies equally to plain streams, file streams, or string streams  &lt;li&gt;The IO library manages a set of &lt;b&gt;&lt;a name=&quot;ch08term2&quot;&gt;&lt;/a&gt;&lt;a href=&quot;ch08lev1sec7.html#gloss08_02&quot;&gt;condition state&lt;/a&gt;&lt;/b&gt; members that indicate whether a given IO object is in a usable state or has encountered a particular kind &lt;a name=&quot;idd1e58607&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e58612&quot;&gt;&lt;/a&gt;of error. The library also defines a set of functions and flags, listed in &lt;a href=&quot;#ch08table02&quot;&gt;Table 8.2&lt;/a&gt;, that give us access to and let us manipulate the state of each stream  &lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td valign=&quot;top&quot;&gt; &lt;p&gt; &lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt; &lt;caption&gt; &lt;h5 class=&quot;docTableTitle&quot;&gt;Table 8.2. IO Library Condition State&lt;/h5&gt;&lt;/caption&gt; &lt;colgroup&gt; &lt;col width=&quot;150&quot;&gt; &lt;col width=&quot;350&quot;&gt;&lt;/colgroup&gt; &lt;thead&gt;&lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphBoldItalic&quot;&gt;strm&lt;/span&gt;&lt;tt&gt;::iostate&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Name of the machine-dependent integral type, defined by each &lt;tt&gt;iostream&lt;/tt&gt; class that is used to define the condition states.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphBoldItalic&quot;&gt;strm&lt;/span&gt;&lt;tt&gt;::badbit&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphasis&quot;&gt;strm&lt;/span&gt;&lt;tt&gt;::iostate&lt;/tt&gt; value used to indicate that a stream is corrupted.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphBoldItalic&quot;&gt;strm&lt;/span&gt;&lt;tt&gt;::failbit&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphasis&quot;&gt;strm&lt;/span&gt;&lt;tt&gt;::iostate&lt;/tt&gt; value used to indicate that an IO operation failed.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphBoldItalic&quot;&gt;strm&lt;/span&gt;&lt;tt&gt;::eofbit&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphasis&quot;&gt;strm&lt;/span&gt;&lt;tt&gt;::iostate&lt;/tt&gt; value used to indicate the a stream hit end-of-file.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.eof()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;true&lt;/tt&gt; if &lt;tt&gt;eofbit&lt;/tt&gt; in the stream &lt;tt&gt;s&lt;/tt&gt; is set.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.fail()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;true&lt;/tt&gt; if &lt;tt&gt;failbit&lt;/tt&gt; in the stream &lt;tt&gt;s&lt;/tt&gt; is set.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.bad()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;TRue&lt;/tt&gt; if &lt;tt&gt;badbit&lt;/tt&gt; in the stream &lt;tt&gt;s&lt;/tt&gt; is set.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.good()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;true&lt;/tt&gt; if the stream &lt;tt&gt;s&lt;/tt&gt; is in a valid state.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.clear()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Reset all condition values in the stream &lt;tt&gt;s&lt;/tt&gt; to valid state.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.clear(flag)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Set specified condition state(s) in &lt;tt&gt;s&lt;/tt&gt; to valid. Type of &lt;tt&gt;flag&lt;/tt&gt; is &lt;span class=&quot;docEmphasis&quot;&gt;strm&lt;/span&gt;&lt;tt&gt;::iostate&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.setstate(flag)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Add specified condition to &lt;tt&gt;s&lt;/tt&gt;. Type of &lt;tt&gt;flag&lt;/tt&gt; is &lt;span class=&quot;docEmphasis&quot;&gt;strm&lt;/span&gt;&lt;tt&gt;::iostate&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;s.rdstate()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt; &lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;p class=&quot;docText&quot;&gt;Returns current condition of &lt;tt&gt;s&lt;/tt&gt; as an &lt;span class=&quot;docEmphasis&quot;&gt;strm&lt;/span&gt;&lt;tt&gt;::iostate&lt;/tt&gt; value.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;a  &lt;li&gt;To be used for input or output, a stream must be in a non-error state. The easiest way to test whether a stream is okay is to test its truth value  &lt;ol&gt; &lt;li&gt;&lt;pre&gt;          &lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt; (cin)&lt;br /&gt;               &lt;span style=&quot;color: #008000&quot;&gt;// ok to use cin, it is in a valid state&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;          &lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt; (cin &amp;gt;&amp;gt; word)&lt;br /&gt;               &lt;span style=&quot;color: #008000&quot;&gt;// ok: read operation successful ...&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Condition States &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Each stream object contains a condition state member that is managed through the setstate and clear operations. This state member has type iostate, which is a machine-dependent integral type defined by each iostream class.&lt;strong&gt;&amp;nbsp; &lt;/strong&gt;&lt;br /&gt;&lt;li&gt;Each IO class also defines three const values of type iostate that represent particular bit patterns. These const values are used to indicate particular kinds of IO conditions. They can be used with the bitwise operators (Section 5.3, p. 154) to test or set multiple flags in one operation. &lt;br /&gt;&lt;li&gt;&lt;strong&gt;The &lt;tt&gt;badbit&lt;/tt&gt; indicates a system level failure, such as an unrecoverable read or write error. It is usually not possible to continue using a stream after such an error. &lt;/strong&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;The &lt;tt&gt;failbit&lt;/tt&gt; is set after a recoverable error, such as reading a character when numeric data was expected. It is often possible to correct the problem that caused the &lt;tt&gt;failbit&lt;/tt&gt; to be set. &lt;/strong&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;The &lt;tt&gt;eofbit&lt;/tt&gt; is set when an end-of-file is encountered. Hitting end-of-file also sets the &lt;tt&gt;failbit&lt;/tt&gt;&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;If any of bad, fail, or eof are true, then testing the stream itself will indicate that the stream is in an error state. Similarly, the good operation returns TRue if none of the other conditions is true. &lt;br /&gt;&lt;li&gt;The clear and setstate operations change the state of the condition member. The clear operations put the condition back in its valid state. They are called after we have remedied whatever problem occurred and we want to reset the stream to its valid state. The setstate operation turns on the specified condition to indicate that a problem occurred. setstate leaves the existing state variables unchanged except that it adds the additional indicated state(s). &lt;br /&gt;&lt;li&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Interrogating and Controlling the State of a Stream &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;Recall that the comma operator executes by evaluating each operand and returns its rightmost operand as &lt;/strong&gt;&lt;a name=&quot;idd1e59160&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59167&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59172&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59180&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59185&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59189&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59195&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59202&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59208&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59216&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59222&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59226&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59230&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59235&quot;&gt;&lt;/a&gt;&lt;strong&gt;its result.&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Accessing the Condition State &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;The rdstate member function returns an iostate value that corresponds to the entire current condition state of the stream&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Dealing with Multiple States &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Alternatively, we could use the bitwise OR (&lt;a href=&quot;ch05lev1sec3.html#ch05lev1sec3&quot;&gt;Section 5.3&lt;/a&gt;, p. &lt;a href=&quot;ch05lev1sec3.html#ch05lev1sec3&quot;&gt;154&lt;/a&gt;) operator to generate a value to pass two or more state bits in a single call. The bitwise OR generates an integral value using the bit patterns of its operands. For each bit in the result, the bit is 1 if the corresponding bit is 1 in either of its operands&lt;pre&gt;&lt;span style=&quot;color: #008000&quot;&gt;// sets both the badbit and the failbit&lt;/span&gt;&lt;br /&gt;   is.setstate(ifstream::badbit | ifstream::failbit);&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;tells the object &lt;tt&gt;is&lt;/tt&gt; to turn on both the &lt;tt&gt;failbit&lt;/tt&gt; and the &lt;tt&gt;badbit&lt;/tt&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;h4&gt;Section 8.3 Managing the Output Buffer&lt;/h4&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Each IO object manages a buffer, which is used to hold the data that the program reads and writes &lt;br /&gt;&lt;li&gt;There are several conditions that cause the buffer to be flushedthat is, writtento the actual output device or file &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;The program completes normally. All output buffers are emptied as part of the return from main.&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;At some indeterminate time, the buffer can become full, in which case it will be flushed before writing the next value&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;We can flush the buffer explicitly using a manipulator (&lt;/strong&gt;&lt;a href=&quot;ch01lev1sec2.html#ch01lev2sec3&quot;&gt;&lt;strong&gt;Section 1.2.2&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;, p. &lt;/strong&gt;&lt;a href=&quot;ch01lev1sec2.html#ch01lev2sec3&quot;&gt;&lt;strong&gt;7&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;) such as &lt;tt&gt;endl&lt;/tt&gt;&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;We can use the &lt;tt&gt;unitbuf&lt;/tt&gt; manipulator to set the stream&#39;s internal state to empty the buffer after each output operation&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;We can &lt;tt&gt;tie&lt;/tt&gt; the output stream to an input stream, in which case the output buffer is flushed whenever the associated input stream is read&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Flushing the Output Buffer &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Our programs have already used the &lt;tt&gt;endl&lt;/tt&gt; manipulator, which writes a newline and flushes the buffer. There are two other similar manipulators. The first, &lt;tt&gt;flush&lt;/tt&gt;, is used quite frequently. It flushes the stream but adds no characters to the output. The second, &lt;tt&gt;ends&lt;/tt&gt;, is used much less often. It inserts a null character into the buffer and then flushes it&lt;pre&gt;    cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;hi!&lt;/span&gt;&quot; &amp;lt;&amp;lt; flush;      &lt;span style=&quot;color: #008000&quot;&gt;// flushes the buffer; adds no data&lt;/span&gt;&lt;br /&gt;    cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;hi!&lt;/span&gt;&quot; &amp;lt;&amp;lt; ends;       &lt;span style=&quot;color: #008000&quot;&gt;// inserts a null, then flushes the buffer&lt;/span&gt;&lt;br /&gt;    cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;hi!&lt;/span&gt;&quot; &amp;lt;&amp;lt; endl;       &lt;span style=&quot;color: #008000&quot;&gt;// inserts a newline, then flushes the buffer&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;The unitbuf Manipulator &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;If we want to flush every output, it is better to use the &lt;tt&gt;unitbuf&lt;/tt&gt; manipulator. This manipulator flushes the stream after every write &lt;br /&gt;&lt;li&gt;The &lt;tt&gt;nounitbuf&lt;/tt&gt; manipulator restores the stream to use normal, system-managed buffer flushing &lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table border=&quot;1&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot; width=&quot;90%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td&gt;&lt;br /&gt;&lt;h2 class=&quot;docSidebarTitle&quot;&gt;Caution: Buffers Are Not Flushed if the Program Crashes&lt;/h2&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;a name=&quot;idd1e59657&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59660&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59668&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59676&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59682&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59690&quot;&gt;&lt;/a&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;Output buffers are &lt;span class=&quot;docEmphasis&quot;&gt;not&lt;/span&gt; flushed if the program terminates abnormally. When attempting to debug a program that has crashed, we often use the last output to help isolate the region of program in which the bug might occur. If the crash is after a particular print statement, then we know that the crash happened after that point in the program.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;When debugging a program, it is essential to make sure that any output you &lt;span class=&quot;docEmphasis&quot;&gt;think&lt;/span&gt; should have been written was actually flushed. Because the system does not automatically flush the buffers when the program crashes, it is likely that there is output that the program wrote but that has not shown up on the standard output. It is still sitting in an output buffer waiting to be printed.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;If you use the last output to help locate the bug, you need to be certain that all the output really did get printed. Making sure that all output operations include an explicit &lt;tt&gt;flush&lt;/tt&gt; or call to &lt;tt&gt;endl&lt;/tt&gt; is the best way to ensure that you are seeing all the output that the program actually processed.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;Countless hours of programmer time have been wasted tracking through code that appeared not to have executed when in fact the buffer simply had not been flushed. For this reason, we tend to use &lt;tt&gt;endl&lt;/tt&gt; rather than &lt;tt&gt;\n&lt;/tt&gt; when writing output. Using &lt;tt&gt;endl&lt;/tt&gt; means we do not have to wonder whether output is pending when a program crashes.&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Tying Input and Output Streams Together &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;When an input stream is tied to an output stream, any attempt to read the input stream will first flush the buffer associated output stream. The library ties &lt;tt&gt;cout&lt;/tt&gt; to &lt;tt&gt;cin&lt;/tt&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;Interactive systems usually should be sure that their input and output streams are tied. Doing so means that we are guaranteed that any output, which might include prompts to the user, has been written before attempting to read&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;The &lt;tt&gt;tie&lt;/tt&gt; function can be called on either &lt;tt&gt;istream&lt;/tt&gt; or an &lt;tt&gt;ostream&lt;/tt&gt;. It takes a pointer to an &lt;tt&gt;ostream&lt;/tt&gt; and ties the argument stream to the object on which &lt;tt&gt;tie&lt;/tt&gt; was called. When a stream ties itself to an &lt;tt&gt;ostream&lt;/tt&gt;, then any IO operation on the stream that called &lt;tt&gt;tie&lt;/tt&gt; flushes the buffer associated with the argument it passed to &lt;tt&gt;tie&lt;/tt&gt;&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;An ostream object can be tied to only one istream object at a time. To break an existing tie, we pass in an argument of 0.&lt;/li&gt;&lt;/ol&gt;&lt;pre&gt;&lt;span style=&quot;color: #008000&quot;&gt;// EXE-8.3.cpp : Defines the entry point for the console application.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #008000&quot;&gt;//&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;#include &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;stdafx.h&lt;/span&gt;&quot;&lt;br /&gt;&lt;br /&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;#include &amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;namespace&lt;/span&gt; std;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; main(&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; argc, _TCHAR* argv[])&lt;br /&gt;{&lt;br /&gt;	&lt;br /&gt;	cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;hi!&lt;/span&gt;&quot; &amp;lt;&amp;lt; ends;       &lt;span style=&quot;color: #008000&quot;&gt;// inserts a null, then flushes the buffer&lt;/span&gt;&lt;br /&gt;	cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;hi!&lt;/span&gt;&quot; &amp;lt;&amp;lt; flush;      &lt;span style=&quot;color: #008000&quot;&gt;// flushes the buffer; adds no data&lt;/span&gt;&lt;br /&gt;	cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;hi!&lt;/span&gt;&quot; &amp;lt;&amp;lt; endl;       &lt;span style=&quot;color: #008000&quot;&gt;// inserts a newline, then flushes the buffer&lt;/span&gt;&lt;br /&gt;	&lt;br /&gt;	cout &amp;lt;&amp;lt; unitbuf &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;first&lt;/span&gt;&quot; &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt; second&lt;/span&gt;&quot; &amp;lt;&amp;lt; nounitbuf &amp;lt;&amp;lt;endl;&lt;br /&gt;	cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;first&lt;/span&gt;&quot; &amp;lt;&amp;lt; flush &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt; second&lt;/span&gt;&quot; &amp;lt;&amp;lt; flush;&lt;br /&gt;&lt;br /&gt;	cout &amp;lt;&amp;lt; endl;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; tieTest;&lt;br /&gt;	cin.tie(&amp;amp;cout);   &lt;span style=&quot;color: #008000&quot;&gt;// illustration only: the library ties cin and cout for us&lt;/span&gt;&lt;br /&gt;	ostream *old_tie = cin.tie();&lt;br /&gt;	cin.tie(0); &lt;span style=&quot;color: #008000&quot;&gt;// break tie to cout, cout no longer flushed when cin is read&lt;/span&gt;&lt;br /&gt;	cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;test&lt;/span&gt;&quot;;&lt;br /&gt;	cin &amp;gt;&amp;gt; tieTest ;&lt;br /&gt;	cin.tie(&amp;amp;cerr);   &lt;span style=&quot;color: #008000&quot;&gt;// ties cin and cerr, not necessarily a good idea!&lt;/span&gt;&lt;br /&gt;	&lt;span style=&quot;color: #008000&quot;&gt;// ...&lt;/span&gt;&lt;br /&gt;	cin.tie(0);       &lt;span style=&quot;color: #008000&quot;&gt;// break tie between cin and cerr&lt;/span&gt;&lt;br /&gt;	cin.tie(old_tie); &lt;span style=&quot;color: #008000&quot;&gt;// restablish normal tie between cin and cout&lt;/span&gt;&lt;br /&gt;	&lt;br /&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; i;&lt;br /&gt;	cin &amp;gt;&amp;gt; i;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;h4&gt;Section 8.4 File Input and Output&lt;/h4&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;a name=&quot;idd1e59887&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59891&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59897&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59903&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59910&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59916&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59920&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59926&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59933&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59937&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59943&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e59950&quot;&gt;&lt;/a&gt;The &lt;tt&gt;fstream&lt;/tt&gt; header defines three types to support file IO: &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;&lt;tt&gt;ifstream&lt;/tt&gt;, derived from &lt;tt&gt;istream&lt;/tt&gt;, reads from a file.&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;&lt;tt&gt;ofstream&lt;/tt&gt;, derived from &lt;tt&gt;ostream&lt;/tt&gt;, writes to a file.&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;&lt;tt&gt;fstream&lt;/tt&gt;, derived from &lt;tt&gt;iostream&lt;/tt&gt;, reads and writes the same file.&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;In addition to the behavior that &lt;tt&gt;fstream&lt;/tt&gt; types inherit, they also define two new operations of their own&lt;tt&gt;open&lt;/tt&gt; and &lt;tt&gt;close&lt;/tt&gt;along with a constructor that takes the name of a file to open. These operations can be called on objects of &lt;tt&gt;fstream, ifstream&lt;/tt&gt;, or &lt;tt&gt;ofstream&lt;/tt&gt; but not on the other IO types&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;8.4.1. Using File Stream Objects &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;When we want to read or write a file, we must define our own objects, and bind them to the desired files &lt;br /&gt;&lt;li&gt;Supplying a file name as an initializer to an &lt;tt&gt;ifstream&lt;/tt&gt; or &lt;tt&gt;ofstream&lt;/tt&gt; object has the effect of opening the specified file &lt;br /&gt;&lt;li&gt;Before we use an &lt;tt&gt;fstream&lt;/tt&gt; object, we must also bind it to a file to read or write &lt;br /&gt;&lt;li&gt;We bind an existing &lt;tt&gt;fstream&lt;/tt&gt; object to the specified file by calling the &lt;tt&gt;open&lt;/tt&gt; member. The &lt;tt&gt;open&lt;/tt&gt; function does whatever system-specific operations are required to locate the given file and open it for reading or writing as appropriate &lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table border=&quot;1&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot; width=&quot;90%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td&gt;&lt;br /&gt;&lt;h2 class=&quot;docSidebarTitle&quot;&gt;Caution: File Names in C++&lt;/h2&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;a name=&quot;idd1e60150&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e60156&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e60160&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e60167&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e60174&quot;&gt;&lt;/a&gt;&lt;a name=&quot;idd1e60181&quot;&gt;&lt;/a&gt;&lt;span class=&quot;docEmphStrong&quot;&gt;For historical reasons, the IO library uses C-style character strings (&lt;a class=&quot;docLink&quot; href=&quot;ch04lev1sec3.html#ch04lev1sec3&quot;&gt;Section 4.3&lt;/a&gt;, p. &lt;a class=&quot;docLink&quot; href=&quot;ch04lev1sec3.html#ch04lev1sec3&quot;&gt;130&lt;/a&gt;) rather than C++ &lt;tt&gt;strings&lt;/tt&gt; to refer to file names. When we call &lt;tt&gt;open&lt;/tt&gt; or use a file name as the initializer when creating an &lt;tt&gt;fstream&lt;/tt&gt; object, the argument we pass is a C-style string, not a library &lt;tt&gt;string&lt;/tt&gt;. Often our programs obtain file names by reading the standard input. As usual, it is a good idea to read into a &lt;tt&gt;string&lt;/tt&gt;, not a C-style character array. Assuming that the name of the file we wish to use is in a &lt;tt&gt;string&lt;/tt&gt;, we can use the &lt;tt&gt;c_str&lt;/tt&gt; member (&lt;a class=&quot;docLink&quot; href=&quot;ch04lev1sec3.html#ch04lev2sec15&quot;&gt;Section 4.3.2&lt;/a&gt;, p. &lt;a class=&quot;docLink&quot; href=&quot;ch04lev1sec3.html#ch04lev2sec15&quot;&gt;139&lt;/a&gt;) to obtain a C-style string.&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;li&gt;Checking Whether an Open Succeeded &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;When we test a stream, the effect is to test whether the object is &quot;okay&quot; for input or output. If the &lt;tt&gt;open&lt;/tt&gt; fails, then the state of the &lt;tt&gt;fstream&lt;/tt&gt; object is that it is not ready for doing IO&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Rebinding a File Stream to a New File &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;Once an &lt;tt&gt;fstream&lt;/tt&gt; has been opened, it remains associated with the specified file. To associate the &lt;tt&gt;fstream&lt;/tt&gt; with a different file, we must first &lt;tt&gt;close&lt;/tt&gt; the existing file and then &lt;tt&gt;open&lt;/tt&gt; a different file&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;It is essential that we &lt;tt&gt;close&lt;/tt&gt; a file stream before attempting to open a new file. The &lt;tt&gt;open&lt;/tt&gt; function checks whether the stream is already open. If it is open, then it sets its internal state to indicate that a failure has happened. Subsequent attempts to use the file stream will fail&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Clearing the State of a File Stream &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Closing a stream does not change the internal state of the stream object &lt;br /&gt;&lt;li&gt;&lt;strong&gt;If we reuse a file stream to read or write more than one file, we must &lt;tt&gt;clear&lt;/tt&gt; the stream before using it to read from another file&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;8.4.2. File Modes &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Each &lt;tt&gt;fstream&lt;/tt&gt; class defines a set of values that represent different modes in which the stream could be opened. Like the condition state flags, the file modes are integral constants that we use with the bitwise operators (&lt;a href=&quot;ch05lev1sec3.html#ch05lev1sec3&quot;&gt;Section 5.3&lt;/a&gt;, p. &lt;a href=&quot;ch05lev1sec3.html#ch05lev1sec3&quot;&gt;154&lt;/a&gt;) to set one or more modes when we open a given file. The file stream constructors and &lt;tt&gt;open&lt;/tt&gt; have a default argument (&lt;a href=&quot;ch07lev1sec4.html#ch07lev2sec17&quot;&gt;Section 7.4.1&lt;/a&gt;, p. &lt;a href=&quot;ch07lev1sec4.html#ch07lev2sec17&quot;&gt;253&lt;/a&gt;) to set the file mode. The value of the default varies based on the type of the stream. Alternatively, we can supply the mode in which to open the file. &lt;a href=&quot;#ch08table03&quot;&gt;Table 8.3&lt;/a&gt; on the next page lists the file modes and their meanings &lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 8.3. File Modes&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;100&quot;&gt;&lt;br /&gt;&lt;col width=&quot;300&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;in&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;open for input&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;out&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;open output&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;app&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;seek to the end before every write&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;ate&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;seek to the end immediately after the open&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;trunc&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;truncate an existing stream when opening it&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;binary&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;do IO operations in binary mode&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;The modes &lt;tt&gt;out, trunc&lt;/tt&gt;, and &lt;tt&gt;app&lt;/tt&gt; may be specifed only for files associated with an &lt;tt&gt;ofstream&lt;/tt&gt; or an &lt;tt&gt;fstream&lt;/tt&gt;; &lt;tt&gt;in&lt;/tt&gt; may be specified only for files associated with either &lt;tt&gt;ifstream&lt;/tt&gt; or &lt;tt&gt;fstream&lt;/tt&gt;. Any file may be opened in &lt;tt&gt;ate&lt;/tt&gt; or &lt;tt&gt;binary&lt;/tt&gt; mode. The &lt;tt&gt;ate&lt;/tt&gt; mode has an effect only at the open: Opening a file in &lt;tt&gt;ate&lt;/tt&gt; mode puts the file at the end-of-file immediately after the open. A stream opened in &lt;tt&gt;binary&lt;/tt&gt; mode processes the file as a sequence of bytes; it does no interpretation of the characters in the stream &lt;br /&gt;&lt;li&gt;By default, files associated with an &lt;tt&gt;ifstream&lt;/tt&gt; are opened in &lt;tt&gt;in&lt;/tt&gt; mode, which is the mode that permits the file to be read. Files opened by an &lt;tt&gt;ofstream&lt;/tt&gt; are opened in &lt;tt&gt;out&lt;/tt&gt; mode, which permits the file to be written. A file opened in &lt;tt&gt;out&lt;/tt&gt; mode is truncated: All data stored in the file is discarded &lt;br /&gt;&lt;li&gt;&lt;strong&gt;In effect, specifying &lt;tt&gt;out&lt;/tt&gt; mode for an &lt;tt&gt;ofstream&lt;/tt&gt; is equivalent to specifying both &lt;tt&gt;out&lt;/tt&gt; and &lt;tt&gt;trunc&lt;/tt&gt;&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;strong&gt;The only way to preserve the existing data in a file opened by an &lt;tt&gt;ofstream&lt;/tt&gt; is to specify &lt;tt&gt;app&lt;/tt&gt; mode explicitly&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;Using the Same File for Input and Output &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;An &lt;tt&gt;fstream&lt;/tt&gt; object can both read and write its associated file. How an &lt;tt&gt;fstream&lt;/tt&gt; uses its file depends on the mode specified when we open the file &lt;br /&gt;&lt;li&gt;By default, an &lt;tt&gt;fstream&lt;/tt&gt; is opened with both &lt;tt&gt;in&lt;/tt&gt; and &lt;tt&gt;out&lt;/tt&gt; set. A file opened with both &lt;tt&gt;in&lt;/tt&gt; and &lt;tt&gt;out&lt;/tt&gt; mode set is not truncated. If we open the file associated with an &lt;tt&gt;fstream&lt;/tt&gt; with &lt;tt&gt;out&lt;/tt&gt; mode, but not &lt;tt&gt;in&lt;/tt&gt; mode specified, then the file is truncated. The file is also truncated if &lt;tt&gt;trunc&lt;/tt&gt; is specified, regardless of whether &lt;tt&gt;in&lt;/tt&gt; is specified&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Mode Is an Attribute of a File, Not a Stream &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;strong&gt;Any time open is called, the file mode is set, either explicitly or implicitly. If a mode is not specified, the default value is used.&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;Valid Combinations for Open Mode &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Not all open modes can be specified at once. Some are nonsensical, such as opening a file setting both &lt;tt&gt;in&lt;/tt&gt; and &lt;tt&gt;TRunc&lt;/tt&gt;. That would yield a stream we intend to read but that we have truncated so that there is no data to read &lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 8.4. File Mode Combinations&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;200&quot;&gt;&lt;br /&gt;&lt;col width=&quot;300&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;out&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;open for output; deletes existing data in the file&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;out | app&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;open for output; all writes at end of file&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;out | trunc&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;same as &lt;tt&gt;out&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;in&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;open for input&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;in | out&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;open for both input and output; &lt;br&gt;positioned to read the beginning of the file&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;in | out | trunc&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;open for both input and output, &lt;br&gt;deletes existing data in the file&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;strong&gt;Any open mode combination may also include &lt;tt&gt;ate&lt;/tt&gt;. The effect of adding &lt;tt&gt;ate&lt;/tt&gt; to any of these modes changes only the initial position of the file. Adding &lt;tt&gt;ate&lt;/tt&gt; to any of these mode combinations positions the file to the end before the first input or output operation is performed&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;8.4.3. A Program to Open and Check Input Files&lt;/li&gt;&lt;/ol&gt;&lt;pre&gt;&lt;span style=&quot;color: #008000&quot;&gt;// EXE-8.4.cpp : Defines the entry point for the console application.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #008000&quot;&gt;//&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;#include &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;stdafx.h&lt;/span&gt;&quot;&lt;br /&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;#include &amp;lt;fstream&amp;gt;&lt;br /&gt;#include &amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;&lt;br /&gt;#include &amp;lt;vector&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;namespace&lt;/span&gt; std;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; main(&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; argc, _TCHAR* argv[])&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;	&lt;span style=&quot;color: #008000&quot;&gt;// construct an ifstream and bind it to the file named ifile&lt;/span&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; filePath = &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;filestreamtesting.txt&lt;/span&gt;&quot;;&lt;br /&gt;	ifstream infile1(filePath);&lt;br /&gt;	&lt;span style=&quot;color: #008000&quot;&gt;// check that the open succeeded&lt;/span&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt; (!infile1) {&lt;br /&gt;		cerr &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;error: unable to open input file: &lt;/span&gt;&quot;&lt;br /&gt;			&amp;lt;&amp;lt; infile1 &amp;lt;&amp;lt; endl;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//return -1;&lt;/span&gt;&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	&lt;span style=&quot;color: #008000&quot;&gt;// ofstream output file object to write file named ofile&lt;/span&gt;&lt;br /&gt;	ofstream outfile(filePath);&lt;br /&gt;	&lt;span style=&quot;color: #008000&quot;&gt;//char *pO = &quot;try to write the file!&quot;;&lt;/span&gt;&lt;br /&gt;	&lt;br /&gt;	outfile.write(filePath.c_str(),filePath.length());&lt;br /&gt;	outfile.flush();&lt;br /&gt;	outfile.close();&lt;br /&gt;&lt;br /&gt;	ifstream infile(filePath);&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;char&lt;/span&gt; *p = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;char&lt;/span&gt;(0);&lt;br /&gt;	infile.read(p,filePath.length());&lt;br /&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; i = 0 ; i &amp;lt; filePath.length(); ++i)&lt;br /&gt;	{&lt;br /&gt;		cout &amp;lt;&amp;lt;*(p+ i) ;&lt;br /&gt;	}&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt;( *p != 0)&lt;br /&gt;	{&lt;br /&gt;	cout &amp;lt;&amp;lt; *p++;&lt;br /&gt;	}&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;delete&lt;/span&gt; p; &lt;span style=&quot;color: #008000&quot;&gt;// error to delete the p&lt;/span&gt;&lt;br /&gt;	p = 0;&lt;br /&gt;&lt;br /&gt;	infile.close();&lt;br /&gt;&lt;br /&gt;	&lt;span style=&quot;color: #008000&quot;&gt;//ifstream infile(&quot;in&quot;);      // opens file named &quot;in&quot; for reading&lt;/span&gt;&lt;br /&gt;	&lt;span style=&quot;color: #008000&quot;&gt;//infile.close();             // closes &quot;in&quot;&lt;/span&gt;&lt;br /&gt;	infile.open(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;no existing file.txt&lt;/span&gt;&quot;);        &lt;span style=&quot;color: #008000&quot;&gt;// opens file named &quot;next&quot; for reading&lt;/span&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(!infile)&lt;br /&gt;	{&lt;br /&gt;		cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;To open file failed!&lt;/span&gt;&quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;	}&lt;br /&gt;	infile.close();	&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Clearing the State of a File Stream&lt;/span&gt;&quot;)&lt;br /&gt;	{&lt;br /&gt;		ifstream input;&lt;br /&gt;		vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt; files;&lt;br /&gt;		files.push_back(filePath);&lt;br /&gt;		files.push_back(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;ReadMe.txt&lt;/span&gt;&quot;);&lt;br /&gt;		vector&amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;::const_iterator it = files.begin();&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; s;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//   for each file in the vector&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; process(&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; s);&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt; (it != files.end()) {&lt;br /&gt;			input.open(it-&amp;gt;c_str());  &lt;span style=&quot;color: #008000&quot;&gt;// open the file&lt;/span&gt;&lt;br /&gt;			&lt;span style=&quot;color: #008000&quot;&gt;// if the file is ok, read and &quot;process&quot; the input&lt;/span&gt;&lt;br /&gt;			&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt; (!input)&lt;br /&gt;				&lt;span style=&quot;color: #0000ff&quot;&gt;break&lt;/span&gt;;                    &lt;span style=&quot;color: #008000&quot;&gt;// error: bail out!&lt;/span&gt;&lt;br /&gt;			&lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt;(input &amp;gt;&amp;gt; s) &lt;span style=&quot;color: #008000&quot;&gt;// do the work on this file&lt;/span&gt;&lt;br /&gt;				process(s);&lt;br /&gt;			input.close();        &lt;span style=&quot;color: #008000&quot;&gt;// close file when we&#39;re done with it&lt;/span&gt;&lt;br /&gt;			input.clear();        &lt;span style=&quot;color: #008000&quot;&gt;// reset state to ok&lt;/span&gt;&lt;br /&gt;			++it;                 &lt;span style=&quot;color: #008000&quot;&gt;// increment iterator to get next file&lt;/span&gt;&lt;br /&gt;		}&lt;br /&gt;	}	&lt;br /&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;File Modes&lt;/span&gt;&quot;)&lt;br /&gt;	{&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//  output mode by default; truncates file named &quot;file1&quot;&lt;/span&gt;&lt;br /&gt;		ofstream outfile(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;file1&lt;/span&gt;&quot;);&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// equivalent effect: &quot;file1&quot; is explicitly truncated&lt;/span&gt;&lt;br /&gt;		ofstream outfile2(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;file1&lt;/span&gt;&quot;, ofstream::out | ofstream::trunc);&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//  append mode; adds new data at end of existing file named &quot;file2&quot;&lt;/span&gt;&lt;br /&gt;		ofstream appfile(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;file2&lt;/span&gt;&quot;, ofstream::app);&lt;br /&gt;	}&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Using the Same File for Input and Output&lt;/span&gt;&quot;)&lt;br /&gt;	{&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// open for input and output&lt;/span&gt;&lt;br /&gt;		fstream inOut(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;copyOut&lt;/span&gt;&quot;, fstream::in | fstream::out);&lt;br /&gt;	}&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Mode Is an Attribute of a File, Not a Stream&lt;/span&gt;&quot;)&lt;br /&gt;	{&lt;br /&gt;		ofstream outfile;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// output mode set to out, &quot;scratchpad&quot; truncated&lt;/span&gt;&lt;br /&gt;		outfile.open(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;scratchpad&lt;/span&gt;&quot;, ofstream::out);&lt;br /&gt;		outfile.close();    &lt;span style=&quot;color: #008000&quot;&gt;// close outfile so we can rebind it&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// appends to file named &quot;precious&quot;&lt;/span&gt;&lt;br /&gt;		outfile.open(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;precious&lt;/span&gt;&quot;, ofstream::app);&lt;br /&gt;		outfile.close();&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// output mode set by default, &quot;out&quot; truncated&lt;/span&gt;&lt;br /&gt;		outfile.open(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;out&lt;/span&gt;&quot;);&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;A Program to Open and Check Input Files&lt;/span&gt;&quot;)&lt;br /&gt;	{&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// opens in binding it to the given file&lt;/span&gt;&lt;br /&gt;		ifstream&amp;amp; open_file(ifstream &amp;amp;in, &lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; &amp;amp;file);&lt;br /&gt;&lt;br /&gt;		&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// failed to build with the following code&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// can&#39;t call the code like the following&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;//ifstream infile = open_file(infile, filePath);&lt;/span&gt;&lt;br /&gt;		ifstream &amp;amp;infile = open_file(infile, filePath);&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; i;&lt;br /&gt;	cin &amp;gt;&amp;gt; i;&lt;br /&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; process(&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; s)&lt;br /&gt;{&lt;br /&gt;	cout &amp;lt;&amp;lt; s &amp;lt;&amp;lt; endl;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: #008000&quot;&gt;// opens in binding it to the given file&lt;/span&gt;&lt;br /&gt;ifstream&amp;amp; open_file(ifstream &amp;amp;in, &lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; &amp;amp;file)&lt;br /&gt;{&lt;br /&gt;	in.close();     &lt;span style=&quot;color: #008000&quot;&gt;// close in case it was already open&lt;/span&gt;&lt;br /&gt;	in.clear();     &lt;span style=&quot;color: #008000&quot;&gt;// clear any existing errors&lt;/span&gt;&lt;br /&gt;	&lt;span style=&quot;color: #008000&quot;&gt;// if the open fails, the stream will be in an invalid state&lt;/span&gt;&lt;br /&gt;	in.open(file.c_str()); &lt;span style=&quot;color: #008000&quot;&gt;// open the file we were given&lt;/span&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; in; &lt;span style=&quot;color: #008000&quot;&gt;// condition state is good if open succeeded&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h4&gt;Section 8.5 String Streams&lt;/h4&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;The &lt;tt&gt;iostream&lt;/tt&gt; library supports in-memory input/output, in which a stream is attached to a &lt;tt&gt;string&lt;/tt&gt; within the program&#39;s memory. That &lt;tt&gt;string&lt;/tt&gt; can be written to and read from using the &lt;tt&gt;iostream&lt;/tt&gt; input and output operators. The library defines three kinds of string streams: &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;&lt;tt&gt;istringstream&lt;/tt&gt;, derived from &lt;tt&gt;istream&lt;/tt&gt;, reads from a &lt;tt&gt;string&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;&lt;tt&gt;ostringstream&lt;/tt&gt;, derived from &lt;tt&gt;ostream&lt;/tt&gt;, writes to a &lt;tt&gt;string&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p&gt;&lt;tt&gt;stringstream&lt;/tt&gt;, derived from &lt;tt&gt;iostream&lt;/tt&gt;, reads and writes a &lt;tt&gt;string&lt;/tt&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;li&gt;In addition to the operations that the &lt;tt&gt;sstream&lt;/tt&gt; types inherit, these types have a constructor that takes a &lt;tt&gt;string&lt;/tt&gt;. The constructor copies the &lt;tt&gt;string&lt;/tt&gt; argument into the &lt;tt&gt;stringstream&lt;/tt&gt; object. The operations that read and write the &lt;tt&gt;stringstream&lt;/tt&gt; read or write the &lt;tt&gt;string&lt;/tt&gt; in the object. These classes also define a member named &lt;tt&gt;str&lt;/tt&gt; to fetch or set the &lt;tt&gt;string&lt;/tt&gt; value that the &lt;tt&gt;stringstream&lt;/tt&gt; manipulates &lt;br /&gt;&lt;li&gt;Note that although &lt;tt&gt;fstream&lt;/tt&gt; and &lt;tt&gt;sstream&lt;/tt&gt; share a common base class, they have no other interrelationship. In particular, we cannot use &lt;tt&gt;open&lt;/tt&gt; and &lt;tt&gt;close&lt;/tt&gt; on a &lt;tt&gt;stringstream&lt;/tt&gt;, nor can we use &lt;tt&gt;str&lt;/tt&gt; on an &lt;tt&gt;fstream&lt;/tt&gt; &lt;br /&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign=&quot;top&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;table class=&quot;allBorders&quot; border=&quot;1&quot; rules=&quot;none&quot; cellspacing=&quot;0&quot; cellpadding=&quot;5&quot;&gt;&lt;br /&gt;&lt;caption&gt;&lt;br /&gt;&lt;h5 class=&quot;docTableTitle&quot;&gt;Table 8.5. &lt;tt&gt;stringstream&lt;/tt&gt;-Specific Operations&lt;/h5&gt;&lt;/caption&gt;&lt;br /&gt;&lt;colgroup&gt;&lt;br /&gt;&lt;col width=&quot;200&quot;&gt;&lt;br /&gt;&lt;col width=&quot;325&quot;&gt;&lt;/colgroup&gt;&lt;br /&gt;&lt;thead&gt;&lt;/thead&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;stringstream strm;&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Creates an unbound &lt;tt&gt;stringstream&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;stringstream strm(s);&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Creates a &lt;tt&gt;stringstream&lt;/tt&gt; that holds a copy of the &lt;tt&gt;string s&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;strm.str()&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Returns a copy of the &lt;tt&gt;string&lt;/tt&gt; that &lt;tt&gt;strm&lt;/tt&gt; holds.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;&lt;tt&gt;strm.str(s)&lt;/tt&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td class=&quot;docTableCell&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;br /&gt;&lt;p class=&quot;docText&quot;&gt;Copies the &lt;tt&gt;string s&lt;/tt&gt; into &lt;tt&gt;strm&lt;/tt&gt;. Returns &lt;tt&gt;void&lt;/tt&gt;.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;li&gt;Using a stringstream &lt;br /&gt;&lt;li&gt;stringstreams Provide Conversions and/or Formatting &lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;One common use of &lt;tt&gt;stringstream&lt;/tt&gt;s is when we want to obtain automatic formatting across multiple data types &lt;br /&gt;&lt;li&gt;Reading an &lt;tt&gt;istringstream&lt;/tt&gt; automatically converts from the character representation of a numeric value to its corresponding arithmetic value &lt;br /&gt;&lt;li&gt;&lt;strong&gt;To read &lt;tt&gt;input_string&lt;/tt&gt;, we must parse the &lt;tt&gt;string&lt;/tt&gt; into its component parts. We want the numeric values; to get them we must read (and ignore) the labels that are interspersed with the data we want&lt;/strong&gt; &lt;br /&gt;&lt;li&gt;Because the input operator reads typed values, it is essential that the types of the objects into which we read be compatible with the types of the values read from the &lt;tt&gt;stringstream&lt;/tt&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;pre&gt;&lt;span style=&quot;color: #008000&quot;&gt;// EXE-8.5.cpp : Defines the entry point for the console application.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #008000&quot;&gt;//&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;#include &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;stdafx.h&lt;/span&gt;&quot;&lt;br /&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;span style=&quot;color: #008000&quot;&gt;//here for using istringstream must include the sstream&lt;/span&gt;&lt;br /&gt;#include &amp;lt;sstream&amp;gt;&lt;br /&gt;#include &amp;lt;&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt;&amp;gt;&lt;br /&gt;#include &amp;lt;vector&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;namespace&lt;/span&gt; std;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; main(&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; argc, _TCHAR* argv[])&lt;br /&gt;{&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Using a stringstream&lt;/span&gt;&quot;)&lt;br /&gt;	{&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; line, word;      &lt;span style=&quot;color: #008000&quot;&gt;// will hold a line and word from input, respectively&lt;/span&gt;&lt;br /&gt;		cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Please input a string (\\0 for break):&lt;/span&gt;&quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt; (getline(cin, line))   {            &lt;span style=&quot;color: #008000&quot;&gt;// read a line from the input into line&lt;/span&gt;&lt;br /&gt;			&lt;span style=&quot;color: #008000&quot;&gt;// do per-line processing&lt;/span&gt;&lt;br /&gt;			&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(line == &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;\\0&lt;/span&gt;&quot;)&lt;br /&gt;				&lt;span style=&quot;color: #0000ff&quot;&gt;break&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;			istringstream strIn(line);            &lt;span style=&quot;color: #008000&quot;&gt;// bind to stream to the line we read&lt;/span&gt;&lt;br /&gt;			&lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt; (strIn &amp;gt;&amp;gt; word){          &lt;span style=&quot;color: #008000&quot;&gt;// read a word from line&lt;/span&gt;&lt;br /&gt;				&lt;span style=&quot;color: #008000&quot;&gt;// do per-word processing&lt;/span&gt;&lt;br /&gt;				cout &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;PrintOut:&lt;/span&gt;&quot; &amp;lt;&amp;lt; word &amp;lt;&amp;lt; endl;&lt;br /&gt;			}&lt;br /&gt;		}&lt;br /&gt;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt;(&quot;&lt;span style=&quot;color: #8b0000&quot;&gt;stringstreams Provide Conversions and/or Formatting&lt;/span&gt;&quot;)&lt;br /&gt;	{&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; val1 = 512, val2 = 1024;&lt;br /&gt;		ostringstream format_message;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// ok: converts values to a string representation&lt;/span&gt;&lt;br /&gt;		format_message &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;val1: &lt;/span&gt;&quot; &amp;lt;&amp;lt; val1 &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;\n&lt;/span&gt;&quot;&lt;br /&gt;			&amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;val2: &lt;/span&gt;&quot; &amp;lt;&amp;lt; val2 &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt;\n&lt;/span&gt;&quot;;&lt;br /&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// str member obtains the string associated with a stringstream&lt;/span&gt;&lt;br /&gt;		istringstream input_istring(format_message.str());&lt;br /&gt;		&lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; dump; &lt;span style=&quot;color: #008000&quot;&gt;// place to dump the labels from the formatted message&lt;/span&gt;&lt;br /&gt;		&lt;span style=&quot;color: #008000&quot;&gt;// extracts the stored ascii values, converting back to arithmetic types&lt;/span&gt;&lt;br /&gt;		input_istring &amp;gt;&amp;gt; dump &amp;gt;&amp;gt; val1 &amp;gt;&amp;gt; dump &amp;gt;&amp;gt; val2;&lt;br /&gt;		cout &amp;lt;&amp;lt; val1 &amp;lt;&amp;lt; &quot;&lt;span style=&quot;color: #8b0000&quot;&gt; &lt;/span&gt;&quot; &amp;lt;&amp;lt; val2 &amp;lt;&amp;lt; endl;  &lt;span style=&quot;color: #008000&quot;&gt;// prints 512 1024&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;int&lt;/span&gt; i;&lt;br /&gt;	cin &amp;gt;&amp;gt; i;&lt;br /&gt;&lt;br /&gt;	&lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;  </content><link rel='replies' type='application/atom+xml' href='http://tech-thinker.blogspot.com/feeds/1556364777770129748/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8409310322589078134/1556364777770129748?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/1556364777770129748'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8409310322589078134/posts/default/1556364777770129748'/><link rel='alternate' type='text/html' href='http://tech-thinker.blogspot.com/2011/01/c-primer-fourth-edition-notes-chapter-8_10.html' title='C++ Primer, Fourth Edition Notes Chapter 8. The IO Library'/><author><name>Angelo</name><uri>http://www.blogger.com/profile/06424189718714948875</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYc1ixnCWhp85mM725flTfDOuqA_04DPYVHNG_E4yLhgAl_qKnipWS5NNwrKRrPqFFbz3XtJFlU98n4Csstul5pnSnvrUwPqIivFSnbOQL6dKuQbTHUz7LazerpTQq8w/s400/logo_s.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLkGuIMhCCXUtWqvA2ZlbTB4rahXqC6GcRl98lVeZvGCXkrLHIrcaZkX02DiC6HfxyGNYn9gR3GViNlAvIQq2FVAbZICKGFQH9bYr_Rox9FNf-feKi9m_VlWvGRYz3-YZ4qTMF_MXEVuQ/s72-c?imgmax=800" height="72" width="72"/><thr:total>0</thr:total></entry></feed>