繁体   English   中英

动态将字符串插入到std :: map

[英]Dynamically inserting strings to a std::map

我正在尝试创建文件对映射...首先,我使用FindFirstFile和FindNextFile在指定目录中搜​​索文件,找到文件后,我搜索映射以查看是否存在关联的文件。 如果将另一个文件添加到地图,则将新找到的文件插入到先前找到的文件旁边。 如果未找到关联文件,则将新文件插入到地图中,并保持其对不变。

为了进一步说明:假设我们有2个文件file.1.a和file.1,这些文件代表一对,因此应成对添加到地图上

//map<File w/o .a, File w .a>
std::map<CString, CString> g_map;

int EnumerateFiles(LPCTSTR Dir)
{
   //Search Files....
   //Found a File....(for ex: file.1)
   //Append .a to the string and search for it in the map
   BOOL bAdded = FALSE;
   for(std::map<CString, CString>::iterator itr = g_map.begin(); itr != g_map.end(); itr++)
      {
        if(StrCmp(tchAssocFile, itr->second) == 0)
         {
                bAdded = TRUE;
                //pair the string with the other one;
         }

      }
    if(!bAdded)
       //Add the new string to the map and leave its associate blank


   //Do the same in reverse if the associate was found first....
}

我希望这很清楚,因为我想不出其他任何方式表达它了。

您能帮忙解决这个问题吗?

问候

您有两个文件。
{X}和{X} .a

您想搜索一些目录空间并存储找到的目录空间。

让我们将查找信息存储在std :: pair <bool,bool>中。
第一个值表示是否找到{x},第二个值表示是否找到{X} .a
这些对值使用{X}作为映射的索引存储在映射中。

#include <memory>
#include <string>
#include <map>

typedef std::pair<bool,bool>            FileInfo;
typedef std::map<std::string,FileInfo>  FileMapInfo;



FileMapInfo     fileMapInfo;

void found(std::string const& fileName)
{
    // baseName:   We will use this to lookup if either file is found.
    //             The extension ".a" is removed from this name.
    // aExtension: is true if the file ends with ".a"
    std::string baseName(fileName);
    bool        aExtension(false);

    std::string::size_type pos = fileName.find_last_of(".a");
    if ((pos != std::string::npos) && (pos == fileName.size()-2))
    {
        // Get the real base
        baseName    = fileName.substr(0,fileName.size() - 2);
        aExtension  = true;
    }


    // This looks up the record about the file(s).
    // If it dies not exist it creates an entry with false,false.
    FileInfo&   fileInfo    = fileMapInfo[baseName];

    // Now set the appropriate value to true.
    if (!aExtension)
    {
        fileInfo.first      = true;
    }
    else
    {
        fileInfo.second     = true;
    }
}

int main()
{
    // loop over files.
    // call found(<fileName>);
}

如果效率是一个问题,那么您要么必须维护两个地图(用于两个查询方向),要么使用双向地图(例如一个 )。

但是考虑到您的问题,我认为两组(std :: set)可能会更好。 将找到的.a文件放入一个文件中,将其他的非a文件放入其中,最后,将它们的std :: intersection放入:)。

我想我会做这样的事情:

string tchFile = <name of file you just found>;
string tchAssocFile = <name of associated file if it exists, empty otherwise>;

if (tchAssocFile.empty())
    g_map[tchFile] = "";
else {
    std::map<string, string>::iterator foundAssocIter = g_map.find(tchAssocFile);
    if (foundAssocIter != g_map.end())
        foundAssocIter->second = tchFile;
    else
        g_map[tchFile] = tchAssocFile;
}

暂无
暂无

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

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