1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補充 : C++ string.

Slides:



Advertisements
Similar presentations
Starting Out with C++, 3 rd Edition 1 Chapter 10 – Characters, Strings, and the string Class.
Advertisements

LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
C Program Design Supplements 主講人:虞台文. Content Who takes the return value of main() ?
學習C++的基本語法 認識關鍵字與識別字的不同 學習程式碼偵錯的流程 學習如何提高程式的可讀性
What is static?. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1; this.instanceX =
1 Web of Science 利用指引 單元二 瀏覽與處理查詢結果. 2 瀏覽檢索結果 查出的結果,預設以時間排列, 使用者可改變結果的排列方式: 還可以依被引用次數、相關度、 第一作者、刊名、出版年等排序 回到前先查的結果畫面 點選想看資料的完整書目 本館訂購範圍的期刊 全文,便可直接連結.
五小專案 黃詩晴 章乃云. 目錄 計算機 智慧盤 拼圖 記憶大挑戰 數學題庫 心得 參考文獻.
邏輯設計 題目:3_8解碼器 姓名:李國豪 學號:B09322001.
1 Introduction to Java Programming Lecture 7 Flow Control : Boolean expressions and the if statement.
MS-DOS 作業系統 張基昇 (Microsoft-Disk Operating System).
程式註解說明. 2 程式註解格式 塊狀註解 對檔案、 class 、 method 、資料結構、一段程式 …. 等程式區塊 做說明。 第一行的開頭必需為 “/*” 且沒有其他文字,最後一行的開頭 必需以 “*/” 做為結束,在中間每一行的開頭都必需是一個 “*” 。 單行註解 佔據一整行的說明。 以.
1 Introduction to Java Programming Lecture 4 Using JOptionPane Spring 2008.
計算機概論實習 How to Use string Template class basic_string String manipulation (copying, searching, etc.) typedef basic_string string; Also typedef.
7 陣列與搜尋 7.1 陣列 一般資料變數 宣告一維陣列 起始一維陣列 7-4
: Problem A : MiniMice ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11411: Problem A : MiniMice 解題者:李重儀 解題日期: 2008 年 9 月 3 日 題意:簡單的說,題目中每一隻老鼠有一個編號.
Introduction to C Language ─ C 語言資料型態. 大綱 常數與變數 整數資料型態與變數宣告 浮點數資料型態與變數宣告 字元資料型態與變數宣告 資料的大小 Homework.
下載 Dev C++ Compiler. 網址 ding.php?groupnamehttp://sourceforge.net/project/downloa ding.php?groupname=dev-cpp & filename=devcpp _setup.exe.
從此處輸入帳號密碼登入到管理頁面. 點選進到檔案管理 點選「上傳檔案」上傳資料 點選瀏覽選擇電腦裡的檔案 可選擇公開或不公開 為平台上的資料夾 此處為檔案分類,可顯示在展示頁面上,若要參加 MY EG 競賽,做品一律上傳到 “ 98 MY EG Contest ” 點選此處確定上傳檔案.
程式設計 Visual Basic 6.0 Visual Basic 6.0 Visual Basic 6.0 程式設計 Visual Basic 6.0 Visual Basic 6.0 Visual Basic 6.0許翠婷
資料結構實習-一 參數傳遞.
參考書籍:古頤榛, Visual C++ 6教學範本 , 碁峰資訊股份有限公司。
Hung-Hsiang WuWindows Processing Design1 Chapter 4 簡單視窗程式 表單與標籤的屬性 按鈕與編輯的屬性 設計簡單的應用程式 表單常用事件 一般鍵盤及滑鼠常見的事件 表單視窗間的呼叫 表單間資料傳送應用 專案選擇設定.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2008.
4 堆疊與佇列 4.1 前言 四種基本的資料結構 (可儲存資料的容器) 陣列 (Array)、串列(List): 最基本
Management Abstracts Retrieval System; MARS 檢索操作.
資料結構實習-二.
: Automatic correction of misspellings ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11048: Automatic correction of misspellings 解題者:陳宜佐 解題日期:
Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式.
File I/O 範例講解 授課:林哲嘉 日期: 2009/04/29. 範例:上機考第三題 Add 部分 1. 將檔案的資料顯示在畫面 2. 將要加入檔案加到資料的尾端.
H 猜猜我幾歲 遊戲規則: user 猜 → 電腦誠實回答 可重複猜、直到答對 程式流程 程式功能,示範 隨機產生一個數字 ( 整數, 1,2,3,4..., 23.6) 給提示並要求輸入資料 比較 顯示結果:太少、太多、答對 重複邀請猜、直到答對 電腦模擬.
資料結構實習-十 Binary Tree Traversal.
: Function Overloading ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11032:Function Overloading 解題者:許智祺 解題日期: 2007 年 5 月 8 日 題意:判對輸入之數字是否為.
1 Introduction to Java Programming Lecture 4 Using JOptionPane Spring 2010.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2009.
String and Text I/O Outline The String Class Immutable Strings and Interned Strings String Comparisons String Length, Characters,
2 C++ 程式概論 2.1 C++ 程式結構 程式註解 // 插入標題檔 #include 2-3
3 數學運算 3.1 鍵盤輸入 鍵盤輸入 輸入函數 cin 輸入函數 cin 多重輸入 cin 多重輸入 cin 輸出格式化 輸出格式化 設定輸出長度 setw.
中序轉後序 藉由由左向右掃瞄中序運算式產生後序運算式,遇到 運算元就直接輸出,遇到運算符號則先存入堆疊,將 優先權較高者輸出。 範例: a + b * c TokenStack [0] [1] [2] topoutput aa ++0a b+0ab *+ *1ab c+ *1abc eosabc*+
蔡中皓 郭尚豪 紀羽軒 1. Outline Background Motive and purpose Method Conclusion 2.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Java class Outline Defining a method Calling Method Passing parameters [Sample code] TestMethod.java 、 TestMethod2.java 、 GCD.java 、 prime.java.
I/O of C and C++. printf and scanf of C Should add printf (“control string”, exp1, exp2,…) Scanf(“control string”, &var1, &var2,…)
C++ standard strings. Standard library of C++ language STL STL (The main part of standard library of C++ language) Stream classes Stream classes String.
ADO.NET. ADO.NET 精靈化設計 精靈化設計 程式化設計 程式化設計 SQL 指令 SQL 指令 DataGrid, DataList DataGrid, DataList DataBind DataBind.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
String Class Mohamed Shehata 1020: Introduction to Programming.
SMIE-121 Software Design II School of Mobile Information Engineering, Sun Yat-sen University Lecture.
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
C/C++ Basics (VI) Strings and Vectors Berlin Chen 2003 Textbook: 1. Walter Savitch, “Absolute C++,” Addison Wesley, 2002 開發代理 2. Walter Savitch, “Problem.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
SEI PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2 nd Edition Copyright 2007 © SEI, All rights reserved Object-Oriented.
C++ 程式語言. 2 C++ 程式語言 – 大綱 1. 指標 2. 結構與類別 3. 標準函式庫 (STL)
計算機程式 第十二單元 Operator Overloading II 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to.
13. string Operations Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Accessing String Elements.
1 7.7Pointer Expressions and Pointer Arithmetic Arithmetic operations can be performed on pointers –Increment/decrement pointer ( ++ or -- ) –Add an integer.
1 Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character Handling Library 8.4String Conversion.
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Learning Objectives String Class.
10.1 Character Testing.
String class and its objects
Today’s Objectives 28-Jun-2006 Announcements
Programming Strings.
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補充 : C++ string Class

2 String Class Constructors

3 String Class Overloaded Operators a (1/2)

4 String Class Overloaded Operators a (2/2)

5 /* Program G-1 String example program with several string constructors and overloaded operators */ #include //C++ header style since using C++ string class #include using namespace std; int main() { string S1("What a great day."), S2, S3; char MyOldString[25] = "Howdy Partner"; string S4(MyOldString); S2 = "Do you love C++?";//assignment S3 = S1 + S2; //Write the initial string values cout << "\n S1 = " << S1; cout << "\n S2 = " << S2; cout << "\n S3 = " << S3; cout << "\n S4 = " << S4; cout << "\n MyOldString = " << MyOldString << endl; if(S1 > S2)cout << "\n S1 is greater than S2"; else cout << "\n S1 is not greater than or the same as S2"; S1 = "\nAll done with strings."; cout << S1 << endl; return 0; }

6

7 String Editing Functions 常用的編輯字串指令 應改成 8 才對

8 // Program G-2 String Class Example Program using Editing Functions #include //C++ header style since using C++ string class #include using namespace std; int main() { string S1("It is a rainy day."); string S2("very "); string S3("sunny "); //Write the initial string values cout << "\n S1 = " << S1; cout << "\n S2 = " << S2; cout << "\n S3 = " << S3; // Insert "very " into S1 S1.insert(8,S2); cout << "\n S1 = " << S1; // Replace "rainy" with "sunny" in S1 S1.replace(13, 6, S3); cout << "\n S1 = " << S1; // Erase "very" from S1 S1.erase(8, 5); cout << "\n S1 = " << S1; S1 = "\nAll done with strings."; cout << S1 << endl; return 0; }

9

10 String Searching Functions 常用的字串搜尋指令

11 // Program G-3 String Class Example Program using Search Functions #include //C++ header style since using C++ string class #include using namespace std; int main() { string S1("One potato, two potato, three potato, four."); string S2("potato"); int First_pos, Last_pos; //Write the initial string values cout << "\n S1 = " << S1; cout << "\n S2 = " << S2; // Search S1 for "potato" from start of the string First_pos = S1.find(S2,0); cout << "\n The first \"potato\" is at " << First_pos; // string::npos is defined as the length of the string Last_pos = S1.rfind(S2,string::npos); cout << "\n The last \"potato\" is at " << Last_pos; S1 = "\nAll done with strings."; cout << S1 << endl; return 0; }

12

13 Miscellaneous String Functions (1/2)

14 Miscellaneous String Functions (2/2)

15 Conversion between Char Arrays and Strings // Program G-4 Convert Between Strings and Character String Arrays #include //C++ header style since using C++ string class #include using namespace std; int main() { string S1("I like the sea and C, you see."); string S2; char MyArray[50] = "Is C++ really a B-?"; const char *MyOtherArray; cout << "\n The original string and array" << endl; cout << S1 << endl; cout << MyArray << endl; //First we place MyArray contents into S2 using the assign S2.assign(MyArray); //Next, we'll stuff the contents of S1 into MyOtherArray MyOtherArray = S1.c_str(); //convert a string to a const char* cout << "\n The converted array and string" << endl; cout << MyOtherArray << endl; cout << S2 << endl; return 0; }

16 Reading into a String (e.g., from the Keyboard) To read text with whitespace characters –Must use get or getline and then assign the contents into a string // Program G-5 Reading into a String #include using namespace std; int main() { string S1; char MyArray[50]; cout << "\n Enter your favorite string saying " << endl; cin.getline(MyArray,50); //Assign the array contents into S1 S1.assign(MyArray); cout << "\n Here is your saying twice! "<< endl; cout << MyArray << endl; cout << S1 << endl; return 0; }

練習 撰寫程式從鍵盤依序讀入若干個名字,迨使用者輸入 空字串則停止讀入,隨即依姓名進行排序並顯示結果 17