【问题标题】:Are multi-dimensional multi maps possible?多维多图可以吗?
【发布时间】:2020-08-06 21:55:13
【问题描述】:

multimap的所有例子,还没找到多维的例子……

系统:Visual Studio 2019,C++。 Winapi 没有额外的库 预期结果:存储键/值对的多维数组。能够通过指定父密钥对来提取密钥对(类似于 Perl 中的哈希)

使用多映射而不是映射,因为某些字段具有相同的标题。 我的数据示例:

对话ID(整数) | 对话类型(wstring) 线ID(整数) | 线型(wstring) 原线(wstring) 标记线(wstring) 字号 | ...

声明结构:

#include <map>
multimap< int, multimap <int, multimap <wstring, multimap <wstring, multimap <wstring, multimap<wstring, wstring> > > > > > conversation;                        
multimap< int, multimap <int, multimap <wstring, multimap <wstring, multimap <wstring, multimap<wstring, wstring> > > > > > ::iterator conversationIt;         

尝试在其中存储一些东西...(错误:“没有重载函数的实例”)

conversation.insert(make_pair<int,int>(2, 1 make_pair<wstring, wstring>(L"originalLine", line)));

也尝试了以下但发现multimap不支持[]插入:

conversation[0][0][L"originalLine"][line];

【问题讨论】:

  • 你忘了问问题。请收下tour 并阅读How to Ask
  • 编译后的代码必须符合数据结构的事实是一种代码味道。即使只是嵌套六个容器深度也会有问题 IMO。我建议你考虑一些更轻松的东西,除非你追求的是电影风格。
  • 我不确定多图的多图是否有意义。如果我想添加或使用 M[x][y],但 M 包含多个带有键 x 的子图,我应该使用键 y 添加/查看吗?
  • 如果你想使用对或元组作为键,你可以,但只需这样声明:std::map&lt;std::pair&lt;int, int&gt;,...
  • 谢谢。我知道我说的很明显,我有很多关于 multimap 的知识,但如果不是这样,我就不会发布我的问题。我一直在阅读“多地图中的多地图”,这可能是票。还查看了散列、集合、数组、类等。每个级别都可以有多个变量,这将是另一个多重映射。有时像:map1 (conversation(int), line(int)) map2 (map1, conversation type(wstring)) map3 (map1, lineID(int)) map4 (map1:lineID, lineType(wstring)) 如何连接部分一个多图到另一个多图。

标签: c++ multidimensional-array multimap


【解决方案1】:

根据上面的树形图,最好的方法是简单的多维数组。

  • 这允许重复的“键”
  • 混合变量类型可以通过使用支持表来实现(参见下面的示例)

与其他方法相比:

  • 地图不允许重复键
  • 多地图。无法让它发挥作用。尝试了数周。
  • 类继承。不将子分支链接到上面的分支。它只是一种公开变量和方法的方法,而无需重复您的工作。
  • 还研究了多重集、向量等。

注意:对于大型多维数组,您将需要使用堆内存。

void conversationArray(LPWSTR line)
    {
        auto conversation = new int[5][10000][100][50];       // conversationID, lineID, objectID, tagid = tag/text
        wstring conversationType[3];
        auto lineType = new wstring[5][10000];                // conversationID, lineID = string
        wstring text[50];
    
        conversationType[0] = L"chat gui";
        lineType[0][0] = L"chat";
        conversation[0][0][14][12] = 25;
        conversation[0][0][15][12] = 30;
        lineType[0][1] = L"chat1";
        conversation[0][1][15][12] = 500;
        lineType[0][2] = L"chat2";
        conversation[0][2][15][12] = 60;
    
        conversationType[1] = L"chat gui1";
        lineType[1][0] = L"chat-also";
        conversation[1][0][15][12] = 33;
        
        // if tag id = 0 then tag is text
        conversation[0][0][14][0] = 0;
        conversation[0][0][15][0] = 20;
        text[0] = line;
        text[20] = L"dog";
    
        // print out
        int records = 0;
        for (int conversationID = 0; conversationID < 5; conversationID++)
        {
            //sendToReportWindow(L"conversationID: %d\n", conversationID);
    
            for (int lineID = 0; lineID < 10000; lineID++)
            {
                //sendToReportWindow(L"lineID:%d\n", lineID);
    
                for (int objectID = 0; objectID < 100; objectID++)
                {
                    //sendToReportWindow(L"objectID:%d\n", objectID);
    
                    for (int tagID = 0; tagID < 50; tagID++)
                    {
                        if (conversation[conversationID][lineID][objectID][tagID] >= 0)
                        {
                            if (tagID > 0)
                            {
                                sendToReportWindow(L"conversationID:%d type:%s\n", conversationID, conversationType[conversationID].c_str());
                                sendToReportWindow(L"lineID:%d type:%s\n", lineID, lineType[conversationID][lineID].c_str());
                                sendToReportWindow(L"conversation[%d][%d][%d][%d]= %d\n", conversationID, lineID, objectID, tagID, conversation[conversationID][lineID][objectID][tagID]);
                                sendToReportWindow(L"\n");
    
                            }
                            else
                            {
                                sendToReportWindow(L"conversationID:%d type:%s\n", conversationID, conversationType[conversationID].c_str());
                                sendToReportWindow(L"lineID:%d type:%s\n", lineID, lineType[conversationID][lineID].c_str());
                                sendToReportWindow(L"conversation[%d][%d][%d][%d] text = %s\n", conversationID, lineID, objectID, tagID, text[conversation[conversationID][lineID][objectID][tagID]].c_str());
                                sendToReportWindow(L"\n");
                            }
                            records++;
                        }          
                    }
                }
            }
        }
        sendToReportWindow(L"records:%d\n", records);
        sendToReportWindow(L"\n");
    
        // print all records on a specific branch. all lines for conversation 1, line 0
        int conversationID = 1; int lineID = 0;
        sendToReportWindow(L"Just print a subset of conversation:%d and Line:%d\n", conversationID, lineID);
    
        for (int objectID = 0; objectID < 100; objectID++)
        {
            for (int tagID = 0; tagID < 50; tagID++)
            {
                if (conversation[1][0][objectID][tagID] >= 0)
                {
                    if (tagID > 0)
                    {
                        sendToReportWindow(L"conversationID:%d type:%s\n", conversationID, conversationType[conversationID].c_str());
                        sendToReportWindow(L"lineID:%d type:%s\n", lineID, lineType[conversationID][lineID].c_str());
                        sendToReportWindow(L"conversation[%d][%d][%d][%d]= %d\n", conversationID, lineID, objectID, tagID, conversation[conversationID][lineID][objectID][tagID]);
                        sendToReportWindow(L"\n");
    
                    }
                    else
                    {
                        sendToReportWindow(L"conversationID:%d type:%s\n", conversationID, conversationType[conversationID].c_str());
                        sendToReportWindow(L"lineID:%d type:%s\n", lineID, lineType[conversationID][lineID].c_str());
                        sendToReportWindow(L"conversation[%d][%d][%d][%d] text = %s\n", conversationID, lineID, objectID, tagID, text[conversation[conversationID][lineID][objectID][tagID]].c_str());
                        sendToReportWindow(L"\n");
                    }
                }
            }
        }
    
        delete[] conversation; delete[] lineType;
    }

【讨论】:

    猜你喜欢
    • 2012-07-12
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    相关资源
    最近更新 更多