简体   繁体   English

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

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

I am trying to create a map of file pairs... First I am searching a specified directory for files using the FindFirstFile and FindNextFile and when a file is found I search the map to see if the associated file is there. 我正在尝试创建文件对映射...首先,我使用FindFirstFile和FindNextFile在指定目录中搜​​索文件,找到文件后,我搜索映射以查看是否存在关联的文件。 If the other file was added to the map, the new found file is inserted beside the previously found one. 如果将另一个文件添加到地图,则将新找到的文件插入到先前找到的文件旁边。 If an associated file was not found, the new file is inserted to the map and its pair is left intact. 如果未找到关联文件,则将新文件插入到地图中,并保持其对不变。

To explain more: lets say we have 2 files file.1.a and file.1 those files represent a pair and thus should be added to the map as a pair 为了进一步说明:假设我们有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....
}

I hope this was clear as I can't think of any other way to put it... sry. 我希望这很清楚,因为我想不出其他任何方式表达它了。

Can you please help in solving this issue... 您能帮忙解决这个问题吗?

regards 问候

You have two files. 您有两个文件。
{X} and {X}.a {X}和{X} .a

You want to search some directory space and store which ones you find. 您想搜索一些目录空间并存储找到的目录空间。

Let us store the information of a find in a std::pair<bool,bool>. 让我们将查找信息存储在std :: pair <bool,bool>中。
The first value represents if we find {x} the second value represents if we find {X}.a 第一个值表示是否找到{x},第二个值表示是否找到{X} .a
These pair values are stored in a map using {X} as the index into the map. 这些对值使用{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>);
}

If efficiency is an issue, you'd either have to maintain two maps (for both directions of queries, or use a bi-directional map (like this one ). 如果效率是一个问题,那么您要么必须维护两个地图(用于两个查询方向),要么使用双向地图(例如一个 )。

But looking at your problem, I think that two sets ( std::set ), might work better. 但是考虑到您的问题,我认为两组(std :: set)可能会更好。 Into one you put found .a files, into the other the non-a files, and at the end, you take the std::intersection of them :). 将找到的.a文件放入一个文件中,将其他的非a文件放入其中,最后,将它们的std :: intersection放入:)。

I think I would do something like this: 我想我会做这样的事情:

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