2008YeungNam Univ. SE Lab. 1  I n n amespace std : t ypedef basic_string<char> string ; Type wchar_t for 16bit UNICODE. A. String near-containers  I.

Slides:



Advertisements
Similar presentations
LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
Advertisements

M ANIPULATING S TRINGS Resources [1] Object Oriented Programming with C++ (3 rd Edition) E Balagurusamy [2] Teach Yourself C++ (3 rd Edition) H Schildt.
Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Definition of Strings Generally speaking, a string is a sequence of characters c string c++ string class Examples: “hello”, “high school”, “H2O”. Typical.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 19 - Class string and Stream Processing Outline 19.1Introduction 19.2 string Assignment and.
Today’s Class Class string –The basics –Assignment –Concatenation –Compare & swap –Find –Conversion to C-style char * strings –Iterators.
 2006 Pearson Education, Inc. All rights reserved Class string and String Stream Processing.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Standard Template Library Ming.
The Standard String Class Is actually a template: –typedef basic_string string This means you can have strings of things other than chars.
計算機概論實習 How to Use string Template class basic_string String manipulation (copying, searching, etc.) typedef basic_string string; Also typedef.
 2006 Pearson Education, Inc. All rights reserved Class string and String Stream Processing.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 12 More.
C++ standard strings. Standard library of C++ language STL STL (The main part of standard library of C++ language) Stream classes Stream classes String.
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
C++ String Class. Outline  String Initialization  Basic Operations  Comparisons  Substrings  Swapping Strings  String Size  Finding Strings and.
1 Principles of Computer Science I Honors Section Note Set 10 CSE 1341.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
Templates code reuse - inheritance - template classes template classes - a class that is not data-type specific - eg. a class of Array of any type - intArray,
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
C++ Streams © Bruce M. Reynolds & Cliff Green, C++ Programming Certificate University of Washington Cliff Green.
C arrays are limited: -they are represented by pointers (which may or may not be valid); -Indexes not checked (which means you can overrun your array);
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
Characters, Strings, And The string Class Chapter 10.
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
CSC 270 – Survey of Programming Languages
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 15 - Class string and String Stream Processing Outline 15.1 Introduction 15.2 string Assignment.
String Class Mohamed Shehata 1020: Introduction to Programming.
SMIE-121 Software Design II School of Mobile Information Engineering, Sun Yat-sen University Lecture.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Arrays, Vectors, and Strings Allocation and referencing.
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
STL string class Programming Team 2/15/2006. string constructors string() = empty string string(int l, char ch) = string of length l, of repeated ch string(char*
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Strings, and the string Class. C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character The C-string.
2008YeungNam Univ. SE Lab. 1 C++ views each file as a sequence of bytes terminated by EOF-marker. Header files Files are opened by creating objects of.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Strings.
CS212: Object Oriented Analysis and Design
C-Strings We have already seen that a C-string is a null-terminated array of characters.
CS Computer Science IA: Procedural Programming
Class string and String Stream Processing: A Deeper Look
Standard Template Library (STL)
Characters, C-Strings, and More About the string Class
Chapter 12: More on C-Strings and the string Class
Standard Version of Starting Out with C++, 4th Edition
Object Oriented Programming COP3330 / CGS5409
Engineering 1020: Introduction to Programming Fall 2018
C++ STRINGS string is part of the Standard C++ Library
Summary of what we learned so far
Programming with ANSI C ++
10.1 Character Testing.
String class and its objects
Containers and the Standard Template Library (STL)
Class string and String Stream Processing
String What it is Why it’s useful
Engineering Problem Solving with C++, Etter
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Standard Version of Starting Out with C++, 4th Edition
Today’s Objectives 28-Jun-2006 Announcements
Chapter 15 - Class string and String Stream Processing
character manipulation
A dictionary lookup mechanism
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

2008YeungNam Univ. SE Lab. 1  I n n amespace std : t ypedef basic_string<char> string ; Type wchar_t for 16bit UNICODE. A. String near-containers  I nstantiating the s tring objects: string s; // Length 0 string is created by default constructor. string a("Apr"); // from c onst char *, s ame as ‘ string a="Apr"; ’ string b( 8, '*' ); // string of 8 ' *' s.  N o E O S ' \0 ' is needed ( differs from C-like “ char * ” strings ): M F l ength() and s ize() tell the length of strings. s tring is not pointer : s is not equal to & s[0].  S tream extraction operator ‘ >> ’ for s tring : c in >> s; // delimited by ws g etline( cin, s ); // delimited by ' \n'  b asic_string template operators : [ ][ ] = + += = == = != < > <= >= >> <<

2008YeungNam Univ. SE Lab. 2A. String sass.cpp #include using std::string; #include void main() { string a("Cat"), b, c; b = a; // string assignment operator c.assign( a ); // string assignment function std::cout << a << b << c << '\n'; b[0] = c[2] = 'r'; // Operator ‘ [ ] ’ does not range check std::cout << a << b << c << '\n'; for ( int i=0; i<a.length(); ++i) std::cout << a.at(i); // MF ‘ at() ’ does range check: out_of_range std::cout << '\n'; string d(a+"Dog"), e; c += "pet"; // Overloaded operator ‘ += ’ a.append("acomb"); e.append(a, 4, a.size()); // appends substring a[4..] to e std::cout << a << b << c << d << e << '\n'; } string Assignment, Concatenation Cat ratCar Cat CatacombratCarpetCatDogcomb

2008YeungNam Univ. SE Lab. scomp.cpp 3 #include using std::string; #include void main() { string a("Cat"), b, c; if ( b = = "" ) b = a; // or ‘ if (b= =string("")) ….’ std::cout << b << '\n'; if ( b > "Cap" ) c = "Cap"; std::cout << c << '\n'; std::cout << b.compare(a) << c.compare(a) << '\n'; std::cout << a.compare(0, 1, c, 0, 1) << '\n'; -- a[0..1] : c[0..1] c += "tain"; std::cout << c.substr(2, 2) << '\n'; a.swap(c); std::cout << a << c << '\n'; } string Comparison, Substring, Swapping A. String length of substring Cat Cap 0 pt CaptainCap 0

2008YeungNam Univ. SE Lab. #include using std::string; #include void main() { string a("Cat"), b, c; if ( b.empty() ) b = a; // or ‘ if ( b== "" ) ….’ std::cout << b << '\n'; std::cout << a.length() << a.size() << '\n'; std::cout << a.compare(0, a.size(), b) << '\n'; std::cout << a.capacity() << ", " << a.max_size() << '\n'; } sch.cpp 4 string Characteristics returns max imum size a string can have returns total number of char that can be stored in a string without increasing the amount of memory allocated to the string A. String cout << a.capacity() << ", " << a.size() << "\n"; a += " plays with qute kittens"; cout << a.capacity() << ", " << a.size() << "\n"; a += " and frisky puppies"; cout << a.capacity() << ", " << a.size() << "\n"; a += " and "; cout << a.capacity() << ", " << a.size() << "\n"; same always , 3 31, 27 63, 46 95, 67 0 Cat capacity( ) Max_size) 3

2008YeungNam Univ. SE Lab. sfind.cpp 5 #include using std::cout; void main() { std::string s("A white cat plays with black cats."); std::cout << s.find("cat") << s.rfind("cat") <<'\n'; std::cout << s.find_first_of("cbus") << '\n'; std::cout << s.find_last_of("cbus") << '\n'; std::cout << s.find_first_not_of(" Aehitw") << '\n'; std::cout << s.find_last_not_of(".ackst ") << '\n'; s.erase( 28 ); // erase s[28..] std::cout << s <<'\n'; s.append(" cats"); int i = s.find(" "); // find() returns string::npos when it fails. while ( i < std::string::npos ) { // -1(4,294,967,295) -- public static constant s.replace(i, 1, "."); i = s.find(" ", i+1); } std::cout << s << '\n'; s.insert(s.find_last_of(".")+1, "little."); std::cout << s << '\n'; s.insert( 2, "a cute puppy", 2, 5 ); std::cout << s << '\n'; } string Find, Replace, Insert A. String A white cat plays with black A.white.cat.plays.with.black. cats A.white.cat.plays.with.black.little.cats A.cutewhite.cat.plays.with.black.littl e.cats

2008YeungNam Univ. SE Lab. siter.cpp 6 #include using std::string; #include void main() { string s("A white cat plays with black cats."); string::const_iterator it = s.begin(); while ( it != s.end() ) std::cout << *it++; std::cout << '\n'; string::reverse_iterator rit = s.rbegin(); while ( rit != s.rend() ) std::cout << *rit++; std::cout << '\n'; } string Iterator The class string provides iterators for forward and backward traversal. Similar to pointers, Iterators provide access to individual elements. Iterators are not range checked. A. String begin() end() rbegin() rend() A white cat plays with black cats..stac kcalb htiw syalp tac etihw A

2008YeungNam Univ. SE Lab. 7 ssio.cpp #include using std::string; #include using std::ostringstream; using std::istringstream; #include using std::cout; void main() { ostringstream oss; string s( "Anny has " ); oss << s << 3 << " Doggies"; // MF str() returns string copy of ostringstream cout << oss.str() << '\n'; oss << " and " << 12 << " Kittens\n"; cout << oss.str(); istringstream iss(oss.str()); int a; string s1, s2, s3; iss >> s1 >> s2 >> a; cout << s1 << s2 << a; iss >> s1 >> s2 >> a >> s3; cout << s1 << s2 << a << s3 << '\n'; iss >> a; // Try to extract int from empty string stream cout << iss.good() << '\n'; } string stream I/O Annyhas3 doggiesand12Kittens A A. String Input from / Output to a string 0 Anny has 3 doggies and 12 Kittens Anny has 3 doggies

2008YeungNam Univ. SE Lab. 8 summary Template class basic_string provides typical string-manipulation operations such as copying, searching, etc. string s are not necessary null terminating. string provides =() and MF assign() for string assignments. [] provides read/write not-range-checked-access to any element of a string. MF at() provides range-checked access – It throws an out_of_range exception. MF capacity() returns the total number of characters that can be stored in the string without increasing the amount of memory allocated to the string. MF max_size() returns the maximum size a string can have. MF resize() changes the length of a string. find(), rfind(), find_first_of(), find_last_of(), etc, locate substrings or characters in a string. MF erase(), replace(), and insert() delete, replace, and insert elements of a string, respectively. MF c_str() returns const char* pointing to a null-terminated C-style string. class string provides MF begin() and end() to iterate through individual elements, and Input from / Output to a string are supported by the type istringstream/ostringstream. ostring stream MF str() returns a string copy of a string. A. String MF data() returns const char* pointing to a non-null-terminated C-style array. MF rbegin() and rend() to iterate individual elements in reverse-direction.