鍍金池/ 教程/ C/ 字符串
動態(tài)內存
類和對象
接口 (抽象類)
結構體
循環(huán)的類型
函數(shù)
數(shù)字
日期和時間
基本語法
多態(tài)
數(shù)據(jù)抽象
注釋
命名空間
字符串
預處理器
決策語句
修飾符的類型
鍙橀噺綾誨瀷
基本輸入輸出
操作符
數(shù)組
模板
多線程
繼承
Web 編程
信號處理
指針
存儲類型
概述
引用
常量
異常處理
開發(fā)環(huán)境
重載
變量作用域
數(shù)據(jù)類型
數(shù)據(jù)封裝
文件和流

字符串

C++ 提供了以下兩種類型的字符串表示形式:

  • C 樣式字符串
  • 用標準 C++ 介紹的標準字符串類型

C 樣式字符串

C 樣式字符串源于 C 語言,在 C++ 中仍然被支持。這個串實際是一個字符的一維數(shù)組,這個數(shù)組以一個字符 ‘\0’ 結束。因此以 null 結尾的字符串包含由字符組成的字符串,此字符串后跟著一個 null。

接下來聲明和初始化創(chuàng)建一個字符串,這個字符串組成一個單詞 "hello"。為了包含數(shù)組末尾的空字符,包含該字符串的字符數(shù)組的大小應該比單詞 "hello" 中的字符的數(shù)目多一個。

    char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

如果你遵循數(shù)組初始化的規(guī)則,你可以像下面一樣書寫上面的語句:

    char greeting[] = "Hello";

以下是在 C/C++ 中定義上面的字符串的內存演示:

http://wiki.jikexueyuan.com/project/cplusplus/images/string_representation.jpg" alt="" />

實際上,你不需要在字符串常量的末尾放置一個空字符。 C++ 編譯器在初始化數(shù)組時自動在串的末尾放置一個 '\0'。讓我們嘗試打印上述字符串:

    #include <iostream>

    using namespace std;

    int main ()
    {
       char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

       cout << "Greeting message: ";
       cout << greeting << endl;

       return 0;
    }

當上述代碼被編譯執(zhí)行時,它將產生如下結果:

    Greeting message: Hello

C++ 支持廣泛的函數(shù)來操作以空字符終止的字符串:

SN 函數(shù)和功能
1 strcpy(s1,s2); 將字符串 s2 復制到字符串 s1 中。
2 strcat(s1,s2); 將字符串 s2 串聯(lián)到字符串 s1 的結尾。
3 strlen(s1); 返回字符串 s1 的長度。
4 strcmp(s1,s2); 如果 s1 和 s2 相同,返回 0;如果 s1≥s2,返回大于 0 的數(shù);如果 s1
5 strchr(s1,ch); 返回在字符串 s1 中指向第一次出現(xiàn)字符 ch 的指針。
6 strstr(s1,s2); 返回在字符串 s1 中指向第一次出現(xiàn)字符串 s2 的指針。

以下是應用了一些上述函數(shù)的示例:

    #include <iostream>
    #include <cstring>

    using namespace std;

    int main ()
    {
       char str1[10] = "Hello";
       char str2[10] = "World";
       char str3[10];
       int  len ;

       // copy str1 into str3
       strcpy( str3, str1);
       cout << "strcpy( str3, str1) : " << str3 << endl;

       // concatenates str1 and str2
       strcat( str1, str2);
       cout << "strcat( str1, str2): " << str1 << endl;

       // total lenghth of str1 after concatenation
       len = strlen(str1);
       cout << "strlen(str1) : " << len << endl;

       return 0;
    }

當上述代碼被編譯執(zhí)行時,它將產生如下結果:

    strcpy( str3, str1) : Hello
    strcat( str1, str2): HelloWorld
    strlen(str1) : 10

C++ 中的字符串類

標準的 C++ 庫中提供了一個 string 類,它支持上面提到的所有的操作,另外它還有更多的功能。我們會研究 C++ 標準庫中的這個類,但現(xiàn)在我們先檢查以下示例:

在這點上,你可能還沒有明白這個例子,因為到目前為止我們還沒有討論類和對象。所以直到你理解了面向對象的概念你才可以繼續(xù)進行下去。

    #include <iostream>
    #include <string>

    using namespace std;

    int main ()
    {
       string str1 = "Hello";
       string str2 = "World";
       string str3;
       int  len ;

       // copy str1 into str3
       str3 = str1;
       cout << "str3 : " << str3 << endl;

       // concatenates str1 and str2
       str3 = str1 + str2;
       cout << "str1 + str2 : " << str3 << endl;

       // total lenghth of str3 after concatenation
       len = str3.size();
       cout << "str3.size() :  " << len << endl;

       return 0;
    }

當上述代碼被編譯執(zhí)行時,它將產生如下結果:

    str3 : Hello
    str1 + str2 : HelloWorld
    str3.size() :  10
上一篇:信號處理下一篇:數(shù)據(jù)抽象