All C++ snippets · 32
Every snippet has its own focus page with an explanation and the output it produces.
Basics · 6
- Streaming To coutThe << operator chains values onto an output stream. Prefer '\n' over std::endl, which also forces a flush.
- Type Deduction With autoauto asks the compiler to deduce the type from the initialiser. The variable is still statically typed.
- References Alias, Copies Do NotA reference is another name for the same object, so writing through it changes the original. A copy is independent.
- const CorrectnessTaking a const reference avoids copying the argument while promising the function will not modify it.
- Compile-Time EvaluationA constexpr function can run at compile time when its arguments are constants, so the result is usable as an array size.
- Braces vs ParenthesesParentheses call the count-and-value constructor, braces prefer the initialiser list. Here a holds three ones, b holds exactly 3 and 1.
Control Flow · 2
Functions · 3
- Function OverloadingSeveral functions may share a name if their parameter lists differ. The compiler picks one from the argument types.
- Default ArgumentsDefaults must be the trailing parameters. Omitted arguments are filled in at the call site by the compiler.
- Lambdas And Capture[x] captures by copy at creation time, [&x] captures by reference. scale kept the original 3 even after factor changed.
Containers · 3
- Vector Basicsvector is the default sequence container: contiguous storage, amortised constant push_back and full support for the algorithms.
- Counting With mapstd::map keeps keys sorted. Indexing a missing key default-constructs it, so counts[w]++ starts from zero.
- push_back Can InvalidateGrowing past the capacity reallocates and invalidates every iterator. Reserving first keeps them valid, which is why this is safe.
Classes · 3
- Constructor Initialiser ListThe initialiser list constructs members directly rather than default-constructing then assigning, and it is required for const and reference members.
- Rule Of ZeroBecause the member manages its own memory, the compiler-generated copy, move and destructor are all correct. Write none of them.
- Polymorphic Base Needs virtual ~Deleting through a base pointer without a virtual destructor is undefined behaviour and skips the derived destructor.
Inheritance · 1
Memory · 3
- Exclusive Ownershipunique_ptr cannot be copied, only moved. After the move the source is null and the destination frees the memory.
- Shared Ownershipshared_ptr counts owners and frees the object when the last one goes away. The count drops as b leaves scope.
- Never Use Raw newRAII ties a resource's lifetime to a scope, so it is released even when an exception unwinds. Manual delete cannot promise that.
Templates · 2
Algorithms · 3
- Sorting With A Comparatorstd::sort takes any strict weak ordering. The comparator must return true only when the first argument comes strictly first.
- Transform And AccumulateThe algorithms work on iterator ranges rather than containers, which is why the destination must already have room.
- Erase-Remove Idiomstd::remove_if only shuffles unwanted elements to the end and returns the new logical end. erase is what actually shrinks the container.