A text is nothing more than a successive sequence of characters. In the old days of programming, text was generally stored inside an array of characters. However, working with such arrays proved to be problematic, as you could easily trigger an overflow of the pre-allocated buffer or call for an invalid index. Fortunately, there is an easier way to handle this today, which you will learn in this article.
Aware of this troublesome glitch inside the first "mature" programming languages (C, Pascal), the designers of new, improved languages started to look for an easy and convenient solution. The way out was provided with object-oriented programming, first present in the C++ programming language designed by Bjarne Stroustrup.
OOP takes away all of the buffer handling from the programmer and completes it internally. When you add new text, it will resize and extend as needed. Furthermore, it offers some functions that will also do some input validity checks while not taking away the old tools. The operator overloads capability of these languages assures one easy and user-friendly usage for it.
Initially developed as a separate class, later, during the standardization process of the C/C++ language, this became the part of the STL library. As its base is just a container that holds characters inside. However, what sets it apart from the std::vector<char> declaration are the additional functions available for this class.
The std::string declaration is in fact just an instantiation of the template class std::basic_string with the argument for char. There is a type definition for wide characters also with the name wstring if you prefer the Unicode way.
Basic usage of it is as simple as it can be. Make sure you include the string header, and to avoid calling each member of the std namespace explicitly, add one of the following:
using namespace std; // can be used all of the STL namespace
//that is included before this
using std::string; // particulate version of the upper, only // for the string container
Now you can declare a new type that you may initialize with one of the constructor types. Be aware that using the assign operator immediately at the declaration process will lead the constructor to be called with the respective parameter.
String text ("Yeti);
// is the same as
string text = "Yeti";
Nevertheless, in the lines below this, the assign operator will take care of this. Because of this, it can be assumed that the following line will throw an error:
string alfa('a'); //or string alfa = 'a';
However, for the assign operator this is a valid operation, and you can assign a single char to a string. So just split the upper code as follows and it will work:
string alfa;
alfa = 'a';
There are additional ways to set the content of a string by using the assign function. This will accept a char* pointer ( the "Yeti" will return a const char pointer), or another definition exists that requires two arguments. The second tells the char what to use to fill the string, while the first points out how many times that should be replicated inside the string.
string alfa;
alfa.assign("Yeti"); // the string will contain Yeti
alfa.assign(3, 'a'); // the string will contain aaa