<small id='O4eX5'></small><noframes id='O4eX5'>

    • <bdo id='O4eX5'></bdo><ul id='O4eX5'></ul>

    <legend id='O4eX5'><style id='O4eX5'><dir id='O4eX5'><q id='O4eX5'></q></dir></style></legend>
  1. <i id='O4eX5'><tr id='O4eX5'><dt id='O4eX5'><q id='O4eX5'><span id='O4eX5'><b id='O4eX5'><form id='O4eX5'><ins id='O4eX5'></ins><ul id='O4eX5'></ul><sub id='O4eX5'></sub></form><legend id='O4eX5'></legend><bdo id='O4eX5'><pre id='O4eX5'><center id='O4eX5'></center></pre></bdo></b><th id='O4eX5'></th></span></q></dt></tr></i><div id='O4eX5'><tfoot id='O4eX5'></tfoot><dl id='O4eX5'><fieldset id='O4eX5'></fieldset></dl></div>

      <tfoot id='O4eX5'></tfoot>

      如何从 C++ 容器中获取随机元素?

      时间:2024-05-12
          <tbody id='1TfGC'></tbody>
          <i id='1TfGC'><tr id='1TfGC'><dt id='1TfGC'><q id='1TfGC'><span id='1TfGC'><b id='1TfGC'><form id='1TfGC'><ins id='1TfGC'></ins><ul id='1TfGC'></ul><sub id='1TfGC'></sub></form><legend id='1TfGC'></legend><bdo id='1TfGC'><pre id='1TfGC'><center id='1TfGC'></center></pre></bdo></b><th id='1TfGC'></th></span></q></dt></tr></i><div id='1TfGC'><tfoot id='1TfGC'></tfoot><dl id='1TfGC'><fieldset id='1TfGC'></fieldset></dl></div>
          <legend id='1TfGC'><style id='1TfGC'><dir id='1TfGC'><q id='1TfGC'></q></dir></style></legend>

            <small id='1TfGC'></small><noframes id='1TfGC'>

              <bdo id='1TfGC'></bdo><ul id='1TfGC'></ul>

              • <tfoot id='1TfGC'></tfoot>
                本文介绍了如何从 C++ 容器中获取随机元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                从 STL 范围中获取 [伪] 随机元素的好方法是什么?

                What is a good way to get a [pseudo-]random element from an STL range?

                我能想到的最好方法是做 std::random_shuffle(c.begin(), c.end()) 然后从 c.begin 中取出我的随机元素().

                The best I can come up with is to do std::random_shuffle(c.begin(), c.end()) and then take my random element from c.begin().

                但是,我可能想要一个来自 const 容器的随机元素,或者我可能不想要完全 shuffle 的成本.

                However, I might want a random element from a const container, or I might not want the cost of a full shuffle.

                有更好的方法吗?

                推荐答案

                我在 Google+ 的一篇文章中发布了这个解决方案,其他人引用了这个.把它贴在这里,因为这个比其他的稍微好一点,因为它通过使用 std::uniform_int_distribution 避免了偏见:

                I posted this solution on a Google+ article where someone else referenced this. Posting it here, as this one is slightly better than others because it avoids bias by using std::uniform_int_distribution:

                #include  <random>
                #include  <iterator>
                
                template<typename Iter, typename RandomGenerator>
                Iter select_randomly(Iter start, Iter end, RandomGenerator& g) {
                    std::uniform_int_distribution<> dis(0, std::distance(start, end) - 1);
                    std::advance(start, dis(g));
                    return start;
                }
                
                template<typename Iter>
                Iter select_randomly(Iter start, Iter end) {
                    static std::random_device rd;
                    static std::mt19937 gen(rd());
                    return select_randomly(start, end, gen);
                }
                

                示例用途是:

                #include <vector>
                using namespace std;
                
                vector<int> foo;
                /* .... */
                int r = *select_randomly(foo.begin(), foo.end());
                

                我最终创建了一个遵循类似方法并具有更好设计的要点.

                这篇关于如何从 C++ 容器中获取随机元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:多维 std::array 下一篇:std::strings 的容量()、reserve() &amp;调整大小()函数

                相关文章

                  <tfoot id='3xbCY'></tfoot>
                  <legend id='3xbCY'><style id='3xbCY'><dir id='3xbCY'><q id='3xbCY'></q></dir></style></legend>
                    <bdo id='3xbCY'></bdo><ul id='3xbCY'></ul>
                  1. <i id='3xbCY'><tr id='3xbCY'><dt id='3xbCY'><q id='3xbCY'><span id='3xbCY'><b id='3xbCY'><form id='3xbCY'><ins id='3xbCY'></ins><ul id='3xbCY'></ul><sub id='3xbCY'></sub></form><legend id='3xbCY'></legend><bdo id='3xbCY'><pre id='3xbCY'><center id='3xbCY'></center></pre></bdo></b><th id='3xbCY'></th></span></q></dt></tr></i><div id='3xbCY'><tfoot id='3xbCY'></tfoot><dl id='3xbCY'><fieldset id='3xbCY'></fieldset></dl></div>

                  2. <small id='3xbCY'></small><noframes id='3xbCY'>