2009-12-21 109 views
1

我想创建一个文件对的映射...首先,我正在使用FindFirstFile和FindNextFile搜索文件的指定目录,当找到文件时,我搜索地图看看相关的文件是否在那里。如果其他文件已添加到地图中,则新找到的文件将插入先前找到的文件旁边。如果找不到相关文件,则新文件将被插入到地图中,并且其对保持不变。动态插入字符串到std :: map

解释得: 让我们说我们有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.... 
} 

希望这是清楚的,因为我想不出有什么其他的方式来表达它......斯里。

能否请您在解决这个问题上需要帮助...

问候

+0

你实际上没有告诉我们问题是什么。 – 2009-12-21 15:15:32

+0

嗯,就像我说过的,我不知道该如何放置它,但我们假设我想填充一张地图,因为我需要一个字符串对的实际需求......例如,如果我找到一个文件,我会检查是否已将其关联关系添加到地图中,如果是,则将该文件与其关联关联。如果不是,则将该文件添加到新的[行]中,并等待找到关联者并将它们配对。 – 2009-12-21 15:22:03

回答

4

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

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

让我们存储在一个std一个发现的信息::对<布尔,布尔>。
第一个值表示如果我们发现{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>); 
} 
+0

哦,是的,thx真的很有帮助.. – 2009-12-22 06:51:54

1

如果效率是一个问题,你要么必须保持两个地图(用于查询的两个方向,或者使用双 - 方向图(如this one

但看着你的问题,我认为两套(std :: set),可能会更好地工作。进入一个你找到.a文件,到另一个非一个文件,最后,你把他们的std ::交叉:)。

0

我想我会做这样的事情:

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; 
}