![Page 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](https://reader035.vdocument.in/reader035/viewer/2022072006/56649f4c5503460f94c6cd65/html5/thumbnails/1.jpg)
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
Outline15.1 Introduction15.2 string Assignment and Concatenation
Outline21.1 Introduction to the Standard Template Library (STL)
21.1.1 Introduction to Containers21.1.2 Introduction to Iterators
Chapter 21 - Standard Template Library (STL)
![Page 2: 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](https://reader035.vdocument.in/reader035/viewer/2022072006/56649f4c5503460f94c6cd65/html5/thumbnails/2.jpg)
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< char > string;• Creates the alias string for basic_string< char >
– To use string, include the header file <string>
• string initializationstring s1( "Hello" );
string s2( 8, 'x' ); // 8 'x' characters
string month = "March"• Implicitly calls constructor
![Page 3: 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](https://reader035.vdocument.in/reader035/viewer/2022072006/56649f4c5503460f94c6cd65/html5/thumbnails/3.jpg)
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 ass = 'n';
![Page 4: 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](https://reader035.vdocument.in/reader035/viewer/2022072006/56649f4c5503460f94c6cd65/html5/thumbnails/4.jpg)
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 overloadedcin >> stringObject;
getline( cin, s)– Reads an input from the keyboard, delimited by a newline
![Page 5: 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](https://reader035.vdocument.in/reader035/viewer/2022072006/56649f4c5503460f94c6cd65/html5/thumbnails/5.jpg)
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: assignments2 = 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 characterss2[0] = s3[2];
![Page 6: 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](https://reader035.vdocument.in/reader035/viewer/2022072006/56649f4c5503460f94c6cd65/html5/thumbnails/6.jpg)
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 checkings3.at( index );
• Returns character at index
• Concatenations3.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
![Page 7: 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](https://reader035.vdocument.in/reader035/viewer/2022072006/56649f4c5503460f94c6cd65/html5/thumbnails/7.jpg)
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
![Page 8: 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](https://reader035.vdocument.in/reader035/viewer/2022072006/56649f4c5503460f94c6cd65/html5/thumbnails/8.jpg)
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
![Page 9: 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](https://reader035.vdocument.in/reader035/viewer/2022072006/56649f4c5503460f94c6cd65/html5/thumbnails/9.jpg)
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 containersvectordequelist
• Associative containerssetmultisetmapmultimap
• Container adaptersstackqueuepriority_queue
![Page 10: 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](https://reader035.vdocument.in/reader035/viewer/2022072006/56649f4c5503460f94c6cd65/html5/thumbnails/10.jpg)
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 containersbegin, end
rbegin, rend
erase, clear
![Page 11: 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](https://reader035.vdocument.in/reader035/viewer/2022072006/56649f4c5503460f94c6cd65/html5/thumbnails/11.jpg)
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
![Page 12: 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](https://reader035.vdocument.in/reader035/viewer/2022072006/56649f4c5503460f94c6cd65/html5/thumbnails/12.jpg)
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
• Usagestd::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
![Page 13: 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](https://reader035.vdocument.in/reader035/viewer/2022072006/56649f4c5503460f94c6cd65/html5/thumbnails/13.jpg)
2003 Prentice Hall, Inc.All rights reserved.
Outline13
fig21_05.cpp(1 of 2)
1 // Fig. 21.5: fig21_05.cpp2 // 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_iterator10 11 int main()12 {13 cout << "Enter two integers: ";14 15 // create istream_iterator for reading int values from cin16 std::istream_iterator< int > inputInt( cin ); 17 18 int number1 = *inputInt; // read int from standard input19 ++inputInt; // move iterator to next input value20 int number2 = *inputInt; // read int from standard input21
Note creation of istream_iterator. For compilation reasons, we use std:: rather than a using statement.
Access and assign the iterator like a pointer.
![Page 14: 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](https://reader035.vdocument.in/reader035/viewer/2022072006/56649f4c5503460f94c6cd65/html5/thumbnails/14.jpg)
2003 Prentice Hall, Inc.All rights reserved.
Outline14
fig21_05.cpp(2 of 2)
fig21_05.cppoutput (1 of 1)
22 // create ostream_iterator for writing int values to cout23 std::ostream_iterator< int > outputInt( cout ); 24 25 cout << "The sum is: ";26 *outputInt = number1 + number2; // output result to cout27 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.
![Page 15: 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](https://reader035.vdocument.in/reader035/viewer/2022072006/56649f4c5503460f94c6cd65/html5/thumbnails/15.jpg)
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
![Page 16: 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](https://reader035.vdocument.in/reader035/viewer/2022072006/56649f4c5503460f94c6cd65/html5/thumbnails/16.jpg)
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 containersvector: random accessdeque: random accesslist: bidirectional
• Associative containers (all bidirectional)setmultisetMapmultimap
• Container adapters (no iterators supported)stackqueuepriority_queue