Dataslope logoDataslope

C++ Essentials for DSA

A fast refresher on the C++ features used throughout data structures and algorithms

You do not need all of C++ to start solving DSA problems. You need a reliable slice: types, references, const, vectors, functions, templates, lambdas, and a handful of headers.

The compilation model

C++ source files are compiled into object files, then linked with libraries to produce an executable.

Code Block
C++ 20 (202002L)

Variables, types, and auto

For DSA, use int for ordinary counts, long long for large integer answers, size_t for container sizes, bool for flags, char for characters, and double for floating-point values. auto steps = path.size(); asks the compiler to deduce the type from the initializer.

Signed and unsigned indices

size_t is unsigned. Be careful when counting backward or comparing it with int; many bugs come from values wrapping below zero.

References vs pointers

A reference is another name for an existing object. A pointer stores an address and can be reassigned or be null. Prefer references when a parameter must refer to a valid object; use pointers when null is meaningful.

Code Block
C++ 20 (202002L)

const and const& parameter passing

Use const when a value should not change. Pass large objects as const& to avoid copying while promising not to modify them.

Code Block
C++ 20 (202002L)

Headers used throughout this course

HeaderGives you
<iostream>std::cin, std::cout
<vector>std::vector
<string>std::string
<algorithm>sort, min, max, reverse, binary_search
<unordered_map>Hash maps
<queue>queue, priority_queue
<stack>stack
<numeric>accumulate, iota, gcd

A quick tour of std::vector

std::vector is the default sequence container for DSA: fast indexing, compact storage, and amortized O(1) appends. Iterators mark ranges; STL algorithms often use [begin, end).

Code Block
C++ 20 (202002L)

Function templates

Templates let one function work for many types while still being checked and optimized at compile time.

Code Block
C++ 20 (202002L)

Lambda expressions

A lambda is an inline function object. Lambdas are used heavily with algorithms such as std::sort.

Code Block
C++ 20 (202002L)

Practice: functions and templates

Challenge
C++ 20 (202002L)
Sum a vector

Implement int sum(const std::vector<int>& v) so it returns the total of all values in the vector. Do not print inside the function; the provided main will print the result.

Challenge
C++ 20 (202002L)
Generic max of three

Implement a templated function max3<T> that returns the largest of three values. The same function should work for both int and double.

Test your knowledge

QuestionSelect one

When should you prefer a reference parameter like int& x over a pointer parameter like int* x?

When the argument may be missing and represented as null

When the function requires a valid object and may modify it

When you need to allocate memory manually

When you want to store an address in a container

QuestionSelect one

Why pass const std::vector<int>& v instead of std::vector<int> v for a read-only helper function?

It makes the vector sorted automatically

It allows the function to delete elements faster

It avoids copying the whole vector while preventing modification

It converts the vector into an array

QuestionSelect one

Which header provides std::sort?

<iostream>

<vector>

<algorithm>

<queue>

Ready for analysis

With this C++ toolkit in place, you can focus on the most important DSA question: how does an algorithm scale as the input grows?

On this page