STL Unit V
STL Unit V
LIBRARY (STL)
Vector, List, Stack, Queue, Map
Array
■ Introduced in C++11, std::array is a wrapper around built-in arrays with extra features like bounds
checking, iterators, and algorithms.
■ Header File
– #include <array>
■ Declaration
• Declaration
• vector<int> v; // vector of integers
• vector<string> names; // vector of strings
Common Operations:
List
■ In C++, list is another container in the Standard Template Library (STL). It represents a doubly
linked list, which allows fast insertion and deletion from anywhere in the list, unlike vector which
is optimized for access and appending at the end.
■ Header
– #include <list>
■ Declaration
– list<int> myList; // list of integers
– list<string> nameList; // list of strings
Iterator-based Traversal:
■ Header File
– #include <queue>
■ Declaration
– std::queue<int> q; // Queue of integers
– std::queue<std::string> names; // Queue of strings
Common Functions:
Function Syntax Description
■ Header File
– #include <map>
■ Declaration
– map<int, string> m; // Key: int, Value: string
– map<string, int> marks; // Key: string, Value: int
int main() {
vector<int> v = {10, 20, 30, 40};
if (it != v.end()) {
cout << "Found at index: " << distance(v.begin(), it) << endl;
} else {
cout << "Not Found" << endl;
}
}
Algorithms: find(), count(), sort() in stl
■ count() - Purpose: Counts the number of times a value appears in the range.
■ Header: #include <algorithm>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
vector<int> v = {1, 2, 2, 3, 4, 2};
cout << "2 appears " << c << " times" << endl;
}
Algorithms: find(), count(), sort() in stl
■ sort() - Purpose: Sorts the elements in ascending (default) or custom order.
■ Header: #include <algorithm>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
vector<int> v = {4, 1, 5, 2};
sort(v.begin(), v.end()); // Ascending
for (int x : v)
cout << x << " ";
for (int x : v)
cout << x << " ";
}
Algorithms: search(), merge(),for_each(), transform() stl
■ Search()- Purpose: Finds the first occurrence of a subsequence within a container.
■ Header: #include <algorithm>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
vector<int> main = {1, 2, 3, 4, 5, 6};
vector<int> pattern = {3, 4};
if (it != main.end())
cout << "Subsequence found at index: " << distance(main.begin(), it) << endl;
else
cout << "Not found" << endl;
}
Algorithms: search(), merge(),for_each(), transform() stl
■ merge()- Purpose: Merges two sorted ranges into a single sorted range.
■ Header: #include <algorithm>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
vector<int> a = {1, 3, 5};
vector<int> b = {2, 4, 6};
vector<int> result(6);
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void printSquare(int x) {
cout << x * x << " ";
}
int main() {
vector<int> v = {1, 2, 3};
int main() {
vector<int> input = {1, 2, 3};
vector<int> output(3);