Skip to main content

Introduction to strings


Fundamental types represent the most basic types handled by the machines where the code may run. But one of the major strengths of the C++ language is its rich set of compound types, of which the fundamental types are mere building blocks.

An example of compound type is the string class. Variables of this type are able to store sequences of characters, such as words or sentences. A very useful feature!

A first difference with fundamental data types is that in order to declare and use objects (variables) of this type, the program needs to include the header where the type is defined within the standard library (header <string>):

// my first string
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string mystring;
  mystring = "This is a string";
  cout << mystring;
  return 0;
}
 
printout:
 
This is a string 

As you can 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. As with fundamental types, all initialization formats are valid with strings:


1
2
3
string mystring = "This is a string";
string mystring ("This is a string");
string mystring {"This is a string"};



Strings can also perform all the other basic operations that fundamental data types can, like being declared without an initial value and change its value during execution:

// my first string
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string mystring;
  mystring = "This is the initial string content";
  cout << mystring << endl;
  mystring = "This is a different string content";
  cout << mystring << endl;
  return 0;
}
 
printout:
 
This is the initial string content
This is a different string content 

Note: inserting the endl manipulator ends the line 
(printing a newline character and flushing the stream).



The string class is a compound type. As you can see in the
 example above, compound types are used in the same way as
 fundamental types: the same syntax is used to declare 
variables and to initialize them.
 
 

Popular posts from this blog

Best chess matchup: Peaceful_Moon_4D vs IM Toro123

Fundamental data types

The values of variables are stored somewhere in an unspecified location in the computer memory as zeros and ones. Our program does not need to know the exact location where a variable is stored; it can simply refer to it by its name. What the program needs to be aware of is the kind of data stored in the variable. It's not the same to store a simple integer as it is to store a letter or a large floating-point number; even though they are all represented using zeros and ones, they are not interpreted in the same way, and in many cases, they don't occupy the same amount of memory. Fundamental data types are basic types implemented directly by the language that represent the basic storage units supported natively by most systems. They can mainly be classified into: Character types: They can represent a single character, such as 'A' or '$' . The most basic type is char , which is a one-byte character. Other types are also provided for wider charac...