Skip to content

Instantly share code, notes, and snippets.

Avatar

Krzysztof Kowalczyk kjk

View GitHub Profile
View main.cc
#include <iostream> // std::cout
#include <atomic> // std::atomic, std::memory_order_relaxed
#include <thread> // std::thread
std::atomic_int foo (0);
void set_foo(int x) {
foo.store(x); // set value atomically
}
@kjk
kjk / main.cc
Created Jul 21, 2020
Example for sort (made with https://codeeval.dev)
View main.cc
#include <algorithm> // std::sort
#include <iostream>
#include <vector> // std::vector
using namespace std;
int main()
{
vector<int> a{3, 8, 1, 5, -8, 33, 4};
sort( a.begin(), a.end() );
int const n = a.size();
View main.cc
#include <complex>
#include <iostream>
int main()
{
using namespace std::literals::complex_literals;
std::complex<double> c = 2.0 + 1i; // {2.0, 1.}
std::complex<float> cf = 2.0f + 1if; // {2.0f, 1.f}
std::complex<long double> cl = 2.0L + 1il; // {2.0L, 1.L}
View main.cc
#include <codecvt>
#include <iostream>
#include <locale>
#include <string>
int main()
{
using namespace std::literals::string_literals;
std::string s = "hello world"s;
View main.cc
#include <chrono>
#include <iostream>
int main()
{
using namespace std::literals::chrono_literals;
std::chrono::nanoseconds t1 = 600ns;
std::chrono::microseconds t2 = 42us;
std::chrono::milliseconds t3 = 51ms;
View main.cc
#include <iostream>
long double operator"" _km(long double val)
{
return val * 1000.0;
}
long double operator"" _mi(long double val)
{
return val * 1609.344;
View main.cc
#include <iostream>
template< char FIRST, char... REST > struct binary
{
static_assert( FIRST == '0' || FIRST == '1', "invalid binary digit" ) ;
enum { value = ( ( FIRST - '0' ) << sizeof...(REST) ) + binary<REST...>::value } ;
};
template<> struct binary<'0'> { enum { value = 0 } ; };
template<> struct binary<'1'> { enum { value = 1 } ; };
View main.js
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
let newArr = arr.slice(0, 2);
console.log("slice(0,2) :", newArr)
newArr = arr.slice(8);
console.log("slice(8) :", newArr)
newArr = arr.slice(5, -1);
console.log("slice(5, -1):", newArr)
View main.cc
#include <iostream>
int main()
{
int x = 1;
if (x > 0) {
std::cout << "x (" << x << ") is greater than zero\n";
}
}
@kjk
kjk / factorial.cc
Created Jun 29, 2020
fancy factorial with templates (made with https://codeeval.dev)
View factorial.cc
#include <iostream>
#include <utility>
template <class T, T N, class I = std::make_integer_sequence<T, N>>
struct factorial;
template <class T, T N, T... Is>
struct factorial<T,N,std::index_sequence<T, Is...>> {
static constexpr T value = (static_cast<T>(1) * ... * (Is + 1));
};
You can’t perform that action at this time.