Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.

Similar presentations


Presentation on theme: "C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator."— Presentation transcript:

1 C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator ● string constructor (#, character) ● + concatenation operator on strings ● size member function

2 #include ● Provides access to the standard string library. ● Look in includes for reference ● on esus: /usr/include/cxx ● on linux: /usr/include/g++-3 (my 7.1 RedHat) ● A better way to look up the standard library is in a reference book such as "The C++ Standard Library, A Tutorial and Reference" by Nicolai M. Josuttis Addision/Wesley, 1999 ISBN - 0-201- 37926-0

3 string name; ● Instantiates a local variable (named object) of type string. Can be anywhere before it's used. ● Gains access to the interface, (the member functions (methods) or operations of the string type. ● This definition uses the default constructor and generates a null string. ● Scope is only for this function (main) and is destroyed when leaving the scope.

4 cin >> name; ● Read from the standard input into the data area of name. ● First it skips any whitespace characters (space, tab, backspace, or end of the line). ● Next it reads any characters into name until it encounters another whitespace character or end-of- file. ● The value of the expression is cin. ● Tries to flush properly to display previous couts.

5 const string greeting = "Hello, " + name + "!"; ● const promises that we will never change this variable again. ● const definitions MUST be initialized when defined. ● This definition can use the values of previous variables as it does here. ● + is a concatenation operator within the string object. ("x"+name, name+"x", name+name, but NOT "x"+"x"); ● Example of overloading the + operator.

6 const string spaces(greeting.size(), ' '); ● Another instantiation of a object of type string. ● This object is called spaces. ● In this example, a different constructor is called. ● (num, character) returns num characters. ● greeting.size() is a member function of the greeting object which returns an integer number for the length of the string stored in greeting. ● Result is a set of spaces the same size as greeting.

7 CHARACTER LITERALS ● In the previous constructor (num character) we used the character literal ' ' for a single character that is a blank. ● Distinct from the string literal " ". ● The type of ' ' is char. ● The type of " " is an array of const char with one more element than the number of characters in the literal (the '\0' null character).

8 DETAILS ● char : built-in type that holds ordinary characters defined by the implementation. ● wchar_t: built-in type that holds "wide characters" which are big enough to hold characters for languages such as Japanese. ● Review other examples of constructors and other member functions on page 15.

9 STRING CONSTRUCTORS ● string s; creates the empty string s ● string s(str); creates a string as a copy of the existing string str. ● string s(str, stridx); creates a string s that is initialized by the characters of string str starting with index stridx. ● string(str, stridx, strlen); creates a string s that is initialized by, at most, strlen characters of string str starting with index stridx.

10 STRING CONSTRUCTORS (cont.) ● string s(cstr); creates a string s that is initialized by the C-string cstr. ● string s(chars, chars_len); creates a string s that is initialized by chars_len characters of the character array chars. ● string s(num c); creates a string that has num occurrences of character c. ● string s(beg,end); creates a string that is initialized by all characters of the range [beg,end].

11 STRING DESTRUCTOR ● s.~string(); destroys all characters and frees the memory.

12 STRING MEMBER FUNCTIONS (METHODS) ● =,assign() Assign a new value ● swap() Swaps values between two strings ● +=, append(), push_back() Appends characters ● insert() Inserts characters ● erase() Erase characters ● clear() Remove all characters (makes it empty) ● resize() Changes the number of characters (deletes or appends characters at the end)

13 STRING MEMBER FUNCTIONS (cont.) ● replace() Replaces characters ● + Concatenates strings ● ==, !=,, >=, compare() Compare strings ● size(), length() Return the number of characters ● max_size() Returns the maximum possible number of characters ● empty() Returns whether the string is empty ● capacity() Returns the number of characters that can be held without reallocation.

14 STRING MEMBER FUNCTIONS (cont.) ● reserve() Reserves memory for a certain number of characters ● [], at() Access a character ● >>, getline() Read the value from a stream ● << Writes the value to a stream ● copy() Copies or writes the contents to a C-string ● c_str() Returns the value as C-string

15 STRING MEMBER FUNCTIONS (cont.) ● data() Returns the value as character array ● substr() Returns a certain substring ● find functions Search for a certain substring or character ● begin(), end() Provide normal iterator support ● rbegin(), rend() Provide reverse iterator support ● get_allocator() Returns the allocator


Download ppt "C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator."

Similar presentations


Ads by Google