传递vector的新实例
C++ Pass a new instance of vector
我正在准备一门课的期中考试。我需要初始化一个常量数组的对象,以一个向量作为构造函数参数。基本上,我想做一些类似于以下Java代码的事情:
final Pizza[] standardMenu = {
new Pizza(Arrays.asList(new Integer(1), new Integer(2), new Integer(3))),
new Pizza(Arrays.asList(new Integer(4), new Integer(5), new Integer(6)))};
除了Integer实例,我将传递Ingredient实例。在c++中有这样的方法吗?我的书和快速的谷歌搜索都没有得到任何好的结果。
我现在正在看的代码(c++):
Ingredient standardIngredients[8] = {Ingredient("American Cheese", "Cheese that comes from America", 1.0), Ingredient("Swiss Cheese", "Cheese that is Swiss", 1.0),
Ingredient("Peperoni", "Does it need a description?", 2.0), Ingredient("Ground Beef", "Ground up beef", 3.0),
Ingredient("Onion", "Its a vegetable", 0.5), Ingredient("Black Olives", "Olives that possess the color of blackness", 0.5),
Ingredient("Jalapenios", "Spicy things", 0.5), Ingredient("Pickles", "Just because I can", 0.5)};
Pizza standardMenu[1] = {Pizza({standardIngredients[0], standardIngredients[1]}, "A string", 7.8)};
这对我有用:
class Pizza
{
private:
std::vector<int> m_members;
public:
Pizza(std::vector<int> &members) : m_members(members)
{
}
};
const Pizza pizzaArray[] = { Pizza(std::vector<int>{1, 2, 3}),
Pizza(std::vector<int>{4, 5, 6}), };
是否可以这样帮助:
typedef vector<string> Pizza;
auto menu = vector<Pizza>{ Pizza{ "chorizo", "tomato", "pepperoni" },
Pizza{ "Ananas", "Ham" } };
或者如果你喜欢数组形式的菜单:
Pizza menu[] = { Pizza{ "chorizo", "tomato", "pepperoni" }, Pizza{ "Ananas", "Ham" } };
当然,这是第一步,因为在这个模型中,Pizza只是各部分的总和。如果你想增加更多的信息,比如披萨的价格、卡路里等,最好有一个专门的课程。
编辑:根据你方补充的资料,我建议以下分类:
class Ingredient {
string name;
string description;
double price;
public:
Ingredient(string n, string d, double p) : name(n), description(d), price(p) {}
};
class Pizza {
vector<Ingredient> ingredients;
string name;
double price;
public:
Pizza(vector<Ingredient>i, string n, double p) : ingredients(i), name(n), price(p) {}
};
您的初始化将使用它。
您可以为Pizza定义一个构造函数,它将Ingredient的向量作为参数,或者为Pizza定义一个initializer_list以节省一些输入。
还要注意std::array的使用。在大多数情况下,你不应该再使用c风格的数组,STL容器做了一些很好的事情,比如记住它们的大小。
#include <iostream>
#include <vector>
#include <array>
#include <initializer_list>
using namespace std;
class Ingredient {};
class Pizza {
public:
Pizza(initializer_list<Ingredient> l) : ing{l} {}
Pizza(vector<Ingredient> l) : ing{l} {}
private:
vector<Ingredient> ing;
};
int main()
{
const array<Pizza, 3> pizzas1 {
Pizza{Ingredient{}, Ingredient{}, Ingredient{}},
Pizza{Ingredient{}, Ingredient{}, Ingredient{}},
Pizza{Ingredient{}, Ingredient{}, Ingredient{}}
};
const array<Pizza, 3> pizzas2 {
Pizza{vector<Ingredient>{Ingredient{}}},
Pizza{vector<Ingredient>{Ingredient{}}},
Pizza{vector<Ingredient>{Ingredient{}}}
};
}
相关文章:
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 根据参数创建派生类的新实例
- 我们可以在没有新实例化的情况下声明一个抽象方法来返回抽象超类中的子类对象吗
- 基于现有实例创建类的新实例
- 将unordered_map的新实例插入到unordered_map中
- 如何自动生成结构的新实例
- 在C 中,是否有一种(新的)方法来测试类是否在模板中具有虚拟破坏者而无需实例化实例
- 如何使递归函数在进入下一级循环之前读取每个新实例
- 为什么方法应该返回新实例,而不是修改实例本身
- C 函数中的新实例
- 正确的编码:返回类的新实例时
- 如果我在一个类中有一个静态属性,并创建该类的许多新实例
- 在 C 中声明结构的新实例
- 如何检查是否存在单例实例,如果没有,则不创建新实例?
- malloc是否创建该类的新实例
- 返回矢量的新实例
- "错误:需要类型"创建新实例
- 如何防止其他人在堆上创建类的新实例
- 使用模板创建类的新实例,不知道如何处理错误
- 尝试使用模板创建类的新实例,意外错误