﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-晓龙的专栏</title><link>http://www.cppblog.com/xiaolong/</link><description>C++ &amp; OO &amp; 数据结构&amp;算法</description><language>zh-cn</language><lastBuildDate>Tue, 09 Jun 2026 18:51:21 GMT</lastBuildDate><pubDate>Tue, 09 Jun 2026 18:51:21 GMT</pubDate><ttl>60</ttl><item><title>对象的创建和使用(笔记)</title><link>http://www.cppblog.com/xiaolong/archive/2007/01/31/18212.html</link><dc:creator>晓龙</dc:creator><author>晓龙</author><pubDate>Wed, 31 Jan 2007 05:47:00 GMT</pubDate><guid>http://www.cppblog.com/xiaolong/archive/2007/01/31/18212.html</guid><wfw:comment>http://www.cppblog.com/xiaolong/comments/18212.html</wfw:comment><comments>http://www.cppblog.com/xiaolong/archive/2007/01/31/18212.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/xiaolong/comments/commentRss/18212.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xiaolong/services/trackbacks/18212.html</trackback:ping><description><![CDATA[/*** 第二章：对象的创建和使用 ***/ <br />#include&lt;iostream&gt;<br />#include&lt;string&gt;<br />#include&lt;fstream&gt;<br />#include&lt;vector&gt;<br />using namespace std;<br />int main()<br />{<br />    // output  13 in the way of octal &amp; hex;<br />    cout &lt;&lt; "the octal form is :"<br />         &lt;&lt; oct &lt;&lt; 13 &lt;&lt; endl;          <br />    cout &lt;&lt; "the hex form is   :"<br />         &lt;&lt; hex &lt;&lt; 13 &lt;&lt; endl;   <br />    // the system can recognize floating-point automatically;      <br />    cout &lt;&lt; "the float number is:"<br />         &lt;&lt; 3.14159 &lt;&lt; endl;<br />    // chang ASCII code 27 to symbol "escape";<br />    cout &lt;&lt; "the non-printing character:"<br />         &lt;&lt; char(27) &lt;&lt; endl;<br />    // usage of object cin;<br />    int number;<br />    cout &lt;&lt; "enter one integer:" &lt;&lt; endl;<br />    cin  &gt;&gt; number;           <br />    cout &lt;&lt; "number is:" &lt;&lt; dec &lt;&lt; number &lt;&lt; endl;<br />    // connect the strings with using the "string" class<br />    cout &lt;&lt; "Display connenting the strings: " &lt;&lt; endl;<br />    string s1,s2;       //empty strings;<br />    string s3 = "hello,my ";//initializing;<br />    string s4("name is");   //initializing;<br />    s1 = "Oh!~";<br />    s2 = s3 + s4 + " ";     //connect...<br />    s1 += s2;               //appending to s1...<br />    cout &lt;&lt; s1 + "mumu" &lt;&lt; endl;<br />    // copy one file to another with one line at a time;<br />    // we need class &lt;fstream&gt;<br />    ifstream in("chapter2.cpp");// object ifstream opened for reading only; <br />    ofstream out("Cpchapter2.cpp");//object ofstream  for writing only;<br />    string temp;<br />    while(getline(in,temp))   //getline() will discard the "\n";<br />         out &lt;&lt; temp &lt;&lt; "\n";<br />    // copy an entire file into a single string<br />    // object string is dynamic &amp; don't worry about the memory allocation;<br />    string str1,str2;<br />    while(getline(in,str2))<br />         str1 += str2 +"\n";<br />    cout &lt;&lt; str1;<br />    // introduction to standard container class -- Vector<br />    // vector class is a template , you can use it in different classes effectively<br />    ifstream in1("chapter2.cpp");<br />    string temp1;<br />    vector&lt;string&gt; v;//make a vector which only loads string objects;<br />    while(getline(in1,temp1))<br />         v.push_back(temp1); // member function adds line to the end of container ;<br />    for(int i=0;i&lt;v.size(); ++i)<br />       cout &lt;&lt; i &lt;&lt;":" &lt;&lt;v[i] &lt;&lt; endl;<br />    system("pause");<br />}<br />/*** 练习感悟 ***/<br />//向容器中输入元素的正确方式是：<br />                                float temp;<br />                                cin &gt;&gt; temp;<br />                                v.push_back(temp);<br />              //    错误方式是：<br />                                cin &gt;&gt; v[i];<br />              // v[i]的使用只用在输出和对元素进行操作(赋值)的时候正确。<br />//读入回车键的正确方法：<br />//习题：将一个文件的内容逐行输出，要求当用户按下回车键的时候输出下一行。 <br />#include&lt;iostream&gt;<br />#include&lt;fstream&gt;<br />#include&lt;string&gt;<br />using namespace std;<br />int main()<br />{<br />    ifstream  in("chapter2.cpp");<br />    string temp;<br />    char ch;<br />    while(getline(in ,temp))<br />    {<br />         cout &lt;&lt; temp;<br />         ch = getchar();<br />         if(ch != '\n')<br />            break;<br />    }<br />    system("pause");<br />}<br />//错误方法：cin &gt;&gt; ch; 系统无法识别输入的是'\r' OR '\n'... <br />    <br />         <br /><img src ="http://www.cppblog.com/xiaolong/aggbug/18212.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xiaolong/" target="_blank">晓龙</a> 2007-01-31 13:47 <a href="http://www.cppblog.com/xiaolong/archive/2007/01/31/18212.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>