Skip to content

The World of Engineers

Treasure of Concepts, Latest Jobs and Scholarships

Menu
  • Home
  • Blog
  • MCQs
  • Past Papers
  • Contractors
    • SSEM
    • Al Gihaz
    • substation contractors
    • OHTL Projects
    • SEC Projects
  • Design
    • 380kV Substation Design
    • Extra High Voltage EHV Substation Design
    • Transmission lines design
  • Jobs
    • Electrical Jobs
  • Privacy Policy
Menu

Data Structures in C++

Posted on September 14, 2025September 14, 2025 by Engr. Hamid Ali

Learning data structures in C++ is one of the most important steps for anyone looking to become a skilled programmer or software engineer. Data structures provide systematic ways to store and organize data, making programs faster and more efficient. With C++, you can implement data structures in two powerful ways: by building them from scratch or by using the Standard Template Library (STL), which offers ready-to-use implementations. From arrays, linked lists, and stacks to advanced trees, graphs, and hash maps, this guide will introduce the essential C++ data structures with examples to help you strengthen your problem-solving skills, prepare for coding interviews, and apply them in real-world projects.

What is STL in C++ :

The Standard Template Library (STL) in C++ is a powerful set of template classes that provide common data structures and algorithms. It helps programmers save time by reusing well-tested components instead of writing everything from scratch. STL mainly consists of containers, algorithms, iterators, and functors.


Data Structures in C++

A data structure is a way of organizing and storing data so it can be used efficiently.
In C++, common data structures include arrays, linked lists, stacks, queues, trees, graphs, and hash tables.

With STL, many of these are already implemented, such as:

  • Vector (dynamic array)
  • List (doubly linked list)
  • Stack and Queue
  • Set and Map (associative containers)

These tools allow developers to handle data effectively and apply algorithms like searching, sorting, and traversing with ease.

Here’s a short comparison table for the mentioned STL containers in C++:

ContainerTypeKey FeaturesUse Case
VectorDynamic ArrayStores elements in contiguous memory
Fast random access (O(1))
Insertion/deletion at end is fast, but slow in the middle
When frequent random access is needed
ListDoubly Linked ListEach element has pointers to prev & next
No random access
Fast insertion/deletion anywhere (O(1) with iterator)
When frequent insertions/deletions are needed
StackContainer AdapterLIFO (Last In, First Out)
push(), pop(), top()
Undo operations, expression evaluation
QueueContainer AdapterFIFO (First In, First Out)
push(), pop(), front(), back()
Task scheduling, order processing
SetAssociative ContainerStores unique, sorted elements
Based on balanced BST
Fast search (O(log n))
When unique, sorted data is needed
MapAssociative ContainerStores key–value pairs, sorted by key
No duplicate keys
Fast search/retrieval (O(log n))
Dictionaries, key-value

C++ Vectors: Basic to Advanced:

1. What is a Vector? (Basics)

  • A vector is a dynamic array in C++ provided by the Standard Template Library (STL).
  • Unlike regular arrays, vectors can resize automatically when you add or remove elements.
  • Elements are stored contiguously in memory → random access is fast (O(1) time).

Declaration:

vector<int> v1; // empty vector
vector<int>v2(5); 
// vector of size 5, default values 0
vector <int>v3(5, 10); 
// vector of size 5, all values 10
vector<int> v4 = {1, 2, 3}; 
// initializer list

Insert elements:

v1.push_back(10); // add at end
v1.emplace_back(20); // faster, constructs in place

Access elements:

cout << v1[0]; // no bounds checking
cout << v1.at(0); // throws error if index out of bounds
cout << v1.front(); // first element
cout << v1.back(); // last element

Remove elements:

v1.pop_back(); // removes last element
v1.clear(); // remove all elements

Size and capacity:

v1.size(); // number of elements
v1.capacity(); // memory allocated, may be larger than size
v1.empty(); // true if vector has no elements

Loops:

for (int i = 0; i < v1.size(); i++)
cout << v1[i] << ” “;

Range-based for loop

for (int x : v1)
cout << x << ” “;

Using iterators:

for (auto it = v1.begin(); it != v1.end(); ++it)
cout << *it << ” “;


Common STL Functions (DSA useful):

Sorting:

Searching:

Max/Min:


Advanced Vector Usage (DSA Patterns):

2D Vectors Like 2D Array :

  • A vector of vectors → useful for matrices, graphs, DP tables

vector<vector<int>> matrix(3, vector(4, 0)); // 3×4 matrix of 0s
matrix[1][2] = 5; // set element

