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

Identifiers

A valid identifier is a sequence of one or more letters, digits, or underscore characters ( _ ). Spaces, punctuation marks, and symbols cannot be part of an identifier. In addition, identifiers shall always begin with a letter. They can also begin with an underline character ( _ ), but such identifiers are -on most cases- considered reserved for compiler-specific keywords or external identifiers, as well as identifiers containing two successive underscore characters anywhere. In no case can they begin with a digit. C++ uses a number of keywords to identify operations and data descriptions; therefore, identifiers created by a programmer cannot match these keywords. The standard reserved keywords that cannot be used for programmer created identifiers are: alignas, alignof, and, and_eq, asm, auto, bitand, bitor, bool, break, case, catch, char, char16_t, char32_t, class, compl, const, constexpr, const_cast, continue, decltype, default, delete, do, double, dynamic_...

Master Excel: Unleash Pro-Level Skills with These Tips and Tricks

7 Best Free Machine Learning Resources - 2022

  Google AI ai.google Education – Google AI Learn with Google AI. Whether you're just learning to code or you're a seasoned machine learning practitioner, you'll find information and exercises in this resource center to help you develop your... Learn from ML experts at Google. Understanding Machine Learning From Theory to Algorithms The term machine learning refers to the automated detection of meaningful patterns in data. https:// cs.huji.ac.il/w~shais/Unders Machine Learning & Deep Learning Tutorials github.com GitHub - ujjwalkarn/Machine-Learning-Tutorials: machine learning and deep learning tutorials,... machine learning and deep learning tutorials, articles and other resources - GitHub - ujjwalkarn/Machine-Learning-Tutorials: machine learning and deep learning tutorials, articles and other resources This repository contains a topic-wise curated list of Machine Learning and Deep Learning tutorials, articles, and other resources. AI Weekly aiweekly.co AI ...