Download presentation
Presentation is loading. Please wait.
1
計算機概論實習 2007-05-18
2
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
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
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
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
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
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
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
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
10 Example see CompString_1.cpp
11
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
12 Practice (P6) 要求使用者輸入學生之姓名,和國文、英文及數學三項 成績。使用者輸入之格式為一字串: “Kuo-Zhe Chiou, 100, 40, 40” 請處理此字串,將姓名以及成績分別配置到參數之中。 Example output: 學生姓名: Kuo-Zhe Chiou 國文成績: 100 英文成績: 40 數學成績: 40
13
basic_string Members & operators http://msdn.microsoft.com/library/default.asp
14
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
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
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
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
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
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.