Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer 2003 1 Chapter 15 - Class string and String Stream.

Similar presentations


Presentation on theme: " 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer 2003 1 Chapter 15 - Class string and String Stream."— Presentation transcript:

1  2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer 2003 1 Chapter 15 - Class string and String Stream Processing Outline 15.1 Introduction 15.2 string Assignment and Concatenation Outline 21.1 Introduction to the Standard Template Library (STL) 21.1.1 Introduction to Containers 21.1.2 Introduction to Iterators Chapter 21 - Standard Template Library (STL)

2  2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer 2003 2 15.1 Introduction Template class basic_string –Provides string manipulation (copying, searching, etc.) typedef basic_string string; Creates the alias string for basic_string –To use string, include the header file string initialization string s1( "Hello" ); string s2( 8, 'x' ); // 8 'x' characters string month = "March" Implicitly calls constructor

3  2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer 2003 3 15.1 Introduction 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 a single char to a string object is permissible if declared as s = 'n';

4  2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer 2003 4 15.1 Introduction string features –Not necessarily null terminated –length member function: s1.length() –Use [] to access individual characters: s1[0] 0 to length-1 –Many member functions take start position and length If length argument too large, max chosen –Stream extraction and getline operator are overloaded cin >> stringObject; getline( cin, s) –Reads an input from the keyboard, delimited by a newline

5  2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer 2003 5 15.2 string Assignment and Concatenation Examples of member functions: 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];

6  2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer 2003 6 15.2 string Assignment and Concatenation Range checking s3.at( index ); Returns character at index 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

7  2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer 2003 7 21.1 Introduction to the Standard Template Library (STL) STL –Powerful, template-based components Containers: template data structures Iterators: like pointers, access elements of containers Algorithms: data manipulation, searching, sorting, etc. –Object- oriented programming: reuse, reuse, reuse –Only an introduction to STL, a huge class library

8  2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer 2003 8 21.1.1 Introduction to Containers Three types of containers –Sequence containers Linear data structures (vectors, linked lists) Referred to as a first-class container –Associative containers Non-linear, can find elements quickly Key/value pairs Referred to as a first-class container –Container adapters stacks (allows a program to view a container) Near containers –Similar to containers, with reduced functionality Containers have some common functions

9  2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer 2003 9 STL Container Classes (Fig. 21.1) Sequence containers vector deque list Associative containers set multiset map multimap Container adapters stack queue priority_queue

10  2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer 2003 10 Common STL Member Functions (Fig. 21.2) Member functions for all containers –Default constructor, copy constructor, destructor empty max_size, size = >= == != swap Functions for first-class containers begin, end rbegin, rend erase, clear

11  2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer 2003 11 21.1.2 Introduction to Iterators Iterator objects are similar to pointers –Point to first element in a container –Iterator operators same for all containers * dereferences ++ points to next element begin() returns iterator to first element end() returns iterator to last element –Use iterators with sequences (also called ranges) such as Containers Input sequences: istream_iterator Output sequences: ostream_iterator

12  2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer 2003 12 21.1.2 Introduction to Iterators Usage std::istream_iterator inputInt( cin ) Can read int input from cin *inputInt –Dereference to read first int from cin ++inputInt –Go to next int in stream std::ostream_iterator outputInt(cout) Can output int s to cout *outputInt = 7 –Outputs 7 to cout ++outputInt –Advances iterator so we can output next int

13  2003 Prentice Hall, Inc. All rights reserved. Outline 13 fig21_05.cpp (1 of 2) 1 // Fig. 21.5: fig21_05.cpp 2 // Demonstrating input and output with iterators. 3 #include 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 #include // ostream_iterator and istream_iterator 10 11 int main() 12 { 13 cout << "Enter two integers: "; 14 15 // create istream_iterator for reading int values from cin 16 std::istream_iterator inputInt( cin ); 17 18 int number1 = *inputInt; // read int from standard input 19 ++inputInt; // move iterator to next input value 20 int number2 = *inputInt; // read int from standard input 21 Note creation of istream_iterator. For compilation reasons, we use std:: rather than a using statement. Access and assign the iterator like a pointer.

14  2003 Prentice Hall, Inc. All rights reserved. Outline 14 fig21_05.cpp (2 of 2) fig21_05.cpp output (1 of 1) 22 // create ostream_iterator for writing int values to cout 23 std::ostream_iterator outputInt( cout ); 24 25 cout << "The sum is: "; 26 *outputInt = number1 + number2; // output result to cout 27 cout << endl; 28 29 return 0; 30 31 } // end main Enter two integers: 12 25 The sum is: 37 Create an ostream_iterator is similar. Assigning to this iterator outputs to cout.

15  2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer 2003 15 Iterator Categories (Fig. 21.6) Input –Read elements from container, can only move forward Output –Write elements to container, only forward Forward –Combines input and output, retains position –Multi-pass (can pass through sequence twice) Bidirectional –Like forward, but can move backwards as well Random access –Like bidirectional, but can also jump to any element

16  2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer 2003 16 Iterator Types Supported (Fig. 21.8) Sequence containers vector : random access deque : random access list : bidirectional Associative containers (all bidirectional) set multiset Map multimap Container adapters (no iterators supported) stack queue priority_queue


Download ppt " 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer 2003 1 Chapter 15 - Class string and String Stream."

Similar presentations


Ads by Google