std :: map< int,std :: set*>>这里my_map [1] = std :: set< int*>(); --->这条线有什么服务

发布于 2025-01-24 21:50:43 字数 683 浏览 5 评论 0原文

以下是我的代码,想知道该行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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丘比特射中我 2025-01-31 21:50:44

如所说,它创建一个空集并将其分配给my_map [1]

但是,不需要。如果不存在键的元素,则使用[]访问该密钥将创建默认构造的数据元素。

此代码:

    int i = 10;
    my_map[1].insert(&i);

就足够了。

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:

    int i = 10;
    my_map[1].insert(&i);

is enough.

以歌曲疗慰 2025-01-31 21:50:44

它使用键'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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文