C++ STRINGS string is part of the Standard C++ Library

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

 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
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.
 2006 Pearson Education, Inc. All rights reserved Class string and String Stream Processing.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
 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.
1 Chapter 10 Characters, Strings, and the string class.
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.
Working with Strings Lecture 2 Hartmut Kaiser
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Chapter 10. Characters, Strings and the string class Csc 125 Introduction to C++ Fall 2005.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: Characters, Strings, and the string class.
 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++
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC 270 – Survey of Programming Languages
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
Chapter 16 C++ Strings By C. Shing ITEC Dept Radford University.
Chapter 9 Strings. Learning Objectives An Array Type for Strings – C-Strings Character Manipulation Tools – Character I/O – get, put member functions.
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
Chapter 7 Continued Arrays & Strings. Strings as Class Members Strings frequently appear as members of classes. The next example, a variation of the objpart.
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.
Chapter Characters, Strings, and the string class 10.
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.
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
Strings.
CS212: Object Oriented Analysis and Design
C-Strings We have already seen that a C-string is a null-terminated array of characters.
Class string and String Stream Processing: A Deeper Look
Strings, StringBuilder, and Character
Vectors Holds a set of elements, like an array
Working with Strings Lecture 2 Hartmut Kaiser
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
Working with Strings Lecture 2 Hartmut Kaiser
Programming with ANSI C ++
10.1 Character Testing.
String class and its objects
Class string and String Stream Processing
String What it is Why it’s useful
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Engineering Problem Solving with C++, Etter
9-10 Classes: A Deeper Look.
Standard Version of Starting Out with C++, 4th Edition
Today’s Objectives 28-Jun-2006 Announcements
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
9-10 Classes: A Deeper Look.
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

C++ STRINGS string is part of the Standard C++ Library Finally we get to the STL string class. This is not the C type character array that we have seen so far (even though internally it may use an array). This string class provides a lot more usability and protection. An example (using command line arguments will be shown after the preliminary coverage)

#include <string> Provides access to the standard string library. Look in includes for reference /usr/include/g++-3 (Red Hat Linux 8.0)

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.

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.

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.

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.

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

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.

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.

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

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

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)

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.

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

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 AND, there are even more (check the reference manual for all the details) Practice: inclass strings