这是确定范围的方式从容器中获取随机元素吗?

编程入门 行业动态 更新时间:2024-10-25 04:25:41
本文介绍了这是确定范围的方式从容器中获取随机元素吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 最近我决定尝试新的c ++ 11随机lib,有一件事情来了:当从容器中挑选随机元素时,去掉[rand()%nelemennts]。原因是我想要可重复的生成,当使用rand封装是不存在的,因为

auto set_a = generateSet(nelements1); // generateSet calls rand auto set_b = generateSet(nelements2); //所以set_b由上一行决定:(

所以这就是我想出来的: (注意,这个isnt线程安全,它的设计是安全的,调用generateSet不影响eachother(通过改变rand内部值的状态))

template< typename container_type,typename element_type> class RandElemGetter { const container_type& containter_ref; std :: uniform_int_distribution< size_t> distribution; std :: mt19937 engine; public: RandElemGetter(container_type& container):containster_ref(container),distribution(0,container.size 1) { } element_type get() { return containster_ref [distribution(engine)]; } };

用法:

{ vector< int> v {1,2,3,1701,1729}; vector< int> result; RandElemGetter< vector< int& ,int> reg_v(v); for(size_t i = 0; i< nelements; ++ i) result.push_back(reg_v.get()); }

我知道它不是线程安全的,这不是重点。我想知道是有更好的scoped的方式从随机访问容器获取随机元素。它可以修改可能与std :: advance为所有工作。

解决方案

RandElemGetter(container_type container): containster_ref (0,container.size() - 1)

复制,并存储对该副本的引用。一旦构造函数完成,引用就无效。

您需要存储一个副本,或者通过引用传递参数。这是一个好主意,通过不断的引用传递复杂的对象,以避免不必要的复制。

recently I decided to try out new c++11 random lib, and one thing came to mind... to get rid of the [rand()%nelemennts] when picking random element from container. Reason is that i want repeatable generation, when using rand encapsulation is nonexistant because

auto set_a=generateSet(nelements1); //generateSet calls rand auto set_b=generateSet(nelements2); //so set_b is determined by the previous line :(

So this is what I came up with: (note that this isnt thread safe, it is designed to be safe in a way that calls to generateSet dont affect eachother(through changing state of rand internal value))

template<typename container_type,typename element_type > class RandElemGetter { const container_type& containter_ref; std::uniform_int_distribution<size_t> distribution; std::mt19937 engine; public: RandElemGetter(container_type& container): containter_ref(container),distribution(0,container.size()-1) { } element_type get() { return containter_ref[distribution(engine)]; } };

usage :

{ vector<int> v{1,2,3,1701,1729}; vector<int> result; RandElemGetter<vector<int>,int> reg_v(v); for(size_t i=0;i<nelements;++i) result.push_back(reg_v.get()); }

So is this OK solution? I know it is not thread safe, that is not the point. Im wondering is there better "scoped" way of getting random element from random access container. It could be modified maybe with std::advance to work for all.

解决方案

RandElemGetter(container_type container): containter_ref(container),distribution(0,container.size()-1)

This takes the container by value, creating a temporary copy, and stores a reference to that copy. The reference is invalid once the constructor has finished.

You need to either store a copy, or pass the argument by reference. It's a good idea to pass complex objects by constant reference anyway, to avoid unnecessary copying.

更多推荐

这是确定范围的方式从容器中获取随机元素吗?

本文发布于:2023-11-29 00:23:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644552.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:这是   从容   器中   元素   方式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!