1.随机数
均匀分布 ,并计算耗时。
#include <vector>
#include <random>
static int get_random(int min_value, int max_value)
{
static std::random_device rd; //生成器
using dist_ = std::uniform_int_distribution<int>; //分布器 (均匀分布 uniform, 正态分布 normal, 二项分布 binomial, 泊松分布poisson)
static dist_ funcDist/* {}*/;
return funcDist(rd, dist_::param_type{ min_value, max_value });
}
static void test_random_2()
{
auto t_begin = chrono::steady_clock::now();
for (int i = 0; i < TOTAL; i++) {
if (i % 200 == 0) {
printf("\n%3d: ", i / 100);
}
//printf("%03d ", test_random_2(1, 999));
get_random(1, 999);
}
auto t_end = chrono::steady_clock::now();
chrono::duration<double, std::ratio<1, 1000>> elap_msec = t_end - t_begin;
std::cout << "\n" << elap_msec.count() << "\n";
}
正态分布的例子
static void test_random_normal()
{
static default_random_engine re(time(NULL)); //默认引擎
static normal_distribution<double> nd(31 /* mean */, 8 /* sigma */);
auto norm = std::bind(nd, re);
vector<int> mn(64);
for (int i = 0; i < 1200; ++i)
++mn[round(norm())]; // 产生随机数
for (int i = 0; i < mn.size(); ++i) {
cout << i << '\t';
for (int j = 0; j < mn[i]; ++j)
cout << '*';
cout << '\n';
}
}
2.类型萃取
1. 类型萃取、类型判断和随机分配的例子
for (int i = 0; i < 81; i++) {
m_gridPos[i] = i;
}
int kk = sizeof(decltype(m_gridPos+0) ); //64位的值为8, 32位为4
int kkk = sizeof(decltype(m_gridPos[0])); //64位、32位都是int的大小,4字节
using T0 = std::decay_t<decltype(m_gridPos + 0)>; //int*
using T1 = std::decay_t<decltype(m_gridPos[0])>; //int
using T2 = std::decay_t<decltype(*m_gridPos)>; //int
using T = decltype(m_gridPos[0]);
if constexpr (std::is_same_v<T1, int>) {
std::cout << "ok. it's same\n"; //输出(类型相同)
}
else {
std::cout << "error!!!\n";
}
if constexpr (!std::is_same_v<T, int>) {
std::cout << "ok. it is not same!\n"; //输出(类型不同)
}
else {
std::cout << "error!!!\n";
}
random_shuffle(m_gridPos, m_gridPos + sizeof(m_gridPos) / sizeof(decltype(m_gridPos[0])));
random_shuffle(m_gridPos, m_gridPos + 81, genRandNum);
2.std::visit
遍历 std::variant
对象中包含的所有对象
void test()
{
std::variant<int, double, std::string> v{ 100.3 };
std::visit([](auto&& arg) {
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, int>)
{
std::cout << "arg is integer, value:" << arg << '\n';
}
else if constexpr (std::is_same_v<T, float>) {
std::cout << "arg is float, value: " << arg << '\n';
}
else if constexpr (std::is_same_v<T, double>) {
std::cout << "arg is double, value:" << arg << "\n";
}
else if constexpr (std::is_same_v<T, std::string>)
{
std::cout << "arg is string, value: " << arg << '\n';
}
else {
std::cout << "arg is unknown!" << arg << "\n";
}
},
v);
}
3.std::copy,一般而言,配合std::ostream_iterator使用比for循环输出效率更高
/*{
std::vector<int> vecDest(5);
std::vector<int> vec = { 1,2,5,10,20 };
std::copy(vec.begin(), vec.end(), &vecDest.front());
std::copy(vecDest.begin(), vecDest.end(), std::ostream_iterator<int>(std::cout, ", "));
}
{
vector<int> v(10);
std::iota(v.begin(), v.end(), 0); //generating a value from 0-9
cout << "before:";
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
*/
auto seed = chrono::system_clock::now().time_since_epoch().count();
{
std::vector<int> l(13);
std::iota(l.begin(), l.end(), 9); //初始化值,从9开始依次递增。
std::shuffle(l.begin(), l.end(), std::default_random_engine(seed)); //c++17 random_shuffle -> shuffle
std::cout << "shuffle: ";
std::copy(l.begin(), l.end(), std::ostream_iterator<int>(std::cout, ", "));
{
std::vector<int> tmp(l.size());
std::copy(l.begin(), l.end(), tmp.begin()); //必须保证目标容器的大小不小于源容器大小
std::cout << endl << "shuffle©: ";
std::copy(tmp.begin(), tmp.end(), std::ostream_iterator<int>(std::cout, ", "));
}
std::cout << endl;
}
注意:std::copy,必须预先设定好目标容器的大小,不能小于源容器大小
4. 获取一个随机数
int get_random(int iMin, int iMax)
{
auto begin = chrono::system_clock::now();
auto seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine re(seed);
std::uniform_int_distribute<int> i_dist(iMin, iMax);
std::this_thread::sleep_for(chrono::nanoseconds(10));
auto end = chrono::system_clock::now();
std::duration<double, std::ratio<1, 1000>> _d(end-begin);
int time_elapse = _d.count();
int ret = i_dist(re); //产生随机数,用于返回。
}
int random = get_random(1, 100);