简体   繁体   English

从地图内的stl地图中删除...在方法内

[英]Deleting From stl map inside a map… inside a method

so i'm trying to implement a File-system in c++. 所以我正在尝试在c ++中实现文件系统。 i have a Directory Class which holds a map. 我有一个包含地图的目录类。 each directory can hold inside it's map another directory (so i have a map inside a map) 每个目录可以在其地图内保存另一个目录(因此我在地图内有一个地图)

i'm trying to delete a file/directory, inside the method it deletes and all is well, but the method is done my main map doesn't update... 我正在尝试删除文件/目录,在它删除的方法内部,一切都很好,但是该方法已完成,我的主地图没有更新...

here the relevant code: 这里是相关代码:

Directory Class (implemented using Composite): 目录类(使用Composite实现):

class Directory : public FileComponent
{
private:
std::map<std::string,FileComponent*> directoryList;
public:
std::map<std::string, FileComponent*> &getMap() {
    return directoryList;
}

class fileSystem: 类文件系统:

private:
FileComponent* find(Mode mode, const std::string& newDirectory, const std::string& directoryName, std::map<std::string, FileComponent*> check);
std::map<std::string,FileComponent*> fileSystem;

}; };

here's what i tried to do (finding a file and copying it...) 这是我尝试做的事情(查找文件并将其复制...)

void filesys::copy(const std::string& fileName, const std::string& directoryName){
//looking for file
mapitr itr = fileSystem.find(fileName);
if (itr != fileSystem.end()){
    fileSystem.erase(itr);
    //file found, looikng for directory
}
else{
    for (itr = fileSystem.begin(); itr != fileSystem.end(); itr++){
        FileComponent* toCopy = find(CopyFile, fileName, directoryName, itr->second->getMap());
        if (toCopy != nullptr)
            //found! need to copy
            break;
    }
}

} }

and my find method: 和我的查找方法:

FileComponent* filesys::find(Mode mode,const std::string& newDirectory, const std::string& directoryName, std::map<std::string,FileComponent*> check){
else if (mode == CopyFile){
     mapitr itr = check.find(newDirectory);
     if (itr != check.end()){
         FileComponent* toCopy = itr->second;
         check.erase(itr);
         return toCopy;
     }
     else{
         for (itr = check.begin(); itr != check.end(); itr++){
             FileComponent* toCopy;
             try{
             toCopy = find(CopyFile, newDirectory, directoryName, itr->second->getMap());
             if (toCopy != nullptr){
                 return toCopy;
             }
             }
             catch (mExceptions& e){
                 e.what();
             }

         }
         return nullptr;
     }
 }

} }

inside the function it finds and erases and all seems to be in order... however once returning the directory/file is still inside my filesystem... i'm returning my inside map by reference... so i really am out of ideas why it doesn't want to work... 在函数中查找和擦除,似乎一切都井井有条...但是一旦返回目录/文件仍然在我的文件系统中...我正在通过引用返回我的内部地图...所以我真的不在了为什么它不想工作的想法...

thanks in advance! 提前致谢!

You're passing the argument check (the map) by value to the find function. 您正在按将参数check (地图)传递给find函数。 That means the data is copied and you only work on the copy and not the original. 这意味着数据已复制 ,您只能处理副本,而不能处理原始数据。 You should pass it by *reference instead: 您应该通过* reference传递它:

std::map<std::string,FileComponent*>& check

On an unrelated note, the function name find is not a good name, it doesn't really describes what the function really does, which is find and erase. 无关紧要的是,函数名称find不是一个好名字,它并没有真正描述该函数实际执行的功能,即查找擦除。

In

filesys::find(Mode mode,
              const std::string& newDirectory,
              const std::string& directoryName,
              std::map<std::string, FileComponent*> check)

you pass check by value, not by reference, so you modify a copy, not the original. 您通过值而不是引用来通过check ,因此您修改的是副本,而不是原始副本。

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

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