C++递归:晚宴座位排列dinner_shuffles方法实现需求
Alright, let's break down how to implement the dinner_shuffles method in C++ to get all permutations of the first n dinner guests as requested.
Step 1: Class Structure
First, we'll need a class that stores the guest names via its constructor, then implements the dinner_shuffles method. Here's the basic framework:
#include <vector> #include <string> #include <algorithm> #include <stdexcept> class DinnerParty { private: std::vector<std::string> m_guests; // Store all guest names public: // Constructor takes the list of guest names DinnerParty(const std::vector<std::string>& guestNames) : m_guests(guestNames) {} // The method we need to implement std::vector<std::string> dinner_shuffles(int n); };
Step 2: Implementing dinner_shuffles
The core of this method is generating all permutations of the first n guests. We can leverage C++'s standard library std::next_permutation to handle the permutation logic efficiently, but we need to make sure we start from a sorted set to capture every possible permutation.
Here's the implementation:
std::vector<std::string> DinnerParty::dinner_shuffles(int n) { // Handle invalid n values first if (n < 1 || n > static_cast<int>(m_guests.size())) { throw std::invalid_argument("n must be between 1 and the number of guests"); // Or return an empty vector if you prefer not to throw: // return {}; } // Extract the first n guests std::vector<std::string> selectedGuests(m_guests.begin(), m_guests.begin() + n); // Sort the selected guests to ensure we start with the lexicographically smallest permutation std::sort(selectedGuests.begin(), selectedGuests.end()); std::vector<std::string> result; // Generate all permutations do { // Build the permutation string by concatenating guest names std::string permutation; for (const auto& name : selectedGuests) { permutation += name; } result.push_back(permutation); } while (std::next_permutation(selectedGuests.begin(), selectedGuests.end())); return result; }
How It Works
- Boundary Checking: We first validate that n is within a valid range (1 to the total number of guests). You can choose to throw an error or return an empty vector based on your needs.
- Selecting Guests: We take the first n names from the stored guest list using iterators.
- Sorting: Sorting the selected guests is crucial because
std::next_permutationgenerates permutations in lex order, starting from the current sequence. If we don't sort first, we'll miss permutations that are lex smaller than the initial unsorted sequence. - Generating Permutations: The
do-whileloop runsstd::next_permutationuntil there are no more permutations left. Each iteration builds a string by concatenating the current permutation of guest names and adds it to the result vector.
Example Usage
Let's test the example you provided:
int main() { DinnerParty party({"a", "b", "c", "d", "e"}); auto shuffles = party.dinner_shuffles(3); // shuffles will contain: "abc", "acb", "bac", "bca", "cab", "cba" return 0; }
This matches exactly the expected output from your example.
Notes
- If your guest names are multi-character (like "Alice", "Bob"), this code still works perfectly—it just concatenates the full names instead of single characters.
- If you wanted to handle circular permutations (where rotations like "abc", "bca", "cab" are considered the same), you'd need to adjust the logic to skip duplicate rotations. But based on your example, it's clear we need all linear permutations, so the current implementation is correct.
内容的提问来源于stack exchange,提问作者Megan Heydari

