Keep bragging

Notes on technologies, coding, and algorithms

Modern C++ basics

C++ has undergone significant changes since 2011. Although it is still one of the most difficult language, it becomes more dynamic, user friendly, and rich with modern features.

iterator

The Iterator is one of the twenty-three well-known GoF design patterns, which:

Iterator plays a critical role in C++ STL and its algorithm library.

//tested on cpp.sh
#include <iostream>
#include <array>
#include <algorithm>

using namespace std;

int main()
{
    std::array<long, 5> numbers = {2, 1, 4, 3, 5};

    cout << "Is it sorted? " << is_sorted(numbers.begin(), numbers.end()) << endl;

    // sort acending
    sort(numbers.begin(), numbers.end());
    /*
    // sort descending
    sort(numbers.begin(), numbers.end(), [](auto a, auto b) {
        return a > b;
    });
    */    
    for_each(numbers.begin(), numbers.end(), [](auto v) { 
        cout << v << endl; 
    });

    cout << "Is it sorted? " << is_sorted(numbers.begin(), numbers.end()) << endl;
    cout << "Total elements: " << distance(numbers.begin(), numbers.end()) << endl; 
}

Reference

initialization

auto

Init statements inside if & switch

smart pointers

lambda expression

C++ algorithm lib

Reference

  1. Some awesome C++ features that every developer should know
  2. Modern C++ features