【发布时间】:2011-10-19 01:53:36
【问题描述】:
我正在寻找一种通用的、可重用的方法来在 C++ 中对 std::vector 进行洗牌。这是我目前的做法,但我认为它不是很有效,因为它需要一个中间数组并且它需要知道项目类型(本例中为 DeckCard):
srand(time(NULL));
cards_.clear();
while (temp.size() > 0) {
int idx = rand() % temp.size();
DeckCard* card = temp[idx];
cards_.push_back(card);
temp.erase(temp.begin() + idx);
}
【问题讨论】:
-
不。查找 Fisher-yates....
-
尽量不要使用
rand(),有更好的RNG API可用(Boost.Random或0x<random>)。