使用构造函数将元素插入 std::set
是否可以像 std::list 一样向 std::set 插入一个新元素,例如:
//insert one element named "string" to sublist of mylist
std::list< std::list<string> > mylist;
mylist.push_back(std::list<string>(1, "string"));
现在,mylist 有一个 std::string 类型的元素它的子列表类型为 std::list。
如果 std::set 是 std::list 我的列表的子集,你怎么能做同样的事情,即
std::list<std::set <string>> mylist;
如果你不能那么为什么不呢?
is it possible to insert a new element to std::set like in case of std::list for example:
//insert one element named "string" to sublist of mylist
std::list< std::list<string> > mylist;
mylist.push_back(std::list<string>(1, "string"));
Now, mylist has one element of type std::string in its sub-list of type std::list.
How can you do the same in if std::set is the sub-set of std::list my list i.e
std::list<std::set <string>> mylist;
if you can't then why not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这应该可以解决问题:
有关将
&s
视为数组的合法性和有效性的澄清,请参阅此讨论:字符串 s; &s+1;合法的? UB?I think this should do the trick:
For clarification on the legality and validity of treating
&s
as an array, see this discussion: string s; &s+1; Legal? UB?std::set
没有需要插入元素的构造函数。您能做的最好的事情就是使用范围构造函数:此版本采用
begin
和end
迭代器来描述要插入到集合中的范围。您也可以选择提供排序标准。这不完全是你想要的,但也很接近了。因此,如果您将元素存储在容器v
中,您可以将一个新集合插入到您的列表中,如下所示:std::set
doesn't have a constructor that takes an element to be inserted. The best you can do is use the range constructor:This version takes a
begin
andend
iterator describing the range to be inserted into the set. You can optionally supply a sorting criterion as well. It's not quite what you wanted, but it's close. So if you had your elements stored in a containerv
, you can insert a new set to your list like this:或者,在 C++0x 中:
Or, in C++0x: