Object-Oriented Programming with C++ Strings: Lecture 22 - Prof. Jeffrey A. Miller, Study notes of Engineering

An overview of the string type in c++, explaining that it is a typedef of the template class basic_string<char>. It covers the creation of string objects, accessing string characters, and the use of string utilities such as length(), substr(), and swap(). The document also discusses string operators and their overloading, as well as the difference between length and capacity of a string.

Typology: Study notes

Pre 2010

Uploaded on 03/28/2010

koofers-user-dl9
koofers-user-dl9 🇺🇸

10 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE294B
Object-Oriented C++ Programming for Engineers
Lecture #22
Jeffrey Miller, Ph.D.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Object-Oriented Programming with C++ Strings: Lecture 22 - Prof. Jeffrey A. Miller and more Study notes Engineering in PDF only on Docsity!

CSE294B

Object-Oriented C++ Programming for Engineers

Lecture

Jeffrey Miller, Ph.D.

string

The string type in C++ is actually a typedef of a template classcalled basic_string

The template definition is in the std namespace

This allows us to create a string object as such:

string text(“hello”);

The above line will call the basic_string constructor, andpass in the char* “hello”

We can also instantiate a string with a value as follows:

string text = “hello”;

The assignment operator is overloaded in the basic_string classto call the constructor

NOTE: There isn’t actually a class called “string” in C++,though many sources reference the “string” class when referringto the “basic_string” class

I will use this notation throughout this lecture

string Example

#include
#include
using namespace std;
void main() {
string s1("hello");
string s2 = "world";
string s3;
s3 = s1;
// s3.assign(s1);
s3 += " " + s2;
// s3.append(" " + s2);
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;
cout << "s3 = " << s3 << endl;

Overloaded string Operators

The string class has overloaded the ==, !=, <, >, >=, <= operators to operate onthe value of the strings instead of the reference of the string (as in C) 1 string s1("hello"); 2 string s

"world"; 3 string s3, s4; 4 5 s

s

s2; 6

appends the substring from s from index

to the number 7

of characters specified by the third parameter 8

NOTE:

the third parameter is not an index, but a length 9 s4.append(s3,

if (s

s4)

cout

s

s

endl; 13

cout

"s

s

endl; 16 cout

"s

s

endl; 17 cout

"s

s

endl; 18 cout

"s

s

endl;

swap Function

The string class has a swap function thatdoes exactly that

string s1("hello");

string s2 = "world";

s1.swap(s2);

cout

"s

s

endl;

cout

"s

s

endl;

Length vs Capacity of a string

The size or length of a string is the number of characters currently stored in the string - The capacity of a string is the number of characters that can be stored in the string withouthaving to allocate more memory - The max_size of a string is the maximum number of characters allowed in a string

void print_stats(string s) {
cout << "s = " << s << endl;
cout << "s.length() = " << s.length() << endl;
cout << "s.size() = " << s.size() << endl;
cout << "s.capacity() = " << s.capacity() << endl;
cout << "s.max_size() = " << s.max_size() << endl;
void main() {
string s1("hello");
print_stats(s1);
s1 = "hello everyone in the world";
print_stats(s1);

Program

Write a program that reads two strings from the user, thenputs the characters in alphabetical order using the stringfunctions we just discussed.

Homework

Homework #6 is posted!