You are on page 1of 4

C++ STL

Container types (and algorithms, functions and all STL as well) are defined
not in global namespace, but in special namespace called std." Add the
following line after your #include and before the code begins:
using namespace std;

The spirit of the Standard Template Library is the idea of


generic programming - the implementation of algorithms or
data structures without being dependent on the type of data
being handled. For instance, you can use the STL vector
container to store a vector (think of it as a resizable array) of
any object you desire.
1. Vectors
- Handle own storage when it has to grow unlike an array
which is fixed
- The push_back( ) member function inserts value at the
end of the vector, expanding its size as needed.
- The size( ) function displays the size of the vector.
- The function begin( ) returns an iterator to the start of
the vector.
- The function end( ) returns an iterator to the end of the
vector.
vector<int> v; creates an empty vector v
vector<int> v(10); creates 10 such empty vector<int>s,
similar to a 2D array
- The empty() function checks if the vector is empty or
not
- Resize() function to resize the vector length
- Erase() function
- Another argument for using vectors are that they help
avoid memory leaks--you don't have to remember to

free vectors, or worry about how to handle freeing a


vector in the case of an exception.

- vector<int> v(20);
-

for(int i = 0; i < 20; i++) {

v[i] = i+1;

v.resize(25);

for(int i = 20; i < 25; i++) {

v[i] = i*2;
}

Note that if you use push_back() after resize(), it will add elements
AFTER the newly allocated size, but not INTO it. In the example
above the size of the resulting vector is 25, while if we use
push_back() in a second loop, it would be 30.

vector<int> v(20);
for(int i = 0; i < 20; i++) {
v[i] = i+1;
}
v.resize(25);
for(int i = 20; i < 25; i++) {
v.push_back(i*2); // Writes to elements with indices [25..30), not
[20..25) ! <
}

int N,M;
// ...
vector< vector<int> > Matrix(N, vector<int>(M));

Here we create a matrix of size N*M and fill it with -1.

So why use iterators? First, they're a flexible way to access the data in containers that
don't have obvious means of accessing all of the data (for instance, maps [to be
discussed later]). They're also quite flexible -- if you change the underlying container,
it's easy to change the associated iterator so long as you only use features associated

with the iterator supported by both classes. Finally, the STL algorithms defined in
<algorithm> (to be discussed later) use iterators.

PAIRS
template<typename T1, typename T2> struct pair {
T1 first;
T2 second;
};

To create an iterator object, we must specify its type. The type of iterator
can be constructed by a type of container by appending ::iterator,
::const_iterator, ::reverse_iterator or ::const_reverse_iterator to it.
Thus, vector can be traversed in the following way:
vector<int> v;

// ...

// Traverse all container, from begin() to end()


for(vector<int>::iterator it = v.begin(); it != v.end(); it++) {
*it++; // Increment the value iterator is pointing to
}

Dont use <= while using vectors. i.e dont use comparison
operators
To request an iterator appropriate for a particular STL templated class, you use the syntax
std::class_name<template_parameters>::iterator name

Iterators are distinguished by whether you can use them for reading or writing data in the
container. Some types of iterators allow for both reading and writing behavior, though not
necessarily at the same time.

he old approach (avoid)


using namespace std;
vector<int> myIntVector;
// Add some elements to myIntVector
myIntVector.push_back(1);
myIntVector.push_back(4);
myIntVector.push_back(8);
for(int y=0; y<myIntVector.size(); y++)
{
cout<<myIntVector[y]<<" ";
//Should output 1 4 8
}
The STL approach (use this)
using namespace std;
vector<int> myIntVector;
vector<int>::iterator myIntVectorIterator;
// Add some elements to myIntVector
myIntVector.push_back(1);
myIntVector.push_back(4);
myIntVector.push_back(8);
for(myIntVectorIterator = myIntVector.begin();
myIntVectorIterator != myIntVector.end();
myIntVectorIterator++)
{
cout<<*myIntVectorIterator<<" ";
//Should output 1 4 8
}

STL MAPS
Suppose that you're working with some data that has values associated with strings -- for
instance, you might have student usernames and you want to assign them grades. How would you
go about storing this in C++? One option would be to write your own hash table. This will require
writing a hash function and handling collisions, and lots of testing to make sure you got it right. On
the other hand, the standard template library (STL) includes a templated class to handle just this
sort of situation: the STL map class, which conceptually you can think of as an "associative array"
-- key names are associated with particular values (e.g., you might use a student name as a key,
and the student's grade as the data).
std::map <key_type, data_type, [comparison_function]>
We don't need to specify a comparison function for so-called primitive types such as int, char, or
even for the string class.

You might also like