Skip to content

Instantly share code, notes, and snippets.

View DVMirchev's full-sized avatar
🦑
Being

Dimitar Mirchev DVMirchev

🦑
Being
View GitHub Profile
template <class T, size_t N>
constexpr auto size(T (&a)[N])
{
return N;
}
template<class T>
T minMaxRand(const T& min, const T& max)
{
static_assert(std::is_arithmetic<T>::value);
static thread_local std::mt19937 generator(std::random_device{}());
std::uniform_int_distribution<T> distribution(min, max);
return distribution(generator);
}
// Sortable is a concept for sortable sequences
void sort(Sortable& s);
// or
template<Sortable Seq>
void sort(Seq& s);
// which is short of / equivalent to / sintactic sugar for
template<typename Seq>
requires Sortable<Seq> // requires tests if Sortable<Seq> returns true
template <Sequence S, typename T>
requires Equality_comparable<Value_type<S>, T>
Iterator_of<S> find(S& seq, const T& value);
//that is equivalent to
template <class S, typename T>
requires Sequence<S> && Equality_comparable<Value_type<S>, T>
Iterator_of<S> find(S& seq, const T& value);
template<typename T, typename U>
concept bool Equality_comparable =
requires (T a, U b) {
{ a == b } -> bool; // compare T == U
{ a != b } -> bool; // compare T != U
{ b == a } -> bool; // compare U == T
{ b != a } -> bool; // compare U != T
};
template<typename T>
template<class T, class U>
auto add(T t, U u) -> decltype(t + u) // the return type is the type of operator+(T, U)
{
return t + u;
}
auto foo() -> int;
auto moo(int a, int b) -> double;
auto sum(const std::vector<int>& numbers) -> long int;
valarray<int> va1(5); // create with 10 elements
valarray<int> va2(5); // create with 10 elements
for (int i=0; i < 5; i++) {
va1[i] = i;
va2[i] = i * 2;
}
valarray<int> vaMult = va1 * va2;
struct A {
A(int) { } // converting constructor
};
struct B {
explicit B(int) { } // non-converting constructor
};
void foo(A a) {}
std::bitset<8> b1(0b11'11'00'00);
// when indexing remember ^ this is bit [0]
std::bitset<8> b2("00001111");
// PROPOSAL OPPORTUNITY - you can`t initialize it with a string "00'00'11'11"
auto b3 = b1 & b2;
b3 |= b3;
b3[0] = true;
std::cout << "Bitsets!\n" << b1.to_string() << '\n'