Dynamic Resizing & Memory

  • Vectors automatically resize when push_back exceeds capacity.
  • v.reserve(n) → pre-allocate memory to reduce resizing overhead.
  • v.shrink_to_fit() → reduce capacity to current size.

Stack / Queue using vector:

vector<int> stack;
stack.push_back(10); // push
stack.pop_back(); // pop
int top = stack.back(); // top

Vector of Pairs:

vector<pair<int,int>> vp;
vp.push_back({1,2});
vp.emplace_back(3,4);

for(auto &p : vp)
cout << p.first << ” ” << p.second << endl;

Vector of Custom Objects:

struct Point { int x, y; };
vector <point> points;
points.push_back({1,2});
points.emplace_back(Point{3,4});

Common Mistakes in DSA:

  1. Accessing v[i] when v is empty use v.at(i) for safety.
  2. Forgetting that vector indices start at 0.
  3. Using push_back in a loop over empty vector incorrectly (common for beginners).
  4. Using size() in loop as int → better to use size_t to avoid warnings:

TASKs:

  1. Write a program to input n integers into a vector<int> and print them. //easy no solution
  2. Write a program to find the maximum and minimum element in a vector. //easy no solution
  3. Write a program to reverse a vector without using the built-in reverse() function. //easy no solution
  4. Write a program to search for an element in a vector (linear search).
  5. Write a program to remove duplicate elements from a vector.
  6. Write a program to sort a vector in ascending order using sort(). //easy no solution
  7. Write a program to add two vectors element-wise and store the result in a new vector.
  8. Write a program to rotate a vector k times to the right.

Solution:

Code is not provided in soft form because you copy code and not write on your own

Write a program to search for an element in a vector (linear search).

solution:

Write a program to remove duplicate elements from a vector.

Write a program to add two vectors element-wise and store the result in a new vector.

Write a program to rotate a vector k times to the right.

Our Top Posts

  • List of Contractors who Won Major BSP Projects in 2024 in KSA
  • Top Electrical Engineering Courses on Coursera
  • List of Top Substation contractors in KSA
  • Transmission Line Design Important Points
  • Panels required inside substations
  • Electrical MCQs with Explanation of Answers
  • Electrical Past papers
  • How to Add MCQs in wordpress posts
  • Transmission Lines design Basics
  • 380kV Substation Design (16)
  • Al BABTAIN (1)
  • Al Gihaz (3)
  • AL-OJAIMI (1)
  • Alsharif Group (1)
  • Basic Electrical (7)
  • Basic Electronics (5)
  • BSP Projects (16)
  • Communication System (1)
  • Control System (2)
  • Control System MCQs (3)
  • Courses (5)
  • electrical (8)
  • Electrical Courses (5)
  • Electrical Jobs (7)
  • Electrical MCQs (12)
  • Electronics MCQs (9)
  • Extra High Voltage EHV Substation Design (16)
  • Fellowships (2)
  • Fellowships in Pakistan (1)
  • FPSC (1)
  • HVDC Projects (1)
  • Information for Engineers (36)
  • Interview Preparation (27)
  • Jobs (10)
  • Jobs in KSA (11)
  • MATLAB and Simulink (1)
  • MCQs (13)
  • Microsoft Excel (2)
  • Microwaves MCQs (1)
  • NTS Past Papers (4)
  • OHTL Projects (7)
  • Past Papers (10)
  • Research Work (4)
  • Safety MCQs (2)
  • Saudi ARAMCO (3)
  • Scholarships (3)
  • Scholarships in Pakistan (1)
  • Scholarships in Pakistan (1)
  • SEC Projects (19)
  • SSEM (2)
  • Synchronous Condensers (2)
  • TDP (1)
  • Transmission lines design (7)
  • Uncategorized (23)
  • Web Development (1)

Latest Posts

  • Safety Assessment at Saudi Aramco Part 3
  • Classification of Lightning Protection Systems
  • Safety Assessment at Saudi Aramco Part 2
  • Safety Assessment at Saudi Aramco
  • Electrical Safety MCQs: Practice Quiz for Engineers

All Pages

  • Active Scholarships
  • C++ Data Structures
  • Electrical MCQs with explanation
  • Jobs
  • Privacy Policy
  • Technical Posts
  • The World of Engineers
  • Top Electrical MCQs with explanation

Follow Us on

  • Facebook
  • YouTube
  • LinkedIn

© 2025 The World of Engineers | Powered by Superbs Personal Blog theme