C++ Data Structures & STL
The Standard Template Library (STL) in C++ is a powerful set of C++ template classes to provide general-purpose classes and functions with templates. It includes data structures and algorithms that can be used to manage collections of data efficiently.
Key Topics
Overview of STL
STL provides a collection of algorithms and data structures that greatly simplifies the process of programming in C++. It is divided into four main components:
- Containers
- Iterators
- Algorithms
- Functors
Containers
Containers are objects that store data. STL provides several types of containers:
vector
: Dynamic array that can resize itself.list
: Doubly linked list.deque
: Double-ended queue.set
: Collection of unique elements.map
: Collection of key-value pairs.
Example: Using a Vector
#include
#include
using namespace std;
int main() {
vector numbers;
numbers.push_back(10);
numbers.push_back(20);
numbers.push_back(30);
cout << "Vector elements: ";
for(int num : numbers) {
cout << num << " ";
}
return 0;
}
Output:
Iterators
Iterators are used to traverse the elements of a container. They provide a way to access the elements without exposing the underlying structure of the container.
Example: Using an Iterator with a Vector
#include
#include
using namespace std;
int main() {
vector numbers = {10, 20, 30};
cout << "Vector elements using iterator: ";
for(auto it = numbers.begin(); it != numbers.end(); ++it) {
cout << *it << " ";
}
return 0;
}
Output:
Algorithms
STL provides a rich set of algorithms that can be used with containers. These algorithms include operations such as sorting, searching, and manipulating data.
Example: Using the Sort Algorithm
#include
#include
#include
using namespace std;
int main() {
vector numbers = {30, 10, 20};
sort(numbers.begin(), numbers.end());
cout << "Sorted elements: ";
for(int num : numbers) {
cout << num << " ";
}
return 0;
}
Output:
Custom Data Structures
In addition to the built-in containers provided by STL, you can also create your own custom data structures using classes. This allows for more complex data management and functionality.
Example: Custom Linked List
#include
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
class LinkedList {
public:
Node* head;
LinkedList() : head(nullptr) {}
void append(int val) {
Node* newNode = new Node(val);
if (!head) {
head = newNode;
} else {
Node* temp = head;
while (temp->next) {
temp = temp->next;
}
temp->next = newNode;
}
}
void display() {
Node* temp = head;
while (temp) {
cout << temp->data << " ";
temp = temp->next;
}
}
};
int main() {
LinkedList list;
list.append(10);
list.append(20);
list.append(30);
cout << "Linked List elements: ";
list.display();
return 0;
}
Output:
Key Takeaways
- STL provides a powerful set of data structures and algorithms for efficient programming in C++.
- Understand the different types of containers available in STL:
vector
,list
,set
, andmap
. - Utilize iterators to traverse and manipulate data within containers.
- Leverage built-in algorithms like sorting and searching to simplify your code.
- Create custom data structures using classes for more complex data management.