Просмотр исходного кода

Merge pull request #181 from HeRaNO/fix-171-wany

fix: use `std::advance` in `wany`
Mike Mirzayanov 7 месяцев назад
Родитель
Сommit
8ff2de3b69
1 измененных файлов с 8 добавлено и 2 удалено
  1. 8 2
      testlib.h

+ 8 - 2
testlib.h

@@ -1105,7 +1105,10 @@ public:
         size_t size = c.size();
         if (size <= 0)
             __testlib_fail("random_t::wany(const Container& c, int type): c.size() must be positive");
-        return *(c.begin() + wnext(size, type));
+        // return *(c.begin() + wnext(size, type));
+        typename Container::const_iterator it = c.begin();
+        std::advance(it, wnext(size, type));
+        return *it;
     }
 
     /* Returns weighted random element from iterator range. */
@@ -1115,7 +1118,10 @@ public:
         if (size <= 0)
             __testlib_fail(
                     "random_t::any(const Iter& begin, const Iter& end, int type): range must have positive length");
-        return *(begin + wnext(size, type));
+        // return *(begin + wnext(size, type));
+        Iter it = begin;
+        std::advance(it, wnext(size, type));
+        return *it;
     }
 
     /* Returns random permutation of the given size (values are between `first` and `first`+size-1)*/