Presentation is loading. Please wait.

Presentation is loading. Please wait.

13 C++字串 字串與數值轉換函數 13.1 C++字串類別 建立C++字串 13-2

Similar presentations


Presentation on theme: "13 C++字串 字串與數值轉換函數 13.1 C++字串類別 建立C++字串 13-2"— Presentation transcript:

1 13 C++字串 字串與數值轉換函數 13.1 C++字串類別 13-2 13.1.1 建立C++字串 13-2

2 字串與數值轉換函數 字串形式的數值(“ ”)是不能當做算數運算的資料,所以C++ 提供字串與數值間的轉換函數,例如字串轉成浮點數(atof)、字串轉成整數(atoi)、字串轉成長整數(atol)等函數,以及整數轉成字串(itoa)的函數。

3 轉成浮點數值atof #include <cstdlib> atof (字串) 範例
s = " E-25 "; //定義字串 x = atof( s ); //轉浮點數x= e-22

4 轉成整數值atoi #include <cstdlib> atoi (字串) 範例
s = " 686 pigs "; //定義字串 i = atoi( s ); //轉換成短整數,i=686

5 轉成長整數值atol #include <cstdlib> atol (字串) 範例
s = " dollars"; //定義字串 l = atol( s ); //轉換成長整數,l=138686

6 整數轉成字串itoa #include <cstdlib> itoa (數值, 字串, 基底) 範例
char intArray[10]; itoa(1234, intArray, 8); //1234轉成字串"2322" itoa(1234, intArray, 10); //1234轉成字串"1234" itoa(1234, intArray, 16); //1234轉成字串"4d2"

