In this tutorial, we will explore the usage of maps, sets, and queues in the Standard Template Library (STL) in C++. These are powerful data structures that can help you manipulate and organize your data in more efficient and effective ways.
You will learn:
Prerequisites:
A map in STL is an associative container that stores elements formed by a combination of key-value pairs. The key is used to identify the element in the map, and it should be unique.
std::map <int, std::string> map1;
map1[1] = "one";
map1[2] = "two";
map1[3] = "three";
A set is another associative container that stores unique elements. The elements are stored in a specific order (ascending by default).
std::set<int> set1;
set1.insert(10);
set1.insert(20);
set1.insert(30);
A queue is a type of container adaptor which operates in a FIFO (first in first out) type of arrangement.
std::queue<int> queue1;
queue1.push(10);
queue1.push(20);
queue1.push(30);
Here is a simple example of a map in STL:
#include <iostream>
#include <map>
int main() {
std::map <int, std::string> map1;
map1[1] = "Apple";
map1[2] = "Banana";
map1[3] = "Cherry";
// Print all the elements in the map
for (auto it = map1.begin(); it != map1.end(); it++) {
std::cout << it->first << " => " << it->second << '\n';
}
return 0;
}
Expected Output:
1 => Apple
2 => Banana
3 => Cherry
Here is a simple example of a set in STL:
#include <iostream>
#include <set>
int main() {
std::set<int> set1;
set1.insert(30);
set1.insert(10);
set1.insert(20);
// Print all elements in the set
for (auto it = set1.begin(); it != set1.end(); it++) {
std::cout << *it << '\n';
}
return 0;
}
Expected Output:
10
20
30
Here is a simple example of a queue in STL:
#include <iostream>
#include <queue>
int main() {
std::queue<int> queue1;
queue1.push(10);
queue1.push(20);
queue1.push(30);
// Print and remove elements in the queue
while (!queue1.empty()) {
std::cout << ' ' << queue1.front();
queue1.pop();
}
return 0;
}
Expected Output:
10 20 30
In this tutorial, you have learned how to use maps, sets, and queues in STL. You've seen how these data structures can make your code more efficient and easier to manage.
For further learning, consider exploring other STL containers like vectors, lists, and deques. You can also look into STL algorithms which provide useful functions for manipulating data in containers.
Create a map that maps a string (name) to an integer (age). Insert three entries into the map, then print them out.
Create a set of integers and insert the numbers 1-5 into the set. Then remove the number 3 and print the set.
Create a queue of strings. Push three strings onto the queue, then pop each off, printing each one.
Exercise 1:
std::map<std::string, int> ageMap;
ageMap["Alice"] = 25;
ageMap["Bob"] = 30;
ageMap["Charlie"] = 35;
for(auto const& pair: ageMap) {
std::cout << pair.first << " is " << pair.second << " years old\n";
}
Exercise 2:
std::set<int> numSet;
for(int i = 1; i <= 5; i++) {
numSet.insert(i);
}
numSet.erase(3);
for(auto const& num: numSet) {
std::cout << num << "\n";
}
Exercise 3:
std::queue<std::string> strQueue;
strQueue.push("Hello");
strQueue.push("World");
strQueue.push("!");
while(!strQueue.empty()) {
std::cout << strQueue.front() << "\n";
strQueue.pop();
}
Practice working with these exercises to get a firm grasp on these data structures.