The STL
From Wikiversity
STL refers to the Standard Template Library, a collection of data structures that can be used in any program. Common classes available in the STL include the dynamic array (vector), linked list (list), the stack, the queue, and the deque (think "deck of cards"). When creating an instance of these aggregate data types, which are templated, one must specify what class is intended to be stored in that data structure.
Example:
list<myClass> listOfMyClass; // create a list to store objects of type myClass, named listOfMyClass. myClass elem1, elem2, elem3; listOfMyClass.push_back(elem1); listOfMyClass.push_back(elem2); listOfMyClass.push_back(elem3);
Contents |
[edit] The Standard Template Library
The Standard Template Library (STL) is a standard. This is important to note when trying to learn how to use the library. At first, the conventions in the library may seem unfamiliar and strange; however, because the library is based on the same underlying principles, once you learn how to properly use one class in the STL, knowing how to use another STL class is relatively easy.
The first and, possibly, the most common use of the STL for programmers learning C++ are using the container classes. An example container class is the std::vector<> template, which is one of the simplest and most versatile used classes in the library.
[edit] Iterator
An iterator is a special pointer designed for use with the STL containers shown below.
Iterators can traverse the contents of a container, using either the ++ or -- operators.
[edit] vector
A vector is a variable sized array.
{ std::vector<int> example; example.push_back(25); }
The vector supports the following methods:
- push_back(<type>): adds an element to the end
- pop_back(): destroys the last element
- size(): Returns size of vector
- resize(): Resizes the vector
- insert(position, value): Anserts an item
- insert(position, number, value): Adds number values.
- insert(position, start, end): Inserts values between start and end.
To get iterators to the beginning or end:
- begin()
- end(): returns an iterator to the end of the vector (not readable)
- rbegin(): Returns a reverse iterator
- rend(): Returns a reverse iterator pointing to the end
[edit] bitset
The bitset is a statically sized storage container that handled boolean values.
{ bitset<12> bits; }
The bits can be accessed as if they were an array, but the following functions are available:
- set(pos, value=true): sets the bit at pos to value.
- reset(pos): sets the bit at pos to false.
- flip(pos): toggles the bit at pos.
All three functions return a reference to the current bitset.
If a variable-sized container is needed, vector<bool> should be used instead.
[edit] deque
[edit] list
[edit] map
[edit] multimap
[edit] multiset
[edit] priority_queue
[edit] queue
[edit] set
[edit] stack
[edit] complex
[edit] valarray
[edit] Lessons
| Topics in C++ | ||
| Beginners | Data Structures | Advanced |
|---|---|---|
|
||
| Part of the School of Computer Science | ||