卐言出法随卐 發表於 2025-4-25 18:36:00

01 C++ 程序设计基础

<h2 id="头文件">头文件</h2>
<h3 id="iostream">iostream</h3>
<p>​        <strong><code>cin/cout</code> 输入输出流对象</strong></p>
<p>​        <strong><code>&lt;&lt;</code> 流插入操作符, <code>&gt;&gt;</code> 流提取操作符</strong></p>
<h3 id="iomanip">iomanip</h3>
<table>
<thead>
<tr>
<th>流操作符</th>
<th>功能</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>stew(n)</code></td>
<td>设置字符宽度(仅对一项有效),可用于<code>cin</code></td>
</tr>
<tr>
<td><code>setprecision(n)</code></td>
<td>设置浮点数精度(对多项有效)</td>
</tr>
<tr>
<td><code>fixed</code></td>
<td>固定小数点(对多项有效)</td>
</tr>
<tr>
<td><code>hex</code> <code>oct</code> <code>dec</code></td>
<td>十六进制、八进制、十进制</td>
</tr>
<tr>
<td><code>scientific</code></td>
<td>科学计数法</td>
</tr>
<tr>
<td><code>showpoint</code></td>
<td>显示小数点和小数位</td>
</tr>
<tr>
<td><code>setfill(c)</code></td>
<td>用字符<code>c</code>填充空位</td>
</tr>
<tr>
<td><code>right</code> <code>left</code></td>
<td>右对齐(默认)、左对齐</td>
</tr>
<tr>
<td><code>showpos</code></td>
<td>显示正数<code>+</code>号</td>
</tr>
</tbody>
</table>
<p><strong>样例:</strong></p>
<pre><code class="language-cpp">cout&lt;&lt;setw(10)&lt;&lt;setfill('*')&lt;&lt;right&lt;&lt;42&lt;&lt;endl;
cout&lt;&lt;stew(10)&lt;&lt;setfill('*')&lt;&lt;left&lt;42&lt;&lt;endl;
cout&lt;&lt;fixed&lt;&lt;setprecision(3)&lt;&lt;3.1415926&lt;&lt;endl;
cout&lt;&lt;showpos&lt;&lt;10&lt;&lt;' '&lt;&lt;-10&lt;&lt;endl;
</code></pre>
<pre><code class="language-cpp">********42
42********
3.142
+10 -10
</code></pre>
<h2 id="格式化输入输出">格式化输入输出</h2>
<h3 id="cout成员函数"><code>cout</code>成员函数:</h3>
<table>
<thead>
<tr>
<th>成员函数</th>
<th>功能</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>cout.precision(n)</code></td>
<td>设置小数位数(影响全局)</td>
</tr>
<tr>
<td><code>cout.fill(c)</code></td>
<td>填充字符<code>c</code>(影响全局)</td>
</tr>
<tr>
<td><code>cout.setf(flag)</code> <code>unsetf(flag)</code></td>
<td>设置标志格式位(影响全局),如<code>ios::fixed</code></td>
</tr>
<tr>
<td><code>cout.width(n)</code></td>
<td>设置输出宽度(仅影响下一次)</td>
</tr>
</tbody>
</table>
<h3 id="cin成员函数"><code>cin</code>成员函数</h3>
<table>
<thead>
<tr>
<th>成员函数</th>
<th>作用</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>cin.get(char&amp;)</code></td>
<td>读取一个字符</td>
</tr>
<tr>
<td><code>cin.getline(char*,int)</code></td>
<td>读取一行字符串(读取整行,丢弃换行符)</td>
</tr>
<tr>
<td><code>cin.ignore(int,char)</code></td>
<td>忽略输入流中的<code>n</code>个字符,或直到<code>char</code>出现(一并丢弃)</td>
</tr>
</tbody>
</table>
<pre><code class="language-cpp">char name;
cin&gt;&gt;name; // 可能数组溢出!
// 正确做法
charword ;
cin &gt;&gt; setw ( 10 ) &gt;&gt; word ;
cin.width(10);
cin &gt;&gt; word ;
</code></pre>
<p><code>cin.ignore(n,ch);</code> 表示跳过后面 <span class="math inline">\(n\)</span> 个字符或遇到字符 <span class="math inline">\(ch\)</span> ,常用 <code>cin.ignore()</code> 跳过下一个字符</p>
<h2 id="默认参数">默认参数</h2>
<p><strong>默认参数也称缺省参数,在函数调用中省略了函数实参,将把参数的缺省值赋给函数形参</strong></p>
<p><strong>缺省形参值必须从右向左顺序声明,因为调用时实参取代形参是从左向右的顺序,避免调用时产生歧义</strong></p>
<pre><code class="language-cpp">voidshowArea(float length,float width=10.0);
voidshowArea(float length,float width)
{
    floatarea = length * width ;
    cout &lt;&lt; "The area is" &lt;&lt; area &lt;&lt; endl ;
}
showArea( ) ;
showArea(12.0) ;
showArea(12.0, 5.5) ;
</code></pre>
<h2 id="引用">引用</h2>
<p><strong>引用是一个已存在对象的别名,可以像指针一样间接操作变量</strong></p>
<p><strong>声明:</strong> <code>type &amp;ref = object;</code></p>
<h2 id="函数重载">函数重载</h2>
<p><strong>程序中定义多个函数,函数的名字相同,但参数的类型或个数不完全相同</strong></p>
<p><strong>概念:</strong>编译器根据函数调用时的参数类型和数量,选择合适的函数进行调用</p>
<p><strong>条件:</strong>参数个数不同、参数类型不同、参数类型不同时顺序不同</p>
<pre><code class="language-cpp">int square(int number)
{return number*number;}
double square(double number)
{return number*number;}
// 不能采用函数返回值类型来区别重载
int square(int);
double square(int); // wrong
</code></pre>
<h2 id="内联函数">内联函数</h2>
<p><strong>将函数前加上<code>inline</code>关键字,只是建议编译器,编译器可忽略它</strong></p>
<p>在内联函数内不允许用循环语句、<code>switch</code>语句和嵌套的<code>if</code>等</p>
<pre><code class="language-cpp">inline bool isDigit(char ch)
{ returnch&gt;='0' &amp;&amp; ch&lt;='9' ? true : false; }
int main ( )
{       
    char ch;       
    while(cin.get(ch))
      if(isDigit(ch)) cout&lt;&lt;ch&lt;&lt;" is a Digit.\n";
            else cout&lt;&lt;ch&lt;&lt;" is not a Digit.\n";
    return 0;
}
</code></pre>
<h2 id="内存的动态分配和释放">内存的动态分配和释放</h2>
<p><code>new</code>操作符:在堆上动态分配内存并返回指向该内存的指针</p>
<p><code>delete</code>操作符:释放由 <code>new</code> 分配的单个对象或数组的内存</p>
<pre><code class="language-cpp">// C 风格
int* p = (int*)malloc(4);
*p=1;
free(p);
</code></pre>
<pre><code class="language-cpp">int *p1,*p2,*p3;
p1=new int;
p2=new int(100); // 初始值为 100
p3=new int; // 数组
*p1=25;
cout&lt;&lt;*p1;
delete p2;
delete [] p3;
</code></pre>
<p><strong>若函数参数是指针,申请动态内存无效</strong></p>
<pre><code class="language-cpp">void getMemory(char *p,int num)
{p=new char;}
</code></pre><br><br>
来源:https://www.cnblogs.com/violet1359/p/18810258
頁: [1]
查看完整版本: 01 C++ 程序设计基础