/c-plus-plus-examples

A collection of C++ examples, all compilable. Covers from C++11 to C++20.

Primary LanguageC++GNU General Public License v2.0GPL-2.0

Modern C++ examples

Are you looking for a collection of short straightforward snippets of modern C++ covering the core features in C++11 and beyond? Of course you are!

As I'm constantly learning C++ myself (disclaimer, I'm not an expert, just enthusiastic), I wanted to create some simple examples of each of the main features of the language. You can build each example individually or as a whole.

Let me know if this is useful to anyone or if there are any areas you want covered. Suggestions, enhancements and errata welcome.

This is a (probably perpetual) work in progress!

How to print hello world in color in C++, because why not

How to use read a file in far too many ways

How to use scoped enums

How to use std::initializer_list

How to use std::initializer_list with your own container like class

How to use constexpr for fame and profit

How to use decltype

How to use range-based for loops

How to use range-based for loops with your own iterator

How to use lambdas

How to use std::to_string with custom classes and templates

How to use std::for_each and how it differs from std::transform

How to use std::map with a custom key

How to use std::map for reverse sorting

How to use std::unordered_map

How to use std::multiset and std::multiset::equal_range

How to use std::set

How to use std::unordered_set

How to use std::sort with a custom container and iterators

How to use std::function and the 'using' keyword

How to use std::bind

How to use std::bind with methods or proxy functions

How to use std::bind with callback inside a class

How to use std::move to avoid the cost of temporaries

How to use std::forward to invoke the exactly correct function through a template

How to use std::unique_ptr

How to use std::unique_ptr with a custom deallocator

How to use std::unique_ptr for file handling

How to use std::shared_ptr

How to use std::shared_ptr with your own wrapper for extra debugging

How to use std::thread in three different ways

How to use std::thread and std::mutex to implement a timeout around a function

How to use variadic templates for recursion instanity

How to use std::thread and variadic templates to call a function with variable number and types of args

How to use std::function with variadic templates

TODO How to use std::remove_if and std::erase

TODO How to use std::erase_if

TODO How to use std::mutex

TODO How to use std::chrono

TODO How to use std::accumulate and std::reduce

To build all the examples, just do:

   make 

To build all the examples and the documentation and run every examle, do:

   sh ./RUNME