簡體   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