C++ Intermediate Cheatsheet

C++ Intermediate Cheatsheet (Pointers, STL, Memory, Structs)

Take your C++ skills to the next level with this intermediate cheatsheet. Master pointers, memory, STL, and core concepts used in real-world coding and interviews.

If you haven’t covered basics, start with the C++ Basics Cheatsheet first.

Quick Navigation

1. Pointers (Core C++ Concept)

Direct access to memory addresses.

cpp
01int x = 10;
02int* p = &x;
03
04cout << *p; // 10

Key Concepts

  • &x gives the address of a variable
  • *p gives the value at that address
  • Pointers store memory addresses

Why Pointers Matter

  • Memory control
  • Dynamic allocation
  • Used in arrays, functions, data structures

Interview must-know topic.

2. References (Cleaner Alternative to Pointers)

Aliases without pointer syntax.

cpp
01int x = 10;
02int& ref = x;
03
04ref = 20;
05cout << x; // 20

Key Insight

  • Reference = alias of a variable
  • No separate memory needed
  • Common for pass-by-reference

3. Memory Management (new & delete)

Manual heap allocation essentials.

cpp
01int* p = new int(5);
02cout << *p;
03
04delete p;

Important Rules

  • Always delete allocated memory
  • Avoid memory leaks
  • Don’t use freed pointers

Common Mistake

cpp
01int* p = new int(10);
02// forgot delete → memory leak

4. Structures (struct)

Group related data simply.

cpp
01struct Student {
02 string name;
03 int age;
04};
05
06Student s1 = {"Corwin", 21};
07cout << s1.name;

Use Case

  • Group related data
  • Simple alternative to classes

5. Vectors (Dynamic Arrays)

Flexible and safer arrays.

cpp
01#include <vector>
02
03vector<int> v = {1,2,3};
04v.push_back(4);
05
06cout << v[0]; // 1

Why Vectors?

  • Dynamic size
  • Easy to use
  • Safer than arrays

Useful Operations

  • v.pop_back()
  • v.size()
  • v.clear()

6. Strings (Advanced Operations)

Practical string methods for real work.

cpp
01string s = "hello";
02s += " world";
03
04cout << s.substr(0, 5); // hello

Common Functions

  • length()
  • substr()
  • find()

7. STL Algorithms (Power Feature)

Write less, do more with optimized algorithms.

cpp
01#include <algorithm>
02#include <vector>
03
04vector<int> v = {3,1,2};
05
06sort(v.begin(), v.end());

More Examples

cpp
01reverse(v.begin(), v.end());

STL saves time, stays optimized, and is widely used in interviews.

8. Common Mistakes (Intermediate Level)

Avoid undefined behavior and runtime crashes.

Dangling Pointer

cpp
01int* p = new int(5);
02delete p;
03cout << *p; // Undefined behavior

Wrong Reference Usage

cpp
01int& ref; // Error (must initialize)

Vector Out of Bounds

cpp
01vector<int> v = {1,2,3};
02cout << v[5]; // Error

9. Quick Practice (Test Yourself)

Run these to build confidence.

Q1

cpp
01int x = 10;
02int* p = &x;
03cout << *p;

Answer: 10

Q2

cpp
01vector<int> v = {1,2};
02v.push_back(3);
03cout << v.size();

Answer: 3

Q3

cpp
01string s = "abc";
02cout << s[1];

Answer: b

Pro Tip

Run these in an editor and tweak the values. Active practice builds memory faster than passive reading.

Interview Focus (Important)

Topics interviewers expect you to know.

Must Know

  • Pointer basics
  • Reference vs pointer
  • Vector vs array
  • Memory allocation

Common Questions

  • What is a dangling pointer?
  • Why use vectors over arrays?
  • Difference between stack & heap?

What’s Next

Move toward interview-ready depth.

Next Step: C++ Interview Cheatsheet

Recursion, time complexity, patterns, and edge cases to become job-ready.

Explore Interview Topics

How to Use This Page

  • Learn → read + run code
  • Revise → scan quickly
  • Practice → solve problems
  • Repeat → build memory

FAQs

Are pointers hard to learn?

Initially yes — but practice makes them easy.

Should I memorize STL?

No — understand patterns and usage instead.

Is this enough for interviews?

You need practice + patterns + depth. This is a strong start.

Product Insight (For You)

This page builds serious learner trust.

High-Value Topics

Pointers, STL, and memory in one place.

Interview Ready

Focused on common interview concepts.

Actionable

Practice prompts built in.