std :: map< int,std :: set*>>这里my_map [1] = std :: set< int*>(); --->这条线有什么服务
以下是我的代码,想知道该行my_map [1] = std :: set< int*>();
服务?
std::map<int, std::set<int *>> my_map;
int main() {
int i = 10;
int *ptr = &i;
my_map[1] = std::set<int *>();
my_map[1].insert(ptr);
for (std::map<int, std::set<int *>>::const_iterator it = my_map.begin();
it != my_map.end(); it++) {
cout << it->first << "-->";
for (std::set<int *>::const_iterator itt = it->second.begin();
itt != it->second.end(); itt++) {
cout << *(*itt) << "\t";
}
cout << endl;
}
cout << "Hello World";
return 0;
}
Below is my code, wanted to know what does the line my_map[1] = std::set<int*>();
serve?
std::map<int, std::set<int *>> my_map;
int main() {
int i = 10;
int *ptr = &i;
my_map[1] = std::set<int *>();
my_map[1].insert(ptr);
for (std::map<int, std::set<int *>>::const_iterator it = my_map.begin();
it != my_map.end(); it++) {
cout << it->first << "-->";
for (std::set<int *>::const_iterator itt = it->second.begin();
itt != it->second.end(); itt++) {
cout << *(*itt) << "\t";
}
cout << endl;
}
cout << "Hello World";
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如所说,它创建一个空集并将其分配给
my_map [1]
。但是,不需要。如果不存在键的元素,则使用
[]
访问该密钥将创建默认构造的数据元素。此代码:
就足够了。
As said it creates an empty set and assigns it to
my_map[1]
.However, it's not needed. If no element exists for a key, then accessing that key with
[]
will create a default-constructed data-element.This code:
is enough.
它使用键'1'在“ my_map”中创建一个新的空条目,然后返回与此条目相关的值的引用。然后调用'std :: set&lt; int *&gt; :: operator ='方法,以复制
std :: set&lt&lt; int *&gt;()
创建的左图。It create a new empty entry in 'my_map' with key '1', and then return the reference of value related to this entry. And then call the 'std::set<int*>::operator=' method to copy the left one set which created by
std::set<int *>()
to this reference.