0% found this document useful (0 votes)
6 views23 pages

STL Unit V

The document provides an overview of various Standard Template Library (STL) containers in C++, including array, vector, list, stack, queue, map, and multimap, along with their common operations and syntax. It also covers algorithms such as find, count, sort, search, merge, for_each, and transform, detailing their purposes and examples. Each container and algorithm is introduced with header file requirements, declarations, and essential functions.

Uploaded by

tosem11071
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views23 pages

STL Unit V

The document provides an overview of various Standard Template Library (STL) containers in C++, including array, vector, list, stack, queue, map, and multimap, along with their common operations and syntax. It also covers algorithms such as find, count, sort, search, merge, for_each, and transform, detailing their purposes and examples. Each container and algorithm is introduced with header file requirements, declarations, and essential functions.

Uploaded by

tosem11071
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

STANDARD TEMPLATE

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

– array<int, 5> arr = {1, 2, 3, 4, 5}; // array of 5 integers

for (int i : arr)


{
cout << i << " ";
}
Common Functions:
Function Syntax Description

Access element arr[0] or arr.at(0) Access 1st element

Size arr.size() Number of elements

Front arr.front() First element

Back arr.back() Last element

Fill arr.fill(0) Fill all elements with a value

Swap arr1.swap(arr2) Swap two arrays

Returns pointer to underlying


Data pointer arr.data()
array
Vector
■ In C++, a vector is a part of the Standard Template Library (STL) and is one of the most
commonly used sequence containers. It functions like a dynamic array that can grow or shrink in
size automatically.
■ Header File
– #include <vector>

• 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:

for (int x : myList)


{
std::cout << x << " ";
}
Common Operations:
Operation Syntax Description

Add at end myList.push_back(10); Adds 10 at the end

Add at front myList.push_front(5); Adds 5 at the beginning

Remove from end myList.pop_back(); Removes last element

Remove from front myList.pop_front(); Removes first element

Size myList.size(); Returns number of elements

Check if empty myList.empty(); Returns true if empty

Remove element by value myList.remove(10); Removes all 10s

Sort list myList.sort(); Sorts in ascending order

Reverse list myList.reverse(); Reverses the list

Clear all elements myList.clear(); Deletes all elements


Stack
■ A stack is a Last In, First Out (LIFO) data structure — the last element added is the first to be
removed.
■ Header File
– #include <stack>
■ Declaration
– std::stack<int> s; // Stack of integers
– std::stack<std::string> names; // Stack of strings
Common Functions:

Function Syntax Description

Push s.push(10); Adds element to top

Pop s.pop(); Removes top element

Top s.top(); Returns top element

Size s.size(); Returns number of elements

Check Empty s.empty(); Returns true if stack is empty


Queue
■ A queue allows insertion at the back and deletion from the front — just like a real-world queue.

■ 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

Add element q.push(10); Adds 10 at the back

Remove element q.pop(); Removes element from the front

Get front q.front(); Returns front element

Get back q.back(); Returns last element

Size q.size(); Returns number of elements

Check empty q.empty(); Returns true if empty


map in STL (C++)
■ A map is an associative container that stores key-value pairs in sorted order (by key).

■ Header File
– #include <map>
■ Declaration
– map<int, string> m; // Key: int, Value: string
– map<string, int> marks; // Key: string, Value: int

for (auto& [key, value] : m)


{
cout << key << " => " << value << std::endl;
}
Common Operations:
Operation Syntax Description

m[101] = "John"; or m.insert({102,


Insert Adds a key-value pair
"Alice"});

Access m[101] Returns value for key 101

Erase m.erase(101); Removes key 101

Size m.size(); Returns number of elements

Check Empty m.empty(); True if map is empty

Find m.find(101) Returns iterator to key or m.end()

Clear m.clear(); Removes all elements


Multimap
■ Multimap in C++ STL – a powerful associative container.

• A multimap is similar to a map, but allows duplicate keys.

• It stores key-value pairs in a sorted order based on the key.

• It uses a balanced binary search tree internally (like map).

• Multiple entries can exist with the same key.


Common Operations
■ insert()
– mm.insert({3, "Cherry"});

■ find()– Finds the first occurrence of a key.


– auto it = mm.find(1); // Points to first key = 1

■ equal_range() – Returns a pair of iterators to all matching keys.


auto range = mm.equal_range(1);
for (auto it = range.first; it != range.second; ++it)
{
cout << it->first << " => " << it->second << endl;
}
■ erase()
– mm.erase(2); // Erases all pairs with key 2
– mm.erase(mm.begin()); // Erase first element
Key Differences: map vs multimap

Feature map multimap

Duplicate keys ❌ Not allowed ✅ Allowed

Access via [] ✅ Supported ❌ Not supported

insert() Overwrites existing key Adds new key-value pair


Algorithms: find(), count(), sort() in stl

■ find() - Purpose: Searches for the first occurrence of a value in a range.


■ Header: #include <algorithm>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
vector<int> v = {10, 20, 30, 40};

auto it = find(v.begin(), v.end(), 30);

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};

int c = count(v.begin(), v.end(), 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 << " ";

sort(v.begin(), v.end(), greater<int>());

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};

auto it = search(main.begin(), main.end(), pattern.begin(), pattern.end());

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);

merge(a.begin(), a.end(), b.begin(), b.end(), result.begin());

for (int x : result)


cout << x << " ";
}
Algorithms: search(), merge(),for_each(), transform() stl
■ for_each()- Purpose: Applies a function to each element in a range.
■ Header: #include <algorithm>

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

void printSquare(int x) {
cout << x * x << " ";
}

int main() {
vector<int> v = {1, 2, 3};

for_each(v.begin(), v.end(), printSquare);


}
Algorithms: search(), merge(),for_each(), transform() stl
■ transform()- Purpose: Applies a function to a range and stores results in another range.
■ Header: #include <algorithm>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int square(int x) { return x * x; }

int main() {
vector<int> input = {1, 2, 3};
vector<int> output(3);

transform(input.begin(), input.end(), output.begin(), square);

for (int x : output)


cout << x << " ";
}

You might also like