Handling text the C++ way

The class std::string

With the C++ standard library, text is handled using the class std::string. What is a class? The usage of classes is one of the main differences between C and C++. It is a relatively complex subject and the third part of this course is all about coding using classes. However, I will give you, for now, an overview of them so you can use them with at least a vague idea of what they are.

Classes

A class can be seen as an improved structure, that contains not only variables, but also functions, from which we can not directly access its members. Variables inside structures and classes are both called members and functions inside classes are called methods. Since we can not directly access the members of classes, we use their methods to do it indirectly. An instance of a class is also called an object of that class.

Setting an object of type std::string

The class std::string is defined in the header file <string>. We can define a std::string similarly to how we would do with a pointer of a static array of char.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string> #include <iostream> void main() { std::string str = "I am a string of characters."; std::cout << str << std::endl; /* "I am a string of characters." is written in the console. */ return 0; }

Unlike with static char arrays, we can change the value of an object of type std::string anytime.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string> #include <iostream> void main() { std::string str = "I am a string of characters."; str = "How are you?"; std::cout << str << std::endl; // "How are you?" is printed in the console. return 0; }

Comparing two std::string

Unlike with pointers of char or static char arrays, with std::string objects, we can directly compare, using the == operator, two strings of character.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string> #include <iostream> void main() { std::string str = "Blue"; std::string str2 = "Blue"; if(str == str2) std::cout << "Same color." << std::endl; // "Same color." is printed in the console. str2 = "Red"; if(str2 == "Red") std::cout << "Same color." << std::endl; // "Same color." is printed in the console. return 0; }

Accessing the characters individually

The characters inside an object of type std::string can be accessed using the brackets [], like we would do with a static char array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string> #include <iostream> void main() { std::string str = "AAA"; str[0] = 'S'; str[1] = 'k'; str[2] = 'y'; std::cout << str << std::endl; // "Sky" is printed in the console. return 0; }

The += operator of std::string

We may use the += operator with std::string objects to append strings of character after them.

1
2
3
4
5
6
7
8
9
10
11
12
#include <string> #include <iostream> void main() { std::string str = "I love "; str += "C++."; std::cout << str << std::endl; // "I love C++." is printed in the console. return 0; }

Methods of std::string

The class std::string has a great amount of methods so we will only see a few of them here.

A method, is accessed the same way we access members from a structure. We put a point after the object name and then we call the method (Like a function).

size()

The method std::string::size() of std::string returns the number of characters inside the object of type std::string it is called from. The class std::string does not store null-terminated strings, so its size is the real number of character.

1
2
3
4
5
6
7
8
9
10
11
12
#include <string> #include <iostream> void main() { std::string str = "Blue"; int nbChars = str.size(); std::cout << nbChars << std::endl; // "4" is outputted in the console. return 0; }

resize(size_t length)

The method std::string::resize(size_t length) changes the size of the std::string object to the number given as argument. The argument is of type size_t which is an unsigned integer type. If an object of type std::string is resized to a smaller length, characters from the right of the string will be lost.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string> #include <iostream> void main() { std::string str = "Blue"; str.resize(2); std::cout << str << std::endl; // "Bl" is outputted in the console. str.resize(4); str[3] = 'u'; str[4] = 'e'; std::cout << str << std::endl; // "Blue" is written in the console. return 0; }

clear()

That method sets the size of the std::string to 0.

1
2
3
4
5
6
7
8
9
10
11
12
#include <string> #include <iostream> void main() { std::string str = "Yellow"; str.clear(); std::cout << str << std::endl; // Nothing is outputted in the console. return 0; }

The line str.clear(); has the same effect as:

str = "";

erase(size_t pos = 0, size_t len = npos)

The method std::string::erase(size_t pos = 0, size_t len = npos) has default parameter values. That means that it is not required to specify them. By leaving the default parameters, the result is the same as the method std::string::clear(). If we specify only the first argument, then all the characters, starting from the character at the index represented by the number given as first argument until the end will be erased. If we also specify the second parameter, the number of character represented by the second argument will be removed, starting at the character indexed by the first parameter. Note that it is not possible to specify the value of the second argument without specifying the first one too.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string> #include <iostream> void main() { std::string str = "Yellow"; str.erase(); std::cout << str << std::endl; // Nothing is outputted in the console. str = "Orange"; str.erase(2); std::cout << str << std::endl; // "Or" is outputted in the console. str = "Green"; str.erase(2, 2); std::cout << str << std::endl; // "Grn" is outputted in the console. return 0; }

insert(size_t pos, const string& str)

The method std::string::insert(size_t pos, const string& str) inserts the string represented in second argument inside the std::string object from which the method is called, at the position (index from the start of the string) specified by the first argument.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string> #include <iostream> void main() { std::string str = "The is blue."; std::string str2 = "sky "; str.insert(4, str2); std::cout << str << std::endl; // "The sky is blue." is printed in the console. return 0; }

It is also possible, using that method, to simply insert a char. Note that most methods have many overloads. Remember, function overload is the definition of many functions with the same name that take different arguments. It also works with methods. That overload is a bit more tricky. It uses what is called an iterator, which is a relatively advanced subject. However, its utilization is fairly simple.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string> #include <iostream> void main() { std::string str = "Remeber."; str.insert(str.begin() + 4, 'm'); /* str.begin() returns an iterator which is a kind of pointer. The iterator returned by str.begin() points to the first characters of str. By adding 4, we jump over 4 characters. */ std::cout << str << std::endl; // "Remember." is printed in the console. return 0; }