8.6. STL: the (C++) standard template library
C++ itself has very few tools for managing sequences of characters, inputs/outputs and collections.
The STL library provides C++ with a standardized answer using the C++ proper mechanisms as:
- the object approach and capacity for abstraction with the operators
- use of templates
- operators overdefinition
The effective normalization of STL library make it a whole part of C++, and we will always use STL library for the management of strings, IO and collections.
In particular, the overwhelming use of this library has proven its reliability and its level of optimization, both in the use of memory or in performance.
Below we will describe only a few of services offered by STL.
8.6.1. Strings
The std::string class manages sequences of characters.
#include <string>
int main (int argc, char **argv)
{
std::string text = "abc defg";
std::string words[] = { "aaa", "bbb", "ccc", "ddd", "eee" };
text += words[0];
return (0);
}
8.6.1.1. Main operations with strings
8.6.2. Vectors
The std::vector class provides a linear sequence
container for any type of object (which can be copied).
8.6.2.1. Main operations acting on a vector
Adding an element at the end of a vector
Removing the last element from a vector
Removing all elements from a vector
Getting the first et the last element from a vector
int first = v.front ();
int last = v.back ();
Getting an element by its position
Setting an element localized by its position
8.6.2.2. Iterations in a vector
One can iterate through the elements of a vector using a category of iterator:
std::vector<T>::iterator
std::vector<T>::const_iterator
std::vector<T>::reverse_iterator
std::vector<T>::const_reverse_iterator
#include <iostream>
#include <vector>
typedef std::vector<int> int_vector;
int main (int argc, char **argv)
{
int_vector v;
for (int i = 0; i < 10; i++) {
v.push_back (i);
}
int_vector::iterator it;
for (it = v.begin (); it != v.end (); ++it) {
int i = *it;
std::cout << "i = " << i << std::endl;
}
return (0);
}
8.6.2.3. Some operations with iterators on vectors
8.6.3. Lists
Lists given by list class almost act as vectors, but offer an implementation based on doubly-linked lists providing efficient inserts and removes.
#include <list>
#include <iostream>
typedef std::list<int> int_list;
int main (int argc, char **argv)
{
int_list lst;
for (int i = 0; i < 10; i++) {
lst.push_front (i);
lst.push_back (i);
}
int_list::iterator it;
for (it = lst.begin (); it != lst.end (); ++it) {
int i = *it;
std::cout << "i = " << i << std::endl;
}
return 0;
}
8.6.4. Maps
The map class provides an indexed collection.
#include <iostream>
#include <map>
#include <string>
typedef std::map<int,std::string> dictionary;
int main (int argc, char **argv)
{
dictionary d;
std::string words[] = { "aaa", "bbb", "ccc", "ddd", "eee" };
for (int i = 0; i < sizeof (words) / sizeof (std::string); i++) {
d[i] = words[i];
}
dictionary::iterator it;
for (it = d.begin (); it != d.end (); ++it) {
int key = (*it).first;
std::string word = (*it).second;
std::cout << "word = " << word << std::endl;
}
return 0;
}