Home arrow C++ arrow The STL String Class

The STL String Class

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.

Author Info:
By: Gabor Bernat
Rating: 5 stars5 stars5 stars5 stars5 stars / 9
March 31, 2009
  1. · The STL String Class
  2. · Read or print using string
  3. · Checks, find, concatenation
  4. · Approaching the end

print this article

The STL String Class
(Page 1 of 4 )

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

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials