0% found this document useful (0 votes)
11 views12 pages

CC Resource STL DataStructures

The document provides an overview of the Standard Template Library (STL) in C++, detailing its components such as data structures (arrays, vectors, strings, pairs, sets, maps) and their functionalities. It highlights the advantages of using STL for efficient programming, including optimized algorithms and dynamic data management. Additionally, it includes links to further resources and practice problems related to these data structures.

Uploaded by

riteshpahari7
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)
11 views12 pages

CC Resource STL DataStructures

The document provides an overview of the Standard Template Library (STL) in C++, detailing its components such as data structures (arrays, vectors, strings, pairs, sets, maps) and their functionalities. It highlights the advantages of using STL for efficient programming, including optimized algorithms and dynamic data management. Additionally, it includes links to further resources and practice problems related to these data structures.

Uploaded by

riteshpahari7
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

STANDARD TEMPLATE LIBRARY (STL)

Prepared by -
Aamod Jain
(Coordinator – Coding Club)

• STL is a collection of algorithms, data structures and other tools that can
be user to simplify the making of C++ programs.

• STL provides several containers such as arrays, vectors, maps, sets along
with algorithms for searching, sorting and manipulating data.

• Since most of the data structures and algorithms in STL are implemented
optimally, it helps to produce efficient code resulting in faster execution
times.

INCLUDING THE STL LIBRARY

To know more about <bits/stdc++.h> in c++ ,


visit https://www.geeksforgeeks.org/bitsstdc-h-c/

DATA STRUCTURES

1. Arrays :

• arrays are a fundamental data structure that allows you to store a fixed-
size sequence of elements of the same data type in contiguous memory
locations .

CREATING AN ARRAY
• int myArray[10];
declares an array of 10 integers.

• int myArray[5] = {1,2,3,4,5};


initializes an array of 5 integers with specified values.

• int firstElement = myArray[0]; // firstElement is 1


int secondElement = myArray[1]; // secondElement is 2

2. VECTORS

A vector is a dynamic array provided by STL. It changes its size


automatically when elements are added or removed.

Advantages of using vector :

• Automatically manages the size of array .


• Provides many useful member functions for various operations.

CREATING A VECTOR

• vector<int> v;
declares vector v of data type int

• vector<int> v = {1,2,3};
declares a vector of 3 elements

• vector<int>v(10) ;
creates empty vector of size 10 with all values initialized to 0

• vector<int>v(10,2) ;
creates empty vector of size 10 with all values initialized to 2

VECTOR FUNTIONS

• v.size() : returns the size of a vector


• v[x] : to access , use and modify the element at index x in vector v.
• v.push_back(5) : pushes elements to the end of vector.
• v.pop_back() : deletes the last element of a vector .
• v.back() : returns the last elements of the vector.
• v.clear() : deletes all the elements of the vector making its size 0.

ITERATORS IN VECTORS

An iterator is like a pointer that allows you to traverse or move through the
elements of a vector (or other container). It helps in accessing elements in a
sequential manner.

Many STL functions operate with iterators.

• begin() : points to the first element in the data structure.


• end() : points to the position after the last element.

# To know more about iterators , visit https://www.geeksforgeeks.org/iterators-c-stl/

FUNCTIONS USING ITERATORS

• sort(v.begin(),v.end()) : sorts the array in ascending order


• reverse(v.begin(),v.end()) : reverses the array
• max_element(v.begin(),v.end()) : returns the pointer to max element in
vector .
• min_element(v.begin(),v.end()) : returns the pointer to min element in
vector .
• v.erase(position) : deletes element at specified position
• v.erase(starting_pos , ending_pos) : for deleting range
• v.insert(position , value) : inserts value ate specified position

3. STRINGS

A string is just a sequence of characters . It is also a dynamic array that can


be used almost like a vector .

CREATING A STRING
STRING FUNTIONS

• string.length() : Returns length of the string .

• Concatenation : strings can be concatenated using ‘+’ operator .

• string.substr(pos , len) : Returns a substring starting from position pos


with length len.

• find(str, pos = 0): Finds the first occurrence of substring str starting from
position pos.
4. PAIR

A pair is a simple container that stores two values. These values can be
of the same type or different types.

CREATING PAIRS

5. SETS

• A set is a data structure that maintains a collection of elements .


• It can contain only unique values and stores the elements in sorted
order .
• It is based on a balanced binary tree and its operations work in O(logn)
time.
CREATING SETS AND USING SET FUNCTIONS
UNORDERED SET : uses hashing , preserves the order of elements and its
operations work in O(1) time on average and hence can be more
efficient.
MULTISET : similar to set but can store duplicate elements.

6. MAPS

• Maps store data as key-value pairs where keys and values can be of any
data type.
• Map structure is based of balanced binary tree and accessing elements
takes O(logn) time.
CREATING MAPS AND USING MAP FUNCTIONS

ADVANCED DATA STRUCTURES

1. STACK

• Stack is a linear data structure that follows the Last In, First Out (LIFO)
principle. This means the most recently added item is the first one to be
removed.
• It provides 2 O(1) operations : adding element to top and removing
element from the top.
• Its only possible to access the top element of a stack.
2. QUEUE

• Queue is a linear data structure that follows the First In, First Out (FIFO)
principle. This means the first element added to the queue will be the
first one to be removed.
• It also provides two O(1) operations : adding element to the end and
removing element from the front.
• Its only possible to access the first and last element of a queue.

To know more about these data structures , visit


https://www.geeksforgeeks.org/the-c-standard-template-library-stl/
https://cplusplus.com/reference/stl/

Useful YouTube playlists -


https://www.youtube.com/playlist?list=PLauivoElc3ggagradg8MfOZreCMmXMmJ-
# Practice Problems :

1. Arrays / Vectors :
• https://codeforces.com/problemset/problem/1979/A
• https://leetcode.com/problems/two-sum/description/
• https://leetcode.com/problems/remove-duplicates-
from-sorted-array/description/
• https://leetcode.com/problems/merge-sorted-
array/description/

2. Strings :
• https://codeforces.com/problemset/problem/1974/B
• https://codeforces.com/problemset/problem/1941/C
• https://codeforces.com/problemset/problem/1907/B

More such problems here –


• https://codeforces.com/problemset?tags=strings,800-
1200
• https://leetcode.com/tag/string/

3. Sets / Maps :
• https://leetcode.com/problems/majority-
element/description/
• https://codeforces.com/contest/701/problem/C
• https://leetcode.com/problems/unique-number-of-
occurrences/description/
• https://codeforces.com/contest/1800/problem/B
Thank You

You might also like