3.string Vector
3.string Vector
Phenikaa University
String
Operations on strings
Dealing with the Characters in a string
Vector
Defining and Initializing vectors
vector Operations
Exercises
Library string Type
String
Operations on strings
Dealing with the Characters in a string
Vector
Defining and Initializing vectors
vector Operations
Exercises
The most common string operations
▶ The string input operator reads and discards any leading whitespace (e.g., spaces,
newlines, tabs). It then reads characters until the next whitespace character is
encountered.
int main()
{
string s; // empty string
cin >> s; // read a whitespace-separated string into s
cout << s << endl; // write s to the output
return 0;
}
Question: If the input to this program is " Hello World! ", the output?
Reading and Writing strings
▶ The string input operator reads and discards any leading whitespace (e.g., spaces,
newlines, tabs). It then reads characters until the next whitespace character is
encountered.
int main()
{
string s; // empty string
cin >> s; // read a whitespace-separated string into s
cout << s << endl; // write s to the output
return 0;
}
Question: If the input to this program is " Hello World! ", the output? "Hello"
Reading and Writing strings
▶ Like the input and output operations on the built-in types, the string oper ators
return their left-hand operand as their result. Thus, we can chain together
multiple reads or writes:
string s1, s2;
cin >> s1 >> s2; // read first input into s1, second into s2
cout << s1 << s2 << endl; // write both strings
Question: If we give this version of the program the same input " Hello World! ",
our output would be?
Reading and Writing strings
▶ Like the input and output operations on the built-in types, the string oper ators
return their left-hand operand as their result. Thus, we can chain together
multiple reads or writes:
string s1, s2;
cin >> s1 >> s2; // read first input into s1, second into s2
cout << s1 << s2 << endl; // write both strings
Question: If we give this version of the program the same input " Hello World! ",
our output would be? "HelloWorld!"
Reading and Writing strings
If we do not want to ignore the whitespace in our input, we can use the getline
function instead of the >> operator. The getline function:
▶ takes an input stream and a string
▶ reads the given stream up to and including the first newline and stores what it
read—not including the newline—in its string argument
▶ After getline sees a newline, even if it is the first character in the input, it stops
reading and returns.
▶ If the first character in the input is a newline, then the resulting string is the
empty string.
Using getline to Read an Entire Line
Like the input operator, getline returns its istream argument. As a result, we can
use getline as a condition.
For example, we can rewrite the previous program that wrote one word per line to
write a line at a time instead:
int main()
{
string line;
// read input a line at a time until end-of-file
while (getline(cin, line))
cout << line << endl;
return 0;
}
The string empty and size Operations
▶ The empty function returns a bool indicating whether the string is empty.
// read input a line at a time and discard blank lines
while (getline(cin, line))
if (!line.empty())
cout << line << endl;
▶ The size member returns the length of a string (i.e., the number of charac- ters in
it).
string line;
// read input a line at a time and print lines that are longer than 80
characters
while (getline(cin, line))
if (line.size() > 80)
cout << line << endl;
Comparing strings
▶ The equality operators (== and !=) test whether two strings are equal or
unequal, respectively.
▶ The relational operators <, <=, >, >= test whether one string is less than, less
than or equal to, greater than, or greater than or equal to another in a
lexicographical manner.
string str = "Hello";
string phrase = "Hello World";
string slang = "Hiya";
Question: str vs phrase vs slang?
Assignment for strings
The string library lets us convert both character literals and character string literals to
strings.
string s1 = "hello", s2 = "world";
string s3 = s1 + ", " + s2 + ’\n’;
string s4 = s1 + ", ";
string s5 = "hello" + ", ";
string s6 = s1 + ", " + "world";
string s7 = "hello" + ", " + s2;
Adding Literals and strings
The string library lets us convert both character literals and character string literals to
strings.
string s1 = "hello", s2 = "world";
string s3 = s1 + ", " + s2 + ’\n’;
string s4 = s1 + ", "; // ok: adding a string and a literal
string s5 = "hello" + ", "; // error: no string operand
string s6 = s1 + ", " + "world"; // ok: each + has a string operand
string s7 = "hello" + ", " + s2; // error: cant add string literals
Warning: For historical reasons, and for compatibility with C, string literals are not
standard library strings. It is important to remember that these types differ when you
use string literals and library strings.
Outline
String
Operations on strings
Dealing with the Characters in a string
Vector
Defining and Initializing vectors
vector Operations
Exercises
Processing Every Character? Use Range-Based for
To iterates through the elements in a given sequence and performs some operation on
each value in that sequence
for (declaration : expression)
statement
▶ expression is an object of a type that represents a sequence
▶ declaration defines the variable that we’ll use to access the underlying elements
in the sequence
As a string represents a sequence of characters
string str("some string");
// print the characters in str one character to a line
for (auto c : str) // for every char in str
cout << c << endl; // print the current char followed by a newline
Using a Range for to Change the Characters in a string
If we want to change the value of the characters in a string, we must define the loop
variable as a reference type
string s("Hello World!!!"); // convert s to uppercase
for (auto &c : s) // for every char in s
c = toupper(c); // c is a reference, so the assignment changes the
char in s
cout << s << endl;
Processing Only Some Characters?
The following example uses the subscript operator to print the first character in a
string:
if (!s.empty()) // make sure there’s a character to print
cout << s[0] << endl; // print the first character in s
or to print the first n characters:
if (!s.empty()) // make sure there’s a character to print
for (int i = 0; i < n; ++i)
cout << s[i] << endl;
Outline
String
Operations on strings
Dealing with the Characters in a string
Vector
Defining and Initializing vectors
vector Operations
Exercises
Outline
String
Operations on strings
Dealing with the Characters in a string
Vector
Defining and Initializing vectors
vector Operations
Exercises
Vector
▶ We can default initialize a vector, which creates an empty vector of the specified
type:
vector<string> svec; // default initialization; svec has no
elements
▶ We can also supply initial value(s) by copying elements from another vector.
vector<int> ivec; // initially empty
// give ivec some values
vector<int> ivec2(ivec); //copy elements of ivec into ivec2
vector<int> ivec3 = ivec; //copy elements of ivec into ivec3
vector<string> svec(ivec2); //error: svec holds strings, not ints
List Initializing a vector
We can list initialize a vector from a list of zero or more initial element values
enclosed in curly braces:
vector<string> articles = {"a", "an", "the"};
vector<string> v1{"a", "an", "the"}; // list initialization
vector<string> v2("a", "an", "the"); // error
Creating a Specified Number of Elements
String
Operations on strings
Dealing with the Characters in a string
Vector
Defining and Initializing vectors
vector Operations
Exercises
Adding Elements to a vector
The push back operation takes a value and ”pushes” that value as a new last element
onto the ”back” of the vector.
vector<int> v2; // empty vector
for (int i = 0; i != 100; ++i)
v2.push_back(i); // append sequential integers to v2
// at end of loop v2 has 100 elements, values 0 . . . 99
Using the same approach, we can read and store an unknown number of values in text
// read words from the standard input and store them as elements in a
vector
string word;
vector<string> text; // empty vector
while (cin >> word)
text.push_back(word); // append word to text
Questions
String
Operations on strings
Dealing with the Characters in a string
Vector
Defining and Initializing vectors
vector Operations
Exercises
Exercises