Standard Template Library

Standard Template Library - Standard Template Library...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Standard Template Library INTRODUCTION TO STL PART 1 - GETTING STARTED STL stands for Standard Template Library, and is a new feature of C++. We will be presenting some of the basic features of STL in this and subsequent issues. STL may not be available with your local C++ compiler as yet. The examples presented here were developed with Borland C++ 5.0. Third-party versions of STL are available from companies like ObjectSpace and Rogue Wave, and HP's original implementation (which may be obsolete) is available free on the Internet. To get an idea of the flavor of STL, let's consider a simple example, one where we wish to create a set of integers and then shuffle them into random order: #include <vector> #include <algorithm> #include <iostream> using namespace std; int main() { vector<int> v; for (int i = 0; i < 25; i++) v.push_back(i); random_shuffle(v.begin(), v.end()); for (int j = 0; j < 25; j++) cout << v[j] << " "; cout << endl; return 0; } When run, this program produces output like: 6 11 9 23 18 12 17 24 20 15 4 22 10 5 1 19 13 3 14 16 0 8 21 2 7 There's quite a bit to say about this example. In the first place, STL is divided into three logical parts: - containers - iterators - algorithms Containers are data structures such as vectors. They are implemented as templates, meaning that a container can hold any type of data element. In the example above, we have "vector<int>", or a vector of integers. Iterators can be viewed as pointers to elements within a container.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Algorithms are functions (function templates actually) that operate on data in containers. Algorithms have no special knowledge of the types of data on which they operate, meaning that an algorithm is generic in its application. We include header files for the STL features that we want to use. Note that the headers have no ".h" on them. This is a new feature in which the .h for standard headers is dropped. The next line of interest is: using namespace std; We discussed namespaces in earlier newsletter issues. This statement means that the names in namespace "std" should be made available to the program. Standard libraries use std to avoid the problem mentioned earlier where library elements (like functions or class names) conflict with names found in other libraries. The line: vector<int> v; declares a vector of integers, and then: for (int i = 0; i < 25; i++) v.push_back(i); adds the numbers 0-24 to the vector, using the push_back() member function. Actual shuffling is done with the line: random_shuffle(v.begin(), v.end()); where v.begin() and v.end() are iterator arguments that delimit the extend of the list to be shuffled. Finally, we display the shuffled list of integers, using an overloaded operator[] on the vector: for (int j = 0; j < 25; j++) cout << v[j] << " "; cout << endl; This code is quite generic. For example, we could change: vector<int> v; to: vector<float> v; and fill the vector with floating-point numbers. The rest of the code that shuffles and displays the result would not change. One point to note about STL performance. The library, at least the version used for
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

Standard Template Library - Standard Template Library...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online