Skip to content

Instantly share code, notes, and snippets.

@raspberrypisig
Created July 4, 2024 03:55
Show Gist options
  • Save raspberrypisig/00414e29a81af825fe80b03e18b5c09f to your computer and use it in GitHub Desktop.
Save raspberrypisig/00414e29a81af825fe80b03e18b5c09f to your computer and use it in GitHub Desktop.
In the following snippet, I want the menu variable to have empty content at index 0, the current contents of index 0 to be at index1, and the current contents
of index 1 at index2
#include <iostream>
#include <vector>
#include <string>
// Define a struct to represent menu items
struct MenuItem {
std::string name;
std::vector<std::string> options;
};
int main() {
// Initialize menu items directly in an array
std::vector<MenuItem> menu = {
{"", {}}, // or std::optional<std::string> name; and {std::nullopt, {}},
{"Victoria", {"Melbourne", "Geelong", "Ballarat", "Bendigo"}},
{"New South Wales", {"Sydney", "Newcastle", "Wollongong", "Albury"}}
};
// Example usage
for (const auto& item : menu) {
std::cout << "Menu for " << item.name << ":\n";
for (const auto& option : item.options) {
std::cout << "- " << option << "\n";
}
std::cout << "\n";
}
return 0;
}
// Example usage using range-based for loop and structured bindings
for (const auto& [name, options] : menu) {
if (name) {
std::cout << "Menu for " << *name << ":\n";
} else {
std::cout << "Empty menu item:\n";
}
for (const auto& option : options) {
std::cout << "- " << option << "\n";
}
std::cout << "\n";
}
I have this code
struct MenuItem {
std::string name;
std::vector<std::string> options;
};
int main() {
// Initialize menu items directly in an array
std::vector<MenuItem> menu = {
{"", {}}, // or std::optional<std::string> name; and {std::nullopt, {}},
{"Victoria", {"Melbourne", "Geelong", "Ballarat", "Bendigo"}},
{"New South Wales", {"Sydney", "Newcastle", "Wollongong", "Albury"}}
};
// Example usage
for (const auto& item : menu) {
std::cout << "Menu for " << item.name << ":\n";
for (const auto& option : item.options) {
std::cout << "- " << option << "\n";
}
std::cout << "\n";
}
return 0;
}
I want to create a class called TFTMenu. It needs a class member called menu and it needs to be initialized with the menu variable in the previous
code snipper
#include <iostream>
#include <string>
#include <vector>
struct MenuItem {
std::string name;
std::vector<std::string> options;
};
class TFTMenu {
public:
TFTMenu() = default; // Default constructor
// Method to set the menu and take ownership using std::move
void set_menu(std::vector<MenuItem>&& menu) {
menu_ = std::move(menu);
}
void displayMenu() const {
for (const auto& item : menu_) {
std::cout << "Menu for " << item.name << ":\n";
for (const auto& option : item.options) {
std::cout << "- " << option << "\n";
}
std::cout << "\n";
}
}
private:
std::vector<MenuItem> menu_;
};
int main() {
std::vector<MenuItem> menu = {
{"", {}}, // or std::optional<std::string> name; and {std::nullopt, {}},
{"Victoria", {"Melbourne", "Geelong", "Ballarat", "Bendigo"}},
{"New South Wales", {"Sydney", "Newcastle", "Wollongong", "Albury"}}
};
TFTMenu tftMenu;
tftMenu.set_menu(std::move(menu)); // Set the menu
tftMenu.displayMenu();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment