简体   繁体   English

在std :: map中存储对象

[英]Storing objects in an std::map

I'd like to store objects of a class in an std::map . 我想将一个类的对象存储在std::map Here is a working example showing how I am doing it currenty 这是一个工作示例,展示了我目前的工作方式

#include <iostream>
#include <map>

class A
{
private:
  int a;
  std::string b;

public:
  A(int init_a, std::string init_b) : a(init_a), b(init_b){};  
  void output_a() {std::cout << a << "\n";}
};

int main()
{
  std::map<size_t, A> result_map;
  for (size_t iter = 0; iter < 10; ++iter)
  {
    A a(iter, "bb");
    result_map.insert(std::make_pair(iter, a));
  }

  return 0;
}

I have two question to this example: 我对此示例有两个疑问:

  1. Is this the professional C++-way to store objects in an std::map in the above case? 在上述情况下,这是将对象存储在std::map中的专业C ++方法吗? Or should I create a pointer to an object of A and store that instead? 还是应该创建一个指向A对象的指针并将其存储呢? I like the first (current) option as I don't have to worry about memory management myself by using new and delete - but most importantly I'd like to do things properly. 我喜欢第一个(当前)选项,因为我自己不必担心通过使用newdelete内存管理-但是最重要的是,我想正确地执行操作。

  2. How would I go about calling a member function of, say, result_map[0] ? 我将如何去调用result_map[0]的成员函数? I naively tried result_map[0].output_a() , but that gave me the error: error: no matching function for call to 'A::A()' 我天真地尝试了result_map[0].output_a() ,但这给了我错误: error: no matching function for call to 'A::A()'

Is this the professional C++-way to store objects in an std::map in the above case? 在上述情况下,这是将对象存储在std :: map中的专业C ++方法吗?

It is fine, simpler code could be: 很好,更简单的代码可以是:

result_map.emplace(iter, A(iter, "bb") );

you should use whatever you find more readable. 您应该使用您认为更具可读性的内容。 By the way calling integer counter iter is not a way to write a readable code. 顺便说一下,调用整数计数器iter并不是编写可读代码的方法。

How would I go about calling a member function of, say, result_map[0]? 我将如何调用例如result_map [0]的成员函数?

You better use std::map::find : 您最好使用std::map::find

auto f = result_map.find( 0 );
if( f != result_map.end() ) f->output_a();

problem with operator[] in your case - it has to create and instance if object does not exist with that index but you do not have default ctor for A . 您的情况下operator[]问题-如果该索引不存在object,但是您没有A默认ctor,则它必须创建并实例化。

1- It depends: If your class can be copied and you're not worried about performance issues with copying objects into the map, then that's a good way to do it. 1- 这取决于:如果可以复制您的类,并且您不担心将对象复制到地图中的性能问题,那么这是一种好方法。 However, if say your class held any immutable data ( std::mutex for example) you'd have to use a pointer, as the copy constructor c++ automatically generates would be ill formed, so it merely wouldn't be able to copy the class 但是,如果说您的类包含任何不可变的数据(例如std::mutex ),则您必须使用指针,因为c ++自动生成的复制构造函数将形成错误的格式,因此它将无法复制类

2- result_map.at(0).output_a() or result_map.at(0)->output_a() if you're using a map of pointers 2- result_map.at(0).output_a()result_map.at(0)->output_a()如果您使用的是指针映射)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM