Mod5 STL
Mod5 STL
The Standard Template Library (STL) in C++ provides a comprehensive set of tools for working with
data structures and algorithms.
It has three main items.: Containers, Algorithm, Iterator.
Containers:
The STL provides various container classes that store collections of objects. Like queue,stack, array ,
list , vector etc…
Containers are mainly of two types: sequence containers and associative containers..
Sequence containers are those which stores data sequentially. Example : vector,list..
vector: A dynamic array.
list: A doubly-linked list.
Associative containers are those which associates one data with another: Eg: MAP
map: An associative container that stores key-value pairs.
These can be used by including the respective headers like <vector>, <list>, <map>, etc.
Algorithms:
The STL includes a wide range of algorithms for tasks such as sorting, searching,
and manipulating data.
These algorithms can be used by including the <algorithm> header.
Examples include: sort: For sorting ranges.
find: For finding elements.
Iterators:
Iterators are like pointers means the objects that point to elements within
containers.
They provide a way to traverse through the contents of a container.
Iterators for vectors are used using <vector> header file. Iterators for list are
used by using <list> header file etc..
If you need advance traversing like advancing iterator by a specified number of
steps or to compute distance between two iterators etc, we need to use
<iterator> header file.
Vectors
• Are dynamic arrays.
• This is an array that can grow as needed.
• In c++ size of an array is fixed at compile time.
• This means size of an array cannot be adjusted at run time to
accommodate changing program conditions.
• A vector solves this problem by allocating memory as needed.
• Although vector is dynamic you can still use the standard array
subscript notation [ ] to access its elements.
• Declaring a vector
• Vector<int> v; THIS CREATES 0 LENGTH INT VECTOR means you can store
the data but it is empty at the time of creation.
v.end() does return an iterator that points to the position just past the last
element of the vector v.
This iterator does not point to the last element itself but rather to the
imaginary position after the last element.
It's important not to access data through v.end() because it doesn't point to a
valid element in the vector.
Example:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v = {3, 4, 5, 6, 7};
vector<int>::iterator p= v.begin();
v.insert(p, 2); // Inserts 2 at the beginning
v.push_back(8); // Appends 8 to the end
p=v.begin+1; // Move iterator to the second element
v.erase(p); // Erases the second element (4 in this case, since 2 was inserted at
the beginning)
for (p = v.begin(); p!= v.end(); p++) {
cout << *p << " ";
}
cout << endl;
return 0;
}
VECTOR
LIST
STUDENT 1 STUDENT 2 STUDENT 3
MAP
1 STUDENT 1 2 STUDENT 2 3 STUDENT 3
Lists
• In C++, the list is a sequence container that allows non-contiguous
memory allocation. Unlike vectors, lists are implemented as doubly
linked lists. They provide efficient insertion and deletion of elements
at both ends of the sequence, as well as in the middle. However, they
do not allow direct access to elements through random access
iterators.
• Declaring a List
• To declare a list of integers:
• list<int> l; // This creates an empty list of integers.
• Adding Elements
• To add an element to the list at the end, use:
• l.push_back(element);
• To add an element to the list at the beginning, use:
• l.push_front(element);
• Deleting Elements
• To delete an element, you can use an iterator to specify which element
to remove:
• l.erase(iterator_position);
• Setting an Iterator
• To set an iterator to the beginning of the list:
• list<int>::iterator p = l.begin();
Example for List
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> l = {3, 4, 5, 6, 7};
list<int>::iterator p = l.begin();
l.insert(p, 2); // Inserts 2 at the beginning
l.push_back(8); // Appends 8 to the end
advance(p, 1); // Move iterator to the second element
l.erase(p); // Erases the second element (4 in this case, since 2 was inserted at the
beginning)
•
for (p = l.begin(); p != l.end(); p++) {
cout << *p << " ";
}
cout << endl;
return 0;
}
• In vector, after inserting an element, p can dynamically adjust to point
to the newly inserted element or adjust its position.
• In list, p remains fixed and you use advance or simply iterate through
the list to move p to specific positions.
MAPS
• In C++, a `map` is an associative container that stores elements in key-
value pairs, where each key is unique and used to access its
corresponding value.
• The elements in a map are always sorted by their keys, which makes
searching, adding, and deleting items very efficient. This sorting
helps keep the map organized and easy to navigate.
• This sorting mechanism ensures that the keys are organized in a
specific order, typically ascending, making it easy to traverse the map.
Adding Elements
To add elements to the map, use the insert function or the subscript
operator []:
Eg: m[2] = "two";
Deleting Elements
To delete an element from the map by key, use the erase function:
m.erase(1); // Removes the element with the key 1 from the map.
Setting an Iterator
To set an iterator to the beginning of the map, use:
map<int, string>::iterator p = m.begin();
Example for MAP
#include <iostream>
#include <map>
#include <string> // Include <string> for std::string usage
using namespace std;
int main() {
map<int, string> m; // Declare a map with key as int and value as string
// Insert elements into the map with state names
m[1] = "California";
m[2] = "Texas";
m[3] = "New York";
m[4] = "Florida";
m[5] = "Washington";
// Iterate through the map using iterators
map<int, string>::iterator p=m.begin();
for (p = m.begin(); p != m.end(); p++) {
cout << "Key: " << p->first << ", Value: " << p->second << endl;
}
return 0;
}
To add element into MAP
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
// Declare a map with key as int and value as string
map<int, string> m;
// Adding elements
m[0]= "Oregon"; // Adding to the beginning
m[6]= "Colorado"; // Adding to the middle (after key 3)
m[7] = "Arizona"; // Adding to the end
cout << "\nMap after adding elements:" << endl;
for (p = m.begin(); p != m.end(); p++) {
cout << "Key: " << p->first << ", Value: " << p->second << endl;
}
// Deleting elements
m.erase(2); // Deleting element with key 2
cout << "\nMap after deleting element with key 2:" << endl;
for (p = m.begin(); p != m.end(); p++) {
cout << "Key: " << p->first << ", Value: " << p->second << endl;
}