C Code Snippets - yszheda/wiki GitHub Wiki
// 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);
- https://gcc.gnu.org/onlinedocs/gcc-4.6.3/libstdc++/api/a00812_source.html
- http://www.cplusplus.com/reference/complex/complex/operators/
- A True C++ Complex Number Class
- http://en.cppreference.com/w/cpp/numeric/complex
- https://stackoverflow.com/questions/14682641/count-number-of-1s-in-binary-format-of-decimal-number
- popcount.cc
- GCC has
__builtin_popcount
- ARM POPCOUNT
- https://stackoverflow.com/questions/15736602/fastest-way-to-count-number-of-1s-in-a-register-arm-assembly
- Bug 36041 - Speed up builtin_popcountll
- glibc的几个有用的处理二进制位的内置函数
- https://stackoverflow.com/questions/13774145/i-want-both-implementations-of-builtin-popcountll
- https://en.wikichip.org/wiki/population_count
#include <climits> // CHAR_BIT
#include <cstddef> // std::size_t
template <typename T>
size_t num_bits()
{
return sizeof (T) * (CHAR_BIT);
}
- https://stackoverflow.com/questions/2090750/number-of-bits-in-a-data-type
- https://stackoverflow.com/questions/29285287/c-getting-size-in-bits-of-integer
#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));
}
- https://stackoverflow.com/questions/1392059/algorithm-to-generate-bit-mask
- https://stackoverflow.com/questions/8774567/c-macro-to-create-a-bit-mask-possible-and-have-i-found-a-gcc-bug`
#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;
}
- https://stackoverflow.com/questions/22387586/measuring-execution-time-of-a-function-in-c
- https://stackoverflow.com/questions/2808398/easily-measure-elapsed-time
template <typename T>
inline T nextPowerOfTwo(T x)
{
T result = 2;
while (result < x) {
result <<= 1;
}
return result;
}
- https://stackoverflow.com/questions/1322510/given-an-integer-how-do-i-find-the-next-largest-power-of-two-using-bit-twiddlin
- https://stackoverflow.com/questions/466204/rounding-up-to-next-power-of-2
- https://stackoverflow.com/questions/364985/algorithm-for-finding-the-smallest-power-of-two-thats-greater-or-equal-to-a-giv
#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';
}
-
https://stackoverflow.com/questions/19665818/generate-random-numbers-using-c11-random-library
-
http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution
-
https://stackoverflow.com/questions/5008804/generating-random-integer-from-a-range
-
Why Aren't
std::uniform_int_distribution<uint8_t>
andstd::uniform_int_distribution<int8_t>
Allowed?
// C++14
std::transform(MyMap.begin(), MyMap.end(),
std::inserter(MySet, MySet.end()),
[](auto pair){ return pair.first; });