0% found this document useful (0 votes)
3 views

Lecture 1b

The document discusses strings in C++, including how to declare and initialize string variables, the standard string class, and basic string operations. It also covers defining constants, declaring constant variables, and basic input and output using cout and standard output streams.

Uploaded by

lyleholst
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 1b

The document discusses strings in C++, including how to declare and initialize string variables, the standard string class, and basic string operations. It also covers defining constants, declaring constant variables, and basic input and output using cout and standard output streams.

Uploaded by

lyleholst
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

1

Introduction to strings

Lecture 2
Oct 7, 2020,

 2003 Prentice Hall, Inc. All rights reserved.


2

Introduction to Strings

• Variables that can store non-numerical values that


are longer than one single character are known as
strings.
• The C++ language library provides support for
strings through the standard string class.
• This is not a fundamental data type, but it behaves
in a similar way as fundamental types do in its
most basic usage.

 2003 Prentice Hall, Inc. All rights reserved.


3

Introduction to Strings (Cont…)

• A first difference with fundamental data types is


that in order to declare and use objects (variables)
of this type we need to include an additional
header file in our source code: <string> and have
access to the std namespace (which we already
had in all our previous programs thanks to the
using namespace statement).

 2003 Prentice Hall, Inc. All rights reserved.


4

Introduction to Strings (Cont…)

 2003 Prentice Hall, Inc. All rights reserved.


5

Introduction to Strings (Cont…)

• As you may see in the previous example, strings


can be initialized with any valid string literal just
like numerical type variables can be initialized to
any valid numerical literal.
• Both initialization formats are valid with strings:

• Strings can also perform all the other basic


operations that fundamental data types can, like
being declared without an initial value and being
assigned values during execution:
 2003 Prentice Hall, Inc. All rights reserved.
6

Introduction to Strings (Cont…)

 2003 Prentice Hall, Inc. All rights reserved.


7

Constants

• Constants are expressions with a fixed value.


