1 / 16

Chapter 15 - Class string and String Stream Processing

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.

paley
Télécharger la présentation

Chapter 15 - Class string and String Stream Processing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. 15.1 Introduction • Template class basic_string • Provides string manipulation (copying, searching, etc.) typedef basic_string< char > string; • Creates the alias string for basic_string< char > • To use string, include the header file <string> • string initialization string s1( "Hello" ); string s2( 8, 'x' );// 8 'x' characters string month = "March" • Implicitly calls constructor

  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. 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. 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. 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. 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. 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. STL Container Classes (Fig. 21.1) • Sequence containers vector deque list • Associative containers set multiset map multimap • Container adapters stack queue priority_queue

  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. 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. 21.1.2 Introduction to Iterators • Usage std::istream_iterator< int > 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< int > outputInt(cout) • Can output ints to cout *outputInt = 7 • Outputs 7 to cout ++outputInt • Advances iterator so we can output next int

  13. 1 // Fig. 21.5: fig21_05.cpp 2 // Demonstrating input and output with iterators. 3 #include <iostream> 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 #include <iterator> // 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< int > 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. fig21_05.cpp(1 of 2) Access and assign the iterator like a pointer.

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

  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. 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

More Related