The C++ language library provides support for strings through the standard
string
class. This is not a fundamental type, but it behaves in a similar way as fundamental types do in its most basic usage.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:
and have access to the std
namespace (which we already had in all our previous programs thanks to the using namespace
statement).1 2 3 4 5 6 7 8 9 10 11 |
| This is a string |
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:
1 2 |
|
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
| This is the initial string content This is a different string content |