将集合的随机元素添加到列表中并将其从原始集合中删除
add random element of a set to a list and remove it from original set
我有一组字符串,想将该组的随机元素添加到列表中(例如,将扑克牌分发给不同的玩家)
我试过以下方法:
std::set<std::string> remaining_cards;
std::vector<std::set<std::string>> player_cards;
int random_number;
for (int i = 0; i < number_of_players; ++i)
{
random_number = 2; // for simplicity let's assume the random number is always 2
auto it = remaining_cards.cbegin();
std::advance(it, random_number);
player_cards.emplace_back(remaining_cards.cbegin(), it); // get one element
remaining_cards.erase(it); // remove distributed card from deck
}
为什么我用 erase
从牌组中移除了最后一行分发的那张牌,但所有玩家最终得到的都是同一张牌?
我不确定您为什么要使用 std::set
...可能是因为它会自动对卡片进行排序。我会使用 std::vector
并手动排序 (std::sort
)。
我必须填补一些关于您在代码中尝试做的事情的空白,因为您没有 post 一个有效的完整示例。
我建议使用封装和移动抽取的卡片,而不是在删除之前复制。例如
#include <random>
#include <string>
#include <set>
#include <vector>
#include <numeric>
class Random {
private:
std::default_random_engine generator{};
public:
int operator()(int maxValue) { return generator() % maxValue; }
};
class Card {
private:
std::string name{};
public:
Card() : name{} {}
Card& operator= (int i) {
name = std::to_string(i);
return *this;
}
friend bool operator<(Card const& lhs, Card const& rhs) {
return lhs.name < rhs.name; // or some other sorting.
}
};
class Player {
private:
std::set<Card> cards{};
public:
void AddCard(Card&& card) noexcept {
cards.emplace(std::move(card));
}
};
int main() {
//fill the deck
std::vector<Card> deck(42); // instead of remaining cards... why would this be a set?
std::iota(std::begin(deck), std::end(deck), 1); // fill 1 to 42
Random random{};
std::vector<Player> players(4);
while (deck.size() > 0) { // distribute the whole deck.
for (auto& player : players) {
if (deck.empty()) break; // if cards in desck is not equaly dividable between players
auto randIdx = random(deck.size());
player.AddCard(std::move(deck[randIdx])); // move one card
deck.erase(std::next(std::begin(deck), randIdx)); // and remove it from deck
}
}
}
我有一组字符串,想将该组的随机元素添加到列表中(例如,将扑克牌分发给不同的玩家)
我试过以下方法:
std::set<std::string> remaining_cards;
std::vector<std::set<std::string>> player_cards;
int random_number;
for (int i = 0; i < number_of_players; ++i)
{
random_number = 2; // for simplicity let's assume the random number is always 2
auto it = remaining_cards.cbegin();
std::advance(it, random_number);
player_cards.emplace_back(remaining_cards.cbegin(), it); // get one element
remaining_cards.erase(it); // remove distributed card from deck
}
为什么我用 erase
从牌组中移除了最后一行分发的那张牌,但所有玩家最终得到的都是同一张牌?
我不确定您为什么要使用 std::set
...可能是因为它会自动对卡片进行排序。我会使用 std::vector
并手动排序 (std::sort
)。
我必须填补一些关于您在代码中尝试做的事情的空白,因为您没有 post 一个有效的完整示例。
我建议使用封装和移动抽取的卡片,而不是在删除之前复制。例如
#include <random>
#include <string>
#include <set>
#include <vector>
#include <numeric>
class Random {
private:
std::default_random_engine generator{};
public:
int operator()(int maxValue) { return generator() % maxValue; }
};
class Card {
private:
std::string name{};
public:
Card() : name{} {}
Card& operator= (int i) {
name = std::to_string(i);
return *this;
}
friend bool operator<(Card const& lhs, Card const& rhs) {
return lhs.name < rhs.name; // or some other sorting.
}
};
class Player {
private:
std::set<Card> cards{};
public:
void AddCard(Card&& card) noexcept {
cards.emplace(std::move(card));
}
};
int main() {
//fill the deck
std::vector<Card> deck(42); // instead of remaining cards... why would this be a set?
std::iota(std::begin(deck), std::end(deck), 1); // fill 1 to 42
Random random{};
std::vector<Player> players(4);
while (deck.size() > 0) { // distribute the whole deck.
for (auto& player : players) {
if (deck.empty()) break; // if cards in desck is not equaly dividable between players
auto randIdx = random(deck.size());
player.AddCard(std::move(deck[randIdx])); // move one card
deck.erase(std::next(std::begin(deck), randIdx)); // and remove it from deck
}
}
}