7 13.4 C++字串類別 C++ 字串類別是一個抽象的資料型態,它不是C++ 原本內建的資料型態,如int或char。C++ 字串類別與字串類別函數是定義於C++ 的新型標題檔(不含 .h的標題檔)中,而C型態的字串標題檔(string.h)並沒有定義這些函數。所以使用這些函數以前,必須插入C++ 新型的標題檔(string)。 在插入C++ 新型標題檔後(例如:#include <iostream>),必須加入(using namespace std;)敘述,來宣告程式中的函數是使用新型的C++ 型態標題檔,而不是使用舊型的C型態標題檔。

8 13.4.1 建立C++字串 #include <string> using namespace std;

9 13.4.1 建立C++字串 (續) 範例:C 型態字串 char *name = "JOHN";
string s1; //宣告s1 string s2("JOHN ARCHER"); //s2 = "JOHN ARCHER" string s3 = "MARY ARCHER"; //s3 = "MARY ARCHER" string s4("A", 4); //宣告s4 = "AAAA" string s5(s2); //宣告s5 = "JOHN ARCHER" string s6(s2, 0, 4); //宣告s6 = "JOHN"

10 13.4.2 輸入C++字串 #include <iostream> using namespace std;
getline (cin, 字串物件) 範例 string s; getline(cin, s); //假設輸入"Hello world!" cout << s; //輸出Hello world!

11 13.4.3 C++字串運算符號 運算符號 功能說明 = 指定資料 + 串接字串 += 連接並指定字串 == 相等 != 不等 <
小於 <= 小於等於 > 大於 >= 大於等於 [] 註標 << 輸出 >> 輸入

12 13.4.3 C++字串運算符號 (續) 指定資料 範例 string s1, s2("Hello");
s1 = s2; //s1="Hello" s1 = "Hello world!"; //s1="Hello world!"

13 13.4.3 C++字串運算符號 (續) 串接字串 範例 string s1("Hello"), s2(" world"), s3;
s3 = s1 + s2; //s3="Hello world" string s4(s3 + "!"); //s4="Hello world!" s1 += "!"; //s1="Hello!"

14 C++字串運算符號 (續) 比較字串 範例 string s1("ANSI/ISO C++"), s2("Visual C++"); if(s1 == s2) cout << "s1 == s2"; else cout << "s1 != s2";

15 13.4.4 C++ 字串陣列 #include <string> using namespace std;
範例 string s1[] = {"Java", "Assembly", "Delpha", "Basic", "Fortran", "Cobol"};

16 13.4.5 C++字串類別成員 成員函數 功能 s1.append(s2) 連接字串 s1.append(s2, 起始位置, 字串長度)
s1.assign(s2) 指定字串 s1.assign(s2, 起始位置, 字串長度) s1.at(位置) 存取指定位置 s1.capacity() 取得字串容量 s1.clear() 清除字串全部內容 s1.compare(s2) 比較字串 s1.compare(s1起點, s1長度, s2, s2起點, s2長度) s1.copy(s2, 起始位置, 字串長度) 複製字串

17 13.4.5 C++字串類別成員 (續) 成員函數 功能 s1.erase(起始位置, 清除長度) 清除字串部分內容 s1.find(s2)
找尋字串 s1.find(s2, 起始位置) s1.insert(起始位置, s2) 插入字串 s1.length() 取得字串長度 s1.max_size() 取得字串最大長度 s1.replace(起始位置, 字串長度, s2) 取代部分字串 s1.size() 取得字串大小 s1.substr(起始位置, 字串長度) 找尋部分字串 s1.swap(s2) 對調字串

18 13.4.5 C++字串類別成員 (續) 指定資料 範例一 string s1, s2("Hello world!");
s1.assign(s2); //s1="Hello world!“ 範例二 string s1("Hello world!"), s2; s2.assign(s1, 6, 5); //s2 = "world" s2[2] = 'u'; //s2 = "would"

19 C++字串類別成員 (續) 串接字串 範例 string s1("Hello"), s2("wi"), s3(" world!"); s1.append(s3); //s1 = "Hello world!" s2.append(s3, 4, 2); //s2 = "wild"

20 C++字串類別成員 (續) 比較字串 範例 string s1("ANSI/ISO C++"), s2("Visual C++"); cout << s1.compare(13, 3, s2, 7, 3) << endl; //等於0表示相等 cout << s1.compare(0, 8, s2, 0, 8) << endl; //不等於0表示不相等

21 13.4.5 C++字串類別成員 (續) 取得子字串 範例 string s1("Hello world!"), s2;
s2 = s1.substr(6, 5); //s2 = world

22 C++字串類別成員 (續) 對調字串 範例 string s1("Visual C++"), s2("ANSI/ISO C++"); s1.swap(s2); //s1與s2對調 cout << s1 << endl; //顯示ANSI/ISO C++ cout << s2 << endl; //顯示Visual C++

23 13.4.5 C++字串類別成員 (續) 找尋字串 範例 string s1("ANSI/ISO C++"), s2("C++");
int p; p = s1.find(s2); // p = 7 (找到再位置7) p = s1.find("Visual"); // p = -1 (找不到)

24 13.4.5 C++字串類別成員 (續) 取代字串 範例 string s("call by reference");
int p = s.find(" "); while(p < string::npos) // 若不等於字串結尾則繼續 { s.replace(p, 1, "-"); // 找到後以"–"取代" " p = s.find(" ", p++); // 找尋下一個空白 }

25 13.4.5 C++字串類別成員 (續) 插入字串 範例 string s1("ANSI C++"), s2("ISO");
s1.insert(4, s2); //s1=ANSIISO C++ s1.insert(4, "/"); //s1=ANSI/ISO C++

26 13.4.5 C++字串類別成員 (續) 其他 範例 string s1("ANSI/ISO C++");
cout << s1.length() << endl; //12 cout << s1.size() << endl; //12 cout << s1.capacity() << endl; //12 cout << s1.max_size() << endl; // cout << s1.at(10) << endl; //'C'


Download ppt "13 C++字串 字串與數值轉換函數 13.1 C++字串類別 建立C++字串 13-2"

Similar presentations


Ads by Google