带数组的列表(向量)定义
我有班级电子邮件, 她的构造函数中有参数“bcc”。 它实际上是副本电子邮件列表。 这些电子邮件的数量没有固定数量,以后我必须有可能扩展此列表。
//constructor prototype
Email::Email(vector<string> bcc)
所以我想使用向量或列表类型以及函数push_back()。 如何使用密件抄送电子邮件创建新实例?
我实际上需要声明和列表的定义。
我发现这个定义带有整数类型的迭代器:
int myints[] = {16,2,77,29};
Email myEmail(vector<int> (myints, myints + sizeof(myints) / sizeof(int) ));
,但它不是非常用户友好,我需要它与字符串。
有这样的事吗?
Email myEmail(vector<string> ("first","second","third"));
I have Class Email,
there is parameter "bcc" in her constructor.
Its actually list of emails for copies.
There is no fixed number of these emails and later i have to have possibility to extend this list.
//constructor prototype
Email::Email(vector<string> bcc)
So i want to use type vector or list for that and function push_back().
How can i make a new instance with bcc emails?
I need actually declaration with definition for my list.
I've found this definition with iterator for integer type:
int myints[] = {16,2,77,29};
Email myEmail(vector<int> (myints, myints + sizeof(myints) / sizeof(int) ));
, but its not very user friend and i need it with strings.
Is there something like this?
Email myEmail(vector<string> ("first","second","third"));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除了 C++0x 列表初始化之外,还有 Boost.Assign 库应该做类似的事情。
Apart from C++0x list-initialization, there is the Boost.Assign library which should do similar things.
如果你有C++0x,你可以做向量{“第一”,“第二”,“第三”}。否则,您将必须在某个范围内创建一个新向量,并手动推送您想要的每个向量,然后构建。
另外,您确实应该参考该向量,它确实很大。
您应该使用 std::vector ,除非您知道需要将项目插入到中间而不是末尾。
If you have C++0x, you can do vector { "first", "second", "third" }. Else, you will have to create a new vector in scope somewhere and manually push on each that you want, then construct.
Also, you should really take that vector by reference, it's really quite large.
You should use a std::vector unless you know that you will need to insert items into the middle, not on the end.
如果您不使用
C++0x
,那么您无权访问初始化程序。您能否添加一个采用任何旧迭代器的构造函数,即:当然,您没有理由不能拥有一个采用
const BccType& 的
(为了简洁和可维护性而进行ctor
;typedef
编辑)。请注意,我建议通过引用传递此值,以节省复制std::vector
两次。If you're not using
C++0x
then you have no access to initialisers. Could you add a constructor that takes any old iterator, viz:There is, of course, no reason why you can't have a
ctor
that takesconst BccType&
(typedef
ed for brevity and maintainability) additionally. Note that I suggest passing this by reference to save copying thestd::vector
twice.