• Defined constants (#define)
• You can define your own names for constants that
you use very often without having to resort to
memory consuming variables, simply by using the
#define preprocessor directive.
• Its format is:
• #define identifier value

 2003 Prentice Hall, Inc. All rights reserved.


8

Constants (Cont….)

• This defines two new constants: PI and


NEWLINE. Once they are defined, you can use
them in the rest of the code as if they were any
other regular constant, for example:

 2003 Prentice Hall, Inc. All rights reserved.


9

Constants (Cont….)

 2003 Prentice Hall, Inc. All rights reserved.


10

Constants (Cont….)

• In fact the only thing that the compiler


preprocessor does when it encounters #define
directives is to literally replace any occurrence of
their identifier (in the previous example, these
were PI and NEWLINE) by the code to which
they have been defined (3.14159 and '\n'
respectively).

 2003 Prentice Hall, Inc. All rights reserved.


11

Constants (Cont….)

• The #define directive is not a C++ statement but a


directive for the preprocessor; therefore it assumes
the entire line as the directive and does not require
a semicolon (;) at its end.
• If you append a semicolon character (;) at the end,
it will also be appended in all occurrences within
the body of the program that the preprocessor
replaces.

 2003 Prentice Hall, Inc. All rights reserved.


12

Declared constants (const)

• With the const prefix you can declare constants


with a specific type in the same way as you would
do with a variable:

• Here, pathwidth and tabulator are two typed


constants.
• They are treated just like regular variables except
that their values cannot be modified after their
definition.
 2003 Prentice Hall, Inc. All rights reserved.
13

Basic Input/Output

• Until now, the example programs of previous


sections provided very little interaction with the
user, if any at all.
• Using the standard input and output library, we
will be able to interact with the user by printing
messages on the screen and getting the user's input
from the keyboard.
• C++ uses a convenient abstraction called streams
to perform input and output operations in
sequential media such as the screen or the
keyboard.

 2003 Prentice Hall, Inc. All rights reserved.


14

Basic Input/Output (Cont…)

• We do not really need to care about many


specifications about the physical media associated
with the stream - we only need to know it will
accept or provide characters sequentially.
• The standard C++ library includes the header file
iostream, where the standard input and output
stream objects are declared.

 2003 Prentice Hall, Inc. All rights reserved.


15

Standard Output (cout)

• By default, the standard output of a program is the


screen, and the C++ stream object defined to
access it is cout.
• cout is used in conjunction with the insertion
operator, which is written as << (two "less than"
signs).
• cout << "Output sentence"; // prints Output
//sentence on screen
• cout << 120; // prints number 120 on screen
• cout << x; // prints the content of x on screen

 2003 Prentice Hall, Inc. All rights reserved.


16

Standard Output (cout) (Cont…)

• The << operator inserts the data that follows it into


the stream preceding it.
• In the examples above it inserted the constant
string Output sentence, the numerical constant 120
and variable x into the standard output stream
cout..
• Notice that the sentence in the first instruction is
enclosed between double quotes (") because it is a
constant string of characters.

 2003 Prentice Hall, Inc. All rights reserved.


17

Standard Output (cout) (Cont…)

• Whenever we want to use constant strings of


characters we must enclose them between double
quotes (") so that they can be clearly distinguished
from variable names.
• For example, these two sentences have very
different results:
• cout << "Hello"; // prints Hello
• cout << Hello; // prints the content of Hello
variable

 2003 Prentice Hall, Inc. All rights reserved.


18

Standard Output (cout) (Cont…)

• The insertion operator (<<) may be used more


than once in a single statement:
• cout << "Hello, " << "I am " << "a C++
statement";
• This last statement would print the message Hello,
I am a C++ statement on the screen. The utility of
repeating the insertion operator (<<) is
demonstrated when we want to print out a
combination of variables and constants or more
than one variable: cout << "Hello, I am " << age
<< " years old and my zipcode is " << zipcode;

 2003 Prentice Hall, Inc. All rights reserved.


19

Standard Output (cout) (Cont…)

• If we assume the age variable to contain the value


24 and the zipcode variable to contain 90064 the
output of the previous statement would be:
• Hello, I am 24 years old and my zipcode is 90064

 2003 Prentice Hall, Inc. All rights reserved.


20

Standard Output (cout) (Cont…)

• It is important to notice that cout does not add a


line break after its output unless we explicitly
indicate it, therefore, the following statements:
• cout << "This is a sentence.";
• cout << "This is another sentence.";
• will be shown on the screen one following the
other without any line break between them:
• This is a sentence.
• This is another sentence.

 2003 Prentice Hall, Inc. All rights reserved.


21

Standard Output (cout) (Cont…)

• even though we had written them in two different


insertions into cout.
• In order to perform a line break on the output we
must explicitly insert a newline character into
cout.
• In C++ a new-line character can be specified as \n
(backslash, n):

 2003 Prentice Hall, Inc. All rights reserved.


22

Standard Output (cout) (Cont…)

• cout << "First sentence.\n ";


• cout << "Second sentence.\nThird
sentence.";
• This produces the following output:
• First sentence.
• Second sentence.
• Third sentence.

 2003 Prentice Hall, Inc. All rights reserved.


23

Standard Output (cout) (Cont…)

• Additionally, to add a new-line, you may also use


the endl manipulator.
• For example:
• cout << "First sentence." <<
endl;
• cout << "Second sentence." <<
endl;
• would print out:
• First sentence.
• Second sentence.

 2003 Prentice Hall, Inc. All rights reserved.


24

Standard Output (cout) (Cont…)

• The endl manipulator produces a newline


character, exactly as the insertion of '\n' does. cout
will be an unbuffered stream in most cases, so you
can generally use both the \n escape character and
the endl manipulator in order to specify a new line
without any difference in its behavior.

 2003 Prentice Hall, Inc. All rights reserved.


25

cin and strings

• We can use cin to get strings with the extraction


operator (>>) as we do with fundamental data type
variables:
• cin >> mystring;
• However, as it has been said, cin extraction stops
reading as soon as if finds any blank space
character, so in this case we will be able to get just
one word for each extraction.
• This behavior may or may not be what we want

 2003 Prentice Hall, Inc. All rights reserved.


26

cin and strings (Cont…)

• For example if we want to get a sentence from the


user, this extraction operation would not be useful.
• In order to get entire lines, we can use the function
getline, which is the more recommendable way to
get user input with cin:

 2003 Prentice Hall, Inc. All rights reserved.


27

cin and strings (Cont…)

 2003 Prentice Hall, Inc. All rights reserved.


28

cin and strings (Cont…)

• Notice how in both calls to getline we used the


same string identifier (mystr).
• What the program does in the second call is
simply to replace the previous content by the new
one that is introduced.

 2003 Prentice Hall, Inc. All rights reserved.


29

stringstream

• The standard header file <sstream> defines a class


called stringstream that allows a string-based
object to be treated as a stream.
• This way we can perform extraction or insertion
operations from/to strings, which is especially
useful to convert strings to numerical values and
vice versa. For example, if we want to extract an
integer from a string we can write:

 2003 Prentice Hall, Inc. All rights reserved.


30

stringstream (Cont…)

• string mystr ("1204");


• int myint;
• stringstream(mystr) >> myint;
• This declares a string object with a value of
"1204", and an int object.
• Then we use stringstream's constructor to
construct an object of this type from the string
object.

 2003 Prentice Hall, Inc. All rights reserved.


31

stringstream (Cont…)

• After this piece of code, the variable myint will


contain the numerical value 1204.

 2003 Prentice Hall, Inc. All rights reserved.


32

stringstream (Cont…)

 2003 Prentice Hall, Inc. All rights reserved.

You might also like