Skip to content

Instantly share code, notes, and snippets.

@NTG-TPL
NTG-TPL / optional.h
Created October 2, 2023 10:17
Simple C++ Implementation std::optional
#include <stdexcept>
#include <utility>
// Исключение этого типа должно генерироватся при обращении к пустому optional
class BadOptionalAccess : public std::exception {
public:
using exception::exception;
virtual const char* what() const noexcept override {
return "Bad optional access";
@NTG-TPL
NTG-TPL / check_tree.cpp
Created October 2, 2023 09:50
Checks the binary tree for correctness (C++ implementation)
#include <cassert>
#include <iostream>
#include <memory>
using namespace std;
template <typename T>
struct TreeNode;
@NTG-TPL
NTG-TPL / main.cpp
Last active October 2, 2023 09:47
simple linked list C++ implementation
#include "single-linked-list.h"
#include <cstdalign>
void Test0() {
using namespace std;
{
const SingleLinkedList<int> empty_int_list;
assert(empty_int_list.GetSize() == 0u);
assert(empty_int_list.IsEmpty());
}
@NTG-TPL
NTG-TPL / simple_stack.cpp
Created October 2, 2023 09:41
Simple Stack C++ implementation
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
using namespace std;
template <typename It>
void PrintRange(It range_begin, It range_end) {
@NTG-TPL
NTG-TPL / HanoiTowers.cpp
Created October 2, 2023 09:40
Hanoi Tower C++ implementation with OOP
#include <iostream>
#include <stdexcept>
#include <vector>
using namespace std;
class Tower {
public:
// конструктор и метод SetDisks нужны, чтобы правильно создать башни
Tower(int disks_num) {
@NTG-TPL
NTG-TPL / rational.cpp
Created October 2, 2023 09:38
C++ rational
#include <iostream>
#include <numeric>
#include <stdexcept>
using namespace std;
class Rational {
public:
Rational() = default;
@NTG-TPL
NTG-TPL / simple_preprocessor.cpp
Created October 2, 2023 09:37
Simple C++ preprocessor
#include <cassert>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <regex>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
@NTG-TPL
NTG-TPL / operations.cpp
Created July 11, 2023 11:24
Addition and product of all parameters
template <typename... Ts>
auto Product(const Ts&... vs) {
return (... * vs); // (((vn1 * vn2) * ...) * vnN)
}
template <typename... Tn>
auto Sum(const Tn&... vn) {
return (... + vn); // (((vn1 + vn2) + ...) + vnN)
}
@NTG-TPL
NTG-TPL / EqualsToOneOf.cpp
Created July 11, 2023 11:16
Function compares all arguments with the first
// Checks if there is an argument equal to the first
template<typename T, typename ...Tail>
bool EqualsToOneOf(const T &t, const Tail&... tail){
if constexpr (sizeof...(tail) == 0) {
return false;
}else {
return ( (t == tail) || ...);
}
}
@NTG-TPL
NTG-TPL / ApplyToManyArgs.cpp
Last active July 11, 2023 11:11
Apply the function to all arguments
// The "ApplyToMany" template applies the function f (the first argument) sequentially to each of its other arguments
template <typename Function, typename ...Args>
void ApplyToMany(Function& f, Args&& ...args) {
((f(std::forward<Args>(args))),...);
}