In this tutorial, we will focus on learning how to use the Standard Template Library (STL) containers and iterators in C++ programming. STL is a powerful feature in C++ that provides predefined templates for a set of common data structures and algorithms.
By the end of this tutorial, you will be able to:
This tutorial assumes you have a basic knowledge of C++ syntax and programming concepts. Familiarity with basic data structures will be helpful but not necessary.
STL containers are template-based data structures. They can store any data type that can be copied or moved, like integers, floating point numbers, custom objects, etc. The primary STL container types are:
Sequence containers (vector, list, deque, array, forward_list): These containers implement data structures which can be accessed sequentially.
Container adaptors (queue, priority_queue, stack): These are not full container classes, but wrappers that provide a specific interface relying on an object of one of the container classes.
Associative containers (set, multiset, map, multimap): These containers implement sorted data structures that can be quickly searched.
Unordered Associative containers (unordered_set, unordered_multiset, unordered_map, unordered_multimap): These containers implement unsorted (hashed) data structures that can be quickly searched.
Iterators in STL provide a way to access the elements in a container sequentially without exposing the underlying structure of the container. Iterators essentially act like pointers.
#include <iostream>
#include <vector>
int main() {
// Declare a vector
std::vector<int> vec;
// Add elements to the vector
vec.push_back(10);
vec.push_back(20);
vec.push_back(30);
// Access elements using an iterator
for(std::vector<int>::iterator it = vec.begin(); it != vec.end(); it++) {
std::cout << *it << " ";
}
return 0;
}
Output
10 20 30
In this example, we declare a vector vec
and add elements using push_back()
. We then use an iterator (it
) to access and print each element in the vector.
#include <iostream>
#include <set>
int main() {
// Declare a set
std::set<int> s;
// Add elements to the set
s.insert(10);
s.insert(20);
s.insert(30);
s.insert(20); // This will not be added, as sets do not allow duplicate elements
// Access elements using an iterator
for(std::set<int>::iterator it = s.begin(); it != s.end(); it++) {
std::cout << *it << " ";
}
return 0;
}
Output
10 20 30
In this example, we declare a set s
and add elements using insert()
. Notice that attempting to insert a duplicate element has no effect, as sets do not allow duplicate elements. We then use an iterator to access and print each element in the set.
In this tutorial, we have learned about STL containers and iterators in C++. We've seen how to declare, populate, and access data in several types of STL containers using iterators. For further exploration of STL, consider learning about different types of iterators (like forward iterators, bidirectional iterators, etc.) and other STL components like algorithms and functors.
Exercise 1: Create a std::list
of integers and print its elements in reverse order.
Exercise 2: Create a std::map
where keys are names (strings) and values are ages (integers). Insert some data in the map and print all names with their corresponding ages.
Exercise 3: Create a std::unordered_set
of integers, insert some data into it and print all unique elements (an unordered_set only stores unique elements).
Solutions and explanations will be provided upon request.