generate、generate_n、sample、iota

#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <random> template<class Container>
void write_to_cout(Container& container, const char* delimiter = " ")
{
std::copy(container.begin(), container.end(),
std::ostream_iterator<typename Container::value_type>(std::cout, delimiter) );
} void test0()
{ std::vector<int> v(5, 10);
v.reserve(10); std::fill_n(std::back_inserter(v), 10, 111); write_to_cout(v);
std::cout << std::endl << std::endl; std::fill(v.begin() + 5, v.begin() + 7, 13); write_to_cout(v);
std::cout << std::endl << std::endl;
} void test1()
{
std::vector<int> v(10); std::iota(v.begin(), v.end(), 0); write_to_cout(v);
std::cout << std::endl << std::endl;
} void test2()
{
std::mt19937 rng( std::random_device{}() );
std::uniform_int_distribution<int> d( 0,20 ); std::vector<int> v;
std::generate_n( std::back_inserter(v), 5, [val = 0]() mutable
{
const auto old = val;
val += 3;
return old;
} ); write_to_cout(v, " | ");
std::cout << std::endl << std::endl;
} void test3()
{
std::vector<std::string> b = {"zero", "one", "two", "three", "four", "five", "six", "seven"}; std::mt19937 rng( std::random_device{}() ); write_to_cout(b);
std::cout << std::endl; std::vector<std::string> v;
// test algrithm c++17
std::sample( b.begin(), b.end(), back_inserter(v), 4, rng ); write_to_cout(v);
std::cout << std::endl << std::endl;
} int main()
{
test0();
test1();
test2();
test3(); return 0;
}

C++ STD Gems06的更多相关文章

  1. 【NX二次开发】NX内部函数,libuifw.dll文件中的内部函数

    本文分为两部分:"带参数的函数"和 "带修饰的函数". 浏览这篇博客前请先阅读: [NX二次开发]NX内部函数,查找内部函数的方法 带参数的函数: void U ...

  2. C++ std::set

    std::set template < class T, // set::key_type/value_type class Compare = less<T>, // set::k ...

  3. C++ std::priority_queue

    std::priority_queue template <class T, class Container = vector<T>, class Compare = less< ...

  4. C++ std::queue

    std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...

  5. C++ std::multimap

    std::multimap template < class Key, // multimap::key_type class T, // multimap::mapped_type class ...

  6. C++ std::map

    std::map template < class Key, // map::key_type class T, // map::mapped_type class Compare = less ...

  7. C++ std::list

    std::list template < class T, class Alloc = allocator > class list; List Lists are sequence co ...

  8. C++ std::forward_list

    std::forward_list template < class T, class Alloc = allocator > class forward_list; Forward li ...

  9. C++ std::deque

    std::deque template < class T, class Alloc = allocator > class deque; Double ended queue deque ...

随机推荐

  1. php 语法错误定位 try catch Throwable

    try { } catch (Exception $ex) {      // 计算错误 }  catch (Throwable $ex) { // 语法错误,致命错误 }   Throwable { ...

  2. greenplum 存储过程 索引信息

    涉及的索引表 参考:http://blog.nbhao.org/1539.html pg_index pg_indexes pg_stat_all_indexes # 记录当前数据库中所有的索引的使用 ...

  3. C++ 动态多态

    背景 以前的学习,只是简单地知道:面向对象的三大特性(封装.继承.多态) ,在项目开发中,用到了多态而自己却不知道. 多态(Polymorphism)按字面的意思就是"多种状态". ...

  4. 基于RabbitMQ的MQTT协议及应用

    MQTT的开源代码地址先贴在这里:https://github.com/mqtt/mqtt.github.io/wiki/servers MQTT定义: MQTT(Message Queuing Te ...

  5. Linux基础命令、软件安装

    常用命令 查看当前系统中存在哪些shell cat /etc/shells [root@** ~]# cat /etc/shells /bin/sh /bin/bash /usr/bin/sh /us ...

  6. println 与 print区别

    ------------恢复内容开始------------ println 与 print区别: 1.print输出之后不换行,如下: public class Newstart {    publ ...

  7. 011、MySQL取14天前Unix时间戳

    #取14天前时间戳 SELECT unix_timestamp( DATE_SUB( curdate( ), INTERVAL DAY ) ); 效果如下: 不忘初心,如果您认为这篇文章有价值,认同作 ...

  8. html5移动端主流适配方案

    1.流式布局(百分比布局)    案例:京东移动端 优点:简单方便,只需要固定高度,宽度自适应: 缺点:大屏幕手机实际显示的不协调. 2.响应式布局 优点:可以节约成本,不用再做专门的web app网 ...

  9. 自己写个tween

    public Vector3 begin,end;//起始终止坐标 public float BtoE_time;//用时 float timer,lerp;//计时器和进度值 void Update ...

  10. [题解] LuoguP3768 简单的数学题

    Description 传送门 给一个整数\(n\),让你求 \[ \sum\limits_{i=1}^n \sum\limits_{j=1}^n ij\gcd(i,j) \] 对一个大质数\(p\) ...