計算機概論實習 2007-05-18. 2 How to Use string Template class basic_string String manipulation (copying, searching, etc.) typedef basic_string string; Also typedef.

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
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.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Chapter 10.
Today’s Class Class string –The basics –Assignment –Concatenation –Compare & swap –Find –Conversion to C-style char * strings –Iterators.
 2006 Pearson Education, Inc. All rights reserved Class string and String Stream Processing.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Standard Template Library Ming.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
The Standard String Class Is actually a template: –typedef basic_string string This means you can have strings of things other than chars.
 2006 Pearson Education, Inc. All rights reserved Class string and String Stream Processing.
計算機概論實習 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 12 More.
Computer Science 1620 Strings. Programs are often called upon to store and manipulate text word processors chat databases webpages etc.
1 Chapter 10 Characters, Strings, and the string class.
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages in Chapter 7, and pages in Chapter 8.  Homework #9 and Lab #9 due next week.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補充 : C++ string.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
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.
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
Characters, Strings, And The string Class Chapter 10.
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++
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
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.
CSC 270 – Survey of Programming Languages
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 15 - Class string and String Stream Processing Outline 15.1 Introduction 15.2 string Assignment.
String Class Mohamed Shehata 1020: Introduction to Programming.
Template is a declaration (similar to class declaration), which deals with generic types. Templates are evaluated to produce concrete classes when a template-base.
SMIE-121 Software Design II School of Mobile Information Engineering, Sun Yat-sen University Lecture.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
Arrays, Vectors, and Strings Allocation and referencing.
C++ Programming Basics
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
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.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Arrays, Character Strings and Standard Type String ~ Collection.
An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character.
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Chapter Characters, Strings, and the string class 10.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Strings, and the string Class. C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character The C-string.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
1 Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character Handling Library 8.4String Conversion.
Strings.
2008YeungNam Univ. SE Lab. 1  I n n amespace std : t ypedef basic_string string ; Type wchar_t for 16bit UNICODE. A. String near-containers  I.
Class string and String Stream Processing: A Deeper Look
C++ STRINGS string is part of the Standard C++ Library
10.1 Character Testing.
String class and its objects
Class string and String Stream Processing
Today’s Objectives 28-Jun-2006 Announcements
Chapter 15 - Class string and String Stream Processing
character manipulation
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

計算機概論實習

2 How to Use string Template class basic_string String manipulation (copying, searching, etc.) typedef basic_string string; Also typedef for wchar_t Include string initialization string s1( "Hello" ); string s2( 8, 'x' ); 8 'x' characters string month = "March" Implicitly calls constructor

3 Incorrect Usage No conversion from int or char The following definitions are errors string error1 = 'c'; string error2( 'u' ); string error3 = 22; string error4( 8 ); However, can assign to one char if declared s = 'n';

4 string Features Not necessarily null terminated length member function: s1.length() Use [] to access individual characters: s1[0] 0 to length-1 String not a pointer Many member functions take start position and length If length argument too large, max chosen Stream extraction cin >> stringObject; getline( cin, s) Delimited by newline

5 Example int main(){ string s1; cout << endl << "Input a sentence:(gotten by getline)"; getline(cin, s1); cout << "The input is: " << s1; } //cout << endl << "Input a sentence:(gotten directly)"; //cin >> s1; //cout << "The input is: " << s1; cout << endl << "Input a sentence:(gotten directly)"; cin >> s1; cout << "The input is: " << s1;

6 string Assignment and Concatenation Assignment s2 = s1; Makes a separate copy s2.assign(s1); Same as s2 = s1; myString.assign(s, start, N); Copies N characters from s, beginning at index start Individual characters s2[0] = s3[2];

7 string Assignment and Concatenation Range checking s3.at( index ); Returns character at index Can throw out_of_range exception [] has no range checking Concatenation s3.append( "pet" ); s3 += "pet"; Both add "pet" to end of s3 s3.append( s1, start, N ); Appends N characters from s1, beginning at index start

8 Comparing strings Overloaded operators ==, !=,, = Return bool s1.compare(s2) Returns positive if s1 lexicographically greater Compares letter by letter 'B' lexicographically greater than 'A' Returns negative if less, zero if equal “Test” > “Header” “ABC” < “AC”

9 s1.compare(start, length, s2, start, length) Compare portions of s1 and s2 s1.compare(start, length, s2) Compare portion of s1 with all of s2

10 Example see CompString_1.cpp

11 Substrings Function substr gets substring s1.substr( start, N ); Gets N characters, beginning with index start Returns substring Example: see SubString_1.cpp

12 Practice (P6) 要求使用者輸入學生之姓名,和國文、英文及數學三項 成績。使用者輸入之格式為一字串: “Kuo-Zhe Chiou, 100, 40, 40” 請處理此字串,將姓名以及成績分別配置到參數之中。 Example output: 學生姓名: Kuo-Zhe Chiou 國文成績: 100 英文成績: 40 數學成績: 40

basic_string Members & operators

14 appendAdds characters to the end of a string. assignAssigns new character values to the contents of a string. atReturns a reference to the element at a specified location in the string. basic_stringConstructs a string that is empty or initialized by specific characters or that is a copy of all or part of some other string object or C-string. beginReturns an iterator addressing the first element in the string. c_strConverts the contents of a string as a C-style, null-terminated, string. capacityReturns the largest number of elements that could be stored in a string without increasing the memory allocation of the string.

15 clearErases all elements of a string. compareCompares a string with a specified string to determine if the two strings are equal or if one is lexicographically less than the other. copyCopies at most a specified number of characters from an indexed position in a source string to a target character array. dataConverts the contents of a string into an array of characters. emptyTests whether the string is contains characters or not. endReturns an iterator that addresses the location succeeding the last element in a string.

16 eraseRemoves an element or a range of elements in a string from specified positions. findSearches a string in a forward direction for the first occurrence of a substring that matches a specified sequence of characters. find_first_not_ofSearches through a string for the first character that is not any element of a specified string. find_first_ofSearches through a string for the first character that matches any element of a specified string. find_last_not_ofSearches through a string for the last character that is not any element of a specified string.

17 find_last_ofSearches through a string for the last character that is an element of a specified string. get_allocatorReturns a copy of the allocator object used to construct the string. insertInserts an element or a number of elements or a range of elements into the string at a specified position. lengthReturns the current number of elements in a string. max_sizeReturns the maximum number of characters a string could contain. push_backAdds an element to the end of the string. rbeginReturns an iterator to the first element in a reversed string. rendReturns an iterator that points just beyond the last element in a reversed string.

18 replaceReplaces elements in a string at a specified position with specified characters or characters copied from other ranges or strings or C-strings. reserveSets the capacity of the string to a number at least as great as a specified number. resizeSpecifies a new size for a string, appending or erasing elements as required. rfindSearches a string in a backward direction for the first occurrence of a substring that matches a specified sequence of characters. sizeReturns the current number of elements in a string. substrCopies a substring of at most some number of characters from a string beginning from a specified position. swapExchange the contents of two strings.

19 Operators operator+=Appends characters to a string. operator=Assigns new character values to the contents of a string. operator[]Provides a reference to the character with a specified index in a string.