C++學習(九)入門篇——String類

來源:互聯網
上載者:User

標籤:入門   nts   alt   yield   返回   對象   assign   stream   ast   

可以用string類而不是字元數組來儲存字串,string更加簡單

要是用string類,則要在程式中包含標頭檔string,且位於std名稱空間中,string類隱藏了字串的數組性質,可以像處理普通變數那樣處理字串

程式清單4.7  strtype1.cpp//strtypel.cpp - - using the C++ string class#include<iostream>#include<string>int main(){    using namespace std;    char charr1[20];    char charr2[20] = "jaguar";    string str1;    string str2 = "panther";    cout << "Enter a kind of feline: ";    cin >> charr1;    cout << "Enter another kind of feline: ";    cin >> str1;    cin.get();    cout << "Here are some felines:\n";    cout << charr1 << " " << charr2 << " "        << str1 << " " << str2 << endl;    cout << "The third letter in " << charr2 << " is "        << charr2[2] << endl;    cout << "The third letter in " << str2 << " is "        << str2[2] << endl;    cin.get();}

輸出結果:

string對象聲明為簡單變數

char數組為一組用於儲存字串的char儲存單元,string類為一個儲存字串的實體

除了數組初始化可用列表初始化,也適用於字串初始化

char first_date[ ] ={"Le Chapon Dodu"};

string second_date={"Le Chapon Dodu"};

char third_date[ ] {"Le Chapon Dodu"};

string forth_date {"Le Chapon Dodu"};

 

不能將一個數組賦給另一個數組,但可以將一個string對象賦給另一個string對象

char charr1[20];char charr2[20] = "jaguar";string str1;string str2 = "panther";charr1=charr2;                      //不允許這麼做str1=str2;                             //可以

string類簡化了字串合併作業

還可以將兩個string對象合并起來

string str3;str3 = str1+str2;str1+=str2;
程式清單4.8 strtype2.cpp//strtype2.cpp - - assigning,adding,and appending#include<iostream>#include<string>int main(){    using namespace std;    string s1 = "penguin";    string s2, s3;    cout << "You can assign one string object to another: s2=s1\n";    s2 = s1;    cout << "s1:" << s1 << ",s2:" << s2 << endl;    cout << "You can assign a C-style string to a string object.\n";    cout << "s2 = \"buzzard\"\n";    s2 = "buzzard";    cout << "s2: " << s2 << endl;    cout << "You can concatenate strings:s3 = s2 + s1\n";    s3 = s2 + s1;    cout << "s3:" << s3 << endl;    cout << "You can append strings.\n";    s1 += s2;    cout << "s1 += s2 yields s1 = " << s1 << endl;    s2 += " for a day";    cout << "s2 += \"for a day\" yields s2 = " << s2 << endl;    cin.get();}

結果:

在C++新增string類前,還是要完成給字串賦值,用標頭檔cstring中的函數來完成,用函數strcpy()將字串複製到字元數組中,用strcat()將字串附加到字元數組末尾

strcpy(charr1,charr2);    //copy charr2 to charr1strcat(charr1,charr2);    //append contents of charr2 to charr1
程式清單4.9  strtype3.cpp//strtype3.cpp - - more string class features#include<iostream>#include<string>#include<cstring>int main(){    using namespace std;    char charr1[20];    char charr2[20] = "jaguar";    string str1;    string str2 = "panther";    str1 = str2;    strcpy_s(charr1,charr2);    str1 += " paste";    strcat_s(charr1, " juice");    int len1 = str1.size();    int len2 = strlen(charr1);    cout << "The string " << str1 << " contains "        << len1 << " characters.\n";    cout << "The string " << charr1 << " contains "        << len2 << " characters.\n";    cin.get();}

輸出:

函數strlen()是一個常規函數,返回該字串包含的字元數。

str1是一個對象,而size()是一個類對象,所以這樣調用str1.size()

 

C++學習(九)入門篇——String類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.