2 views
27 Jan 2026
Generate next lexicographic permutation.
Real Life: Finding all possible arrangements.
Step-by-Step Logic:
1. Sort array first
2. Use next_permutation() repeatedly
3. Generates next arrangement...
3 views
27 Jan 2026
Find position to insert element to keep sorted order.
Real Life: Finding where to place book in sorted shelf.
Step-by-Step Logic:
1. lower_bound() finds first position >= value
2. upper_bound() ...
3 views
27 Jan 2026
Add/remove from both front and back.
Real Life: Line where you can join from either end.
Step-by-Step Logic:
1. Create deque
2. push_front() adds to front
3. push_back() adds to back
4. pop_fr...
3 views
27 Jan 2026
Like set but allows duplicates, keeps sorted.
Real Life: Sorted list with repetitions allowed.
Step-by-Step Logic:
1. Create multiset
2. Insert elements (duplicates allowed)
3. Automatically so...
3 views
27 Jan 2026
Sort vector of pairs by second element.
Real Life: Sorting students by marks, not by name.
Step-by-Step Logic:
1. Create custom comparison function
2. Pass it to sort()
3. Define your own sorti...
4 views
27 Jan 2026
Store two related values as single unit.
Real Life: Storing coordinates (x,y), name-age pairs.
Step-by-Step Logic:
1. Create pair using make_pair()
2. Access first element using .first
3. Acces...
4 views
27 Jan 2026
Count how many times each element appears.
Real Life: Vote counting, inventory management.
Step-by-Step Logic:
1. Use map to store element and its count
2. For each element, increment its count
...
4 views
27 Jan 2026
Queue where highest priority element comes first.
Real Life: Emergency room - critical patients treated first.
Step-by-Step Logic:
1. Create priority_queue
2. Push elements
3. Top always gives ...
3 views
27 Jan 2026
Set automatically keeps only unique elements, sorted.
Real Life: Collection of unique items, no duplicates.
Step-by-Step Logic:
1. Create set
2. Insert elements
3. Duplicates automatically igno...
3 views
27 Jan 2026
Store data in key-value pairs like dictionary.
Real Life: Phone book - name (key) to number (value).
Step-by-Step Logic:
1. Create map
2. Insert key-value pairs
3. Access value using key
4. Ch...
4 views
27 Jan 2026
Search for element in vector using find().
Real Life: Searching for item in list.
Step-by-Step Logic:
1. Use find() function
2. It returns iterator
3. If found, iterator points to element
4. I...
3 views
27 Jan 2026
Sort vector in ascending and descending order.
Real Life: Arranging numbers in order automatically.
Step-by-Step Logic:
1. Create vector with unsorted elements
2. Use sort() function for ascendi...
2 views
27 Jan 2026
Use queue - First In First Out data structure.
Real Life: Like queue at ticket counter - first come first serve.
Step-by-Step Logic:
1. Create queue
2. Push adds to rear
3. Front shows first el...
2 views
27 Jan 2026
Use stack - Last In First Out data structure.
Real Life: Like stack of plates - take from top, put on top.
Step-by-Step Logic:
1. Create stack
2. Push elements (add to top)
3. Top shows element...
2 views
27 Jan 2026
Learn vector - dynamic array that can grow/shrink.
Real Life: Like a flexible list that auto-adjusts size.
Step-by-Step Logic:
1. Create vector
2. Add elements using push_back()
3. Access elem...