⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 string1.html

📁 关于 C++ 的历史无须我来介绍了
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

  
  <link rel="stylesheet" href="css/stdlayout.css" type="text/css">

  
  <link rel="stylesheet" href="css/print.css" type="text/css">

  
  <meta content="text/html; charset=gb2312" http-equiv="content-type">

  
  <title>字元阵列(C-style 字串)</title>
</head>


<body>

<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>

<h1><a href="CppGossip.html">C++ Gossip: 字元阵列(C-style 字串)</a></h1>

<br>

在C++中字串的本质是由字元所组成的阵列,并在最后加上一个空(null)字元'\0',例如下面这个程式就是一个"hello"字串的宣告:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">char
str[] = {'h', 'e', 'l', 'l', 'o', '\0'};</span><br>

</div>

<br>

之后您可以直接使用str来代表该字串,例如在文字模式下输出str字串:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout
&lt;&lt; str &lt;&lt; endl;</span><br>

</div>

<br>

C++是使用空字元来识别一个字元阵列是否表示字串,像上例就可以用来表示一个字串"hello",在C++中您也可以这么宣告字串:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">char
str[] = "hello";</span><br>

</div>

<br>

在这个例子中,虽然您没有指定空字元'\0',但是程式会自动加上空字元,而它基本上还是字元阵列,与前一个宣告方式是相同的,您从下面这个程式就可以知
道: <br>

<br>

<pre>#include &lt;iostream&gt; <br>using namespace std; <br><br>int main() { <br>    char str[] = "hello"; <br><br>    for(int i = 0; i &lt; (sizeof(str)/sizeof(str[0])); i++) { <br>        if(str[i] == '\0') <br>            cout &lt;&lt; " null"; <br>        else <br>            cout &lt;&lt; " " &lt;&lt; str[i]; <br>    } <br>    cout &lt;&lt; endl; <br>    <br>    return 0; <br>}</pre>

<br>

<span class="postbody">执行结果:</span><br>

<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">

  <tbody>

    <tr>

      <td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">&nbsp;h e l l o null</span></small><span style="color: rgb(255, 255, 255);"><br>

      </span></td>

    </tr>

  
  </tbody>
</table>

<br>

字串是字元阵列,所以您可以使用阵列的存取方式取出每一个字元,在指定"hello"时表面上虽然只有5个字元,但是最后会加上一个空字元'\0',所以
str[]共使用了6个字元,由于字串的最后一个字元为空字元,所以您可以检验这个字元来看看字串是否为空,例如:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">if(str[0] == '\0') {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; cout
&lt;&lt; "字串为空";</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>

</div>

<br>

空字元在作条件判断时,会被视为0,所以上式还可以这么写:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">if( ! str[0] ) {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; cout
&lt;&lt; "字串为空";</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>

</div>

<br>

字串一但指定,它的长度就固定了,也就是字元阵列的长度,所以您由使用者输入取得字串值时,需注意不要超过字串的长度;从使用者输入取得字串值只要这么作
就可以了: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">char str[80]; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout &lt;&lt;
"输入字串:"; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cin &gt;&gt; str; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout &lt;&lt;
"您输入的字串为 " &lt;&lt; str &lt;&lt; endl;</span><br>

</div>

<br>

这个程式片段可以取得使用者的字串输入,所输入的字串长度不得超过80个字元,80个字元的上限包括空白字元,所以实际上可以输入79个字元;如果输入的
字元超出所宣告的上限,会发生不可预期的错误。 <br>

<br>

要指定新的字串值给它时,您不能像下面的方式指定: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">char str[80]; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">str = "Just";</span><br>

</div>

<br>

而必须要一个字元一个字元的指定至阵列中,并在最后加上空白字元,例如: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">char str[80] = {'\0'}; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">str[0] = 'J'; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">str[1] = 'u'; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">str[2] = 's'; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">str[3] = 't'; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">str[4] = '\0'; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout &lt;&lt; str
&lt;&lt; endl;</span><br>

</div>

<br>

这样的字元指定方式当然相当的不方便,所以C++提供了字串处理的相关函式,例如: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">strcpy(str1,
str2);&nbsp; // str2字串复制给str1字串 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">strcat(str1,
str2);&nbsp; // str2字串串接在str1字串后 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">strlen(str);&nbsp;
// 计算不含空字元的字串长度 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">strcmp(str1,
str2); // 比较两个字串</span><br>

</div>

<br>

事实上这是从Standard C延续下来的字串处理函式,要使用这些函式,您要含入cstring或是string,例如:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">#include &lt;cstring&gt;</span><br>

</div>

<br>

下面这个程式是个简单的示范:<br>

<br>

<pre>#include &lt;iostream&gt; <br>#include &lt;cstring&gt; <br>using namespace std; <br><br>int main() { <br>    char str1[80] = {'\0'}; <br>    char str2[] = "caterpillar"; <br><br>    cout &lt;&lt; "str1: " &lt;&lt; str1 &lt;&lt; endl<br>         &lt;&lt; "str2: " &lt;&lt; str2 &lt;&lt; endl<br>         &lt;&lt; endl; <br><br>    // 将str2复制给str1 <br>    strcpy(str1, str2); <br>    cout &lt;&lt; "str1: " &lt;&lt; str1 &lt;&lt; endl<br>         &lt;&lt; "str2: " &lt;&lt; str2 &lt;&lt; endl<br>         &lt;&lt; endl;<br><br>    // 将str2接在str1后 <br>    strcat(str1, str2); <br>    cout &lt;&lt; "str1: " &lt;&lt; str1 &lt;&lt; endl<br>         &lt;&lt; "str2: " &lt;&lt; str2 &lt;&lt; endl<br>         &lt;&lt; endl;<br><br>    cout &lt;&lt; "str1长度:" &lt;&lt; strlen(str1) &lt;&lt; endl<br>         &lt;&lt; "str2长度:" &lt;&lt; strlen(str2) &lt;&lt; endl<br>         &lt;&lt; endl;<br><br>    cout &lt;&lt; "str1与str2比较:" &lt;&lt; strcmp(str1, str2) &lt;&lt; endl<br>         &lt;&lt; endl;<br><br>    <br>    system("pause");<br>    <br>    return 0; <br>}<br></pre>

<br>

<span class="postbody">
执行结果:</span><br>

<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">

  <tbody>

    <tr>

      <td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">str1:<br>

str2: caterpillar<br>

      <br>

str1: caterpillar<br>

str2: caterpillar<br>

      <br>

str1: caterpillarcaterpillar<br>

str2: caterpillar<br>

      <br>

str1长度:22<br>

str2长度:11<br>

      <br>

str1与str2比较:1</span></small><span style="color: rgb(255, 255, 255);"><br>

      </span></td>

    </tr>

  
  </tbody>
</table>

<br>

strcmp(str1,
str2)会比较字串str1与str2的大小,若相同就传回0,str1大于str2则传回大于0的值,小于则传回小于0的值,比较的标准是依字典顺序
比对,例如若str1大于str2,表示str1在字典中的顺序是在str2之后;strlen()所传回的字串长度值并不包括空字元,这点必须注意。
<br>

<br>

如果您使用cin来取得使用者的输入字串,则您会发现输入字串时中间不能包括空白,如果您想要在输入字串时包括空白,您必须使用gets()函式,例如:
<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">char str[80]; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout &lt;&lt;
"输入字串:"; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">gets(str); </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout &lt;&lt;
"输入的字串:" &lt;&lt; str &lt;&lt; endl;</span><br>

</div>

<br>

使用gets()函式时有一点必须注意,就是它并不会检查使用者的输入是否超出字元阵列的长度,使用时必须小心,有的编译器会提示警告讯息。 <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace; color: rgb(255, 0, 0);">: the `gets' function
is dangerous and should not be used.</span><br>

</div>

<br>

字串的宣告还有指标(Pointer)的宣告方式,这个留待谈到指标时再来说明。<br>

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -