English 中文(简体)
问题:地图
原标题:Problems understanding the use of shared_ptr<void> in std::map

I have the following code:

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <sstream>

using namespace std;

typedef std::map<std::string, std::shared_ptr<void>> M;
typedef std::vector<std::string> S;

void build_word_tree_from_sentences(const S& sentence_list, M& root)
{
    for (const auto& sentence : sentence_list)
    {
        std::string word;
        std::stringstream ss(sentence);
        M* base = &root;
        while (ss >> word)
        {
            auto found = base->find(word);
            if (found == base->end())
            {
                base->insert(std::make_pair(word, std::make_shared<M>()));
            }

            auto b = base->find(word)->second;
            base = std::static_pointer_cast<M>(b).get();
        }
    }
}

int main()
{
    S sentence_list = {"Hello word", "Hello there"};
    M tree;
    build_word_tree_from_sentences(sentence_list, tree);
}

I would like to have a help in understanding the use of the shared_ptr in the map M. When inserting a new element, if not already present, in the map:

base->insert(std::make_pair(word, std::make_shared<M>()));

共有的买家用空话,这也使我感到困惑。

问题回答

您的问题要求研究<代码>的一些特征:共享_ptr,然后回答。

https://en.cppvis.com/w/cpp/memory/commd_ptr”rel=“nofollow noreferer”>std:commd_ptr 这是一个聪明的点子,通过点子保留对物体的共同所有权。 几个<代码>std:共有_ptr 物体可拥有同一物体。 该物体被销毁,其记忆在下列任一情况下被存放:

  • the last remaining std::shared_ptr owning the object is destroyed;
  • the last remaining std::shared_ptr owning the object is assigned to another pointer.

这一和其他聪明点人的基本想法是,允许客户使用他们作为本地点人,但提供自动记忆管理的额外特征。 他们特别利用SBRM

这一机制对集装箱极为有用,因为它们与<编码> 价值>的物体——类型合作,而不是最终与物体提及的外部储存合作。 例如,在切碎作业中,集装箱销毁了该物体(在这种情况下,含有该物体的整个代号被销毁,然后处置)。 如果使用一名本地点人来指外部物体,因为点子被销毁,那么外部储存不会被分配,导致记忆泄露。 除其他外,使用一名本地人将无法在不同集装箱之间分享资源,例如<代码>std:共有_ptr<>。

由于物体删除程序是类型,std:共有_ptr 可专门处理不完整的类型<代码> 撤销,使客户能够以与原住点<代码> 避免*相同的方式提及任何物体。

Calling the std:make_commd() 由于<代码>M并不是一个阵列,因此没有争论地分配了这种类型的价值初步标的。 此外,该功能在性能和空间使用方面提供较小的优势,因为它将记忆与物体一起分配给参照台。

我认为,该守则的制定是为了建立<代码>st:map,其中载有std:map的物体,以便创建树苗,树子是树木本身,等等。 然而,<代码>的<代码>下定义(>打成型>)载于<代码>,其原因是,由于专业本身,因此可以重新进行。

using M = std::map<std::string, M>>;

因此,必须界定<条码>被打成型<>>> 类型<>/代码>,将其作为一个空白点,可在无需界定的情况下参照<条码>。 虽然使用<代码>std:共有_ptr可能似乎不合适,因为资源共享从来不出现在代码中,但倾向于作为<代码>std:unique_ptr<>/code>。 页: 1 这只是一种妥协。





相关问题
VS2010 RC - only 100 std::map elements in debugger

I have a small problem during debugging my App in VS 2010 RC when I want to see all the elements of std::map container. When debugger reaches the breakpoint and I want to check the values of the map ...

Java equivalent of C++ std::map?

I m looking for a Java class with the characteristics of C++ std::map s usual implementation (as I understand it, a self-balancing binary search tree): O(log n) performance for insertion/removal/...

Is there any generic vesion of HashTable?

I need a class that will work like C++ std::map. More specifically, I need such a behavior: map< string, vector<int> > my_map; Is it possible?

Thread safety of std::map for read-only operations

I have a std::map that I use to map values (field ID s) to a human readable string. This map is initialised once when my program starts before any other threads are started, and after that it is never ...

How to get the first n elements of a std::map

Since there is no .resize() member function in C++ std::map I was wondering, how one can get a std::map with at most n elements. The obvious solution is to create a loop from 0 to n and use the nth ...

Crash when utilising a std:map

In my SDL program, I am using a map construct to simulate an "infinite" array of objects within a certain class. The code manages to compile fine, but when I run the program, as soon as one of the ...

热门标签