C Code Snippets - yszheda/wiki GitHub Wiki

template class operator

// member functions
complex& operator= (const T& val);
complex& operator+= (const T& val);
complex& operator-= (const T& val);
complex& operator*= (const T& val);
complex& operator/= (const T& val);

complex& operator= (const complex& rhs);

template<class X> complex& operator= (const complex<X>& rhs);
template<class X> complex& operator+= (const complex<X>& rhs);
template<class X> complex& operator-= (const complex<X>& rhs);
template<class X> complex& operator*= (const complex<X>& rhs);
template<class X> complex& operator/= (const complex<X>& rhs);

// non-member functions

template<class T> complex<T> operator+(const complex<T>& lhs, const complex<T>& rhs);
template<class T> complex<T> operator+(const complex<T>& lhs, const T& val);
template<class T> complex<T> operator+(const T& val, const complex<T>& rhs);

template<class T> complex<T> operator-(const complex<T>& lhs, const complex<T>& rhs);
template<class T> complex<T> operator-(const complex<T>& lhs, const T& val);
template<class T> complex<T> operator-(const T& val, const complex<T>& rhs);

template<class T> complex<T> operator*(const complex<T>& lhs, const complex<T>& rhs);
template<class T> complex<T> operator*(const complex<T>& lhs, const T& val);
template<class T> complex<T> operator*(const T& val, const complex<T>& rhs);

template<class T> complex<T> operator/(const complex<T>& lhs, const complex<T>& rhs);
template<class T> complex<T> operator/(const complex<T>& lhs, const T& val);
template<class T> complex<T> operator/(const T& val, const complex<T>& rhs);

template<class T> complex<T> operator+(const complex<T>& rhs);
template<class T> complex<T> operator-(const complex<T>& rhs);

bool operator==(const complex<T>& lhs, const complex<T>& rhs);
bool operator==(const complex<T>& lhs, const T& val);
bool operator==(const T& val, const complex<T>& rhs);

bool operator!=(const complex<T>& lhs, const complex<T>& rhs);
bool operator!=(const complex<T>& lhs, const T& val);
bool operator!=(const T& val, const complex<T>& rhs);

template<class T, class charT, class traits>
  basic_istream<charT,traits>&
    operator>> (basic_istream<charT,traits>& istr, complex<T>& rhs);
template<class T, class charT, class traits>
  basic_ostream<charT,traits>&
    operator<< (basic_ostream<charT,traits>& ostr, const complex<T>& rhs);

Bit operations

popcount

bit number of type

#include <climits>   // CHAR_BIT
#include <cstddef>   // std::size_t

template <typename T>
size_t num_bits()
{
    return sizeof (T) * (CHAR_BIT);
}

bitmask

#include <climits>

template <typename R>
static constexpr R bitmask(unsigned int const onecount)
{
//  return (onecount != 0)
//      ? (static_cast<R>(-1) >> ((sizeof(R) * CHAR_BIT) - onecount))
//      : 0;
    return static_cast<R>(-(onecount != 0))
        & (static_cast<R>(-1) >> ((sizeof(R) * CHAR_BIT) - onecount));
}

time measure

#include <iostream>
#include <chrono>

template<typename TimeT = std::chrono::milliseconds>
struct measure
{
    template<typename F, typename ...Args>
    static typename TimeT::rep execution(F&& func, Args&&... args)
    {
        auto start = std::chrono::steady_clock::now();
        std::forward<decltype(func)>(func)(std::forward<Args>(args)...);
        auto duration = std::chrono::duration_cast< TimeT> 
                            (std::chrono::steady_clock::now() - start);
        return duration.count();
    }
};

int main() {
    std::cout << measure<>::execution(functor(dummy)) << std::endl;
}

Next Power of Two

template <typename T>
inline T nextPowerOfTwo(T x)
{
    T result = 2;
    while (result < x) {
        result <<= 1;
    }
    return result;
}

random

#include <random>
#include <iostream>

int main() {
    std::random_device rd;
    std::mt19937 mt(rd());
    std::uniform_real_distribution<double> dist(1.0, 10.0);

    for (int i=0; i<16; ++i)
        std::cout << dist(mt) << "\n";
}
#include <random>
#include <iostream>
 
int main()
{
    std::random_device rd;  //Will be used to obtain a seed for the random number engine
    std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
    std::uniform_int_distribution<> dis(1, 6);
 
    for (int n=0; n<10; ++n)
        //Use dis to transform the random unsigned int generated by gen into an int in [1, 6]
        std::cout << dis(gen) << ' ';
    std::cout << '\n';
}

transfer map keys to set

// C++14
std::transform(MyMap.begin(), MyMap.end(),
    std::inserter(MySet, MySet.end()),
    [](auto pair){ return pair.first; });
⚠️ **GitHub.com Fallback** ⚠️