【问题标题】:Using pointer to class objects as key in a multimap example在多映射示例中使用指向类对象的指针作为键
【发布时间】:2016-03-21 15:39:55
【问题描述】:

我已经阅读了很多,但我似乎无法理解这应该如何工作。这个程序原本是使用一个成员multimap<CFile, Filetype>,但是我需要将它改造成multimap<CFile*, Filetype>。根据我对指针的一点了解,我需要制作一个多映射,它以指向 CFile 对象的指针作为键,但我未能实现这一点。我实际上将指针放在了CDirectory 构造函数的多映射中,但是当我尝试打印 CDirectory 的对象时,程序崩溃了,所以(许多错误中的一个)是我为CDirectory 类重载了ostream operator<<。我真的不知道从哪里开始,我得到了基本的指针逻辑,但我似乎无法实现它。


class CFile {
    string m_strFile;
    unsigned int m_size;
public:
   /*constructors, get() and set() functions are
     implemented but I've deleted them for the example */

    bool operator< (const CFile& obj) const {
        return (m_strFile < obj.m_strFile);
    }
    bool operator== (const CFile& obj) const {
        return (m_size == obj.m_size);
    }
    friend ostream& operator<< (ostream& ost, const CFile& obj) {
        return ost << "name: " << obj.m_strFile << ", size: " << obj.m_size;
    }
    friend istream& operator>> (istream& ist, CFile& obj) {
        return ist >> obj.m_strFile >> obj.m_size;
    }
};

class CDirectory {
    string m_strDirectory;
    enum class Filetype {
        Archive, Hidden, ReadOnly, System, FileNotSupported
    };
    multimap <CFile*, Filetype> m_DirectoryMap;
public:
    /* overloading operator<< for class CDirectory and uses the friend function filetypeToString to convert the enum value to string */
    friend std::ostream& operator<<(std::ostream &os, const CDirectory &dir) {
        os << dir.m_strDirectory << "\n";
        auto p = m_DirectoryMap.begin();
        while ( p != m_DirectoryMap.end()) {
           os << p->first->getFileName() << '\t' << p->first->getFileSize() << '\t' <<  CDirectory::filetypeToString(p->second) << '\n';
           ++p;
        }
        return os;
    }
    /* comparator function, used to find the min and max files by size
       - takes 2 pairs of the multimap and compares their CFile objects filesize */
    static bool Greater(const pair<const CFile, Filetype>& a,
                    const pair<const CFile, Filetype>& b) {
    return (a.first.getFileSize() < b.first.getFileSize());
    }
    /* explicit constructor - reads data from a file and inserts pairs
      of types pair <CFile, enum Filetype> in a multimap */
    CDirectory (const string& n) {
        fp.open (n, ios::in);
        if (!fp) {
            throw std::runtime_error("Could not open file");
        }
        string dirName, fileName,  fType;
        int fileSize;
        Filetype filetype;
        fp >> dirName;
        m_strDirectory = dirName;
        while (fp >> fileName >> fileSize >> fType) {
            CFile obj (fileName, fileSize);
            CFile* ptr = &obj;
            if (fType == "Archive")
                filetype = Filetype::Archive;
            else if (fType == "Hidden")
                filetype = Filetype::Hidden;
            else if (fType == "ReadOnly")
                filetype = Filetype::ReadOnly;
            else if (fType == "System")
                filetype = Filetype::System;
            else
                filetype = Filetype::FileNotSupported;
            m_DirectoryMap.insert(pair<CFile*, Filetype>(ptr, Filetype(filetype)));
        }
    }
    string getDirectory () const { return m_strDirectory; }
    void printMap () {
         auto p = m_DirectoryMap.begin();
         cout << m_strDirectory << endl;
         while ( p != m_DirectoryMap.end()) {
                cout << endl << p->first->getFileName() << '\t' << p->first->getFileSize() << '\t' << filetypeToString(p->second) << endl;
                ++p;
        }
    }
    int countDuplicates( const string& strToCount ) const {
        CFile obj (strToCount, 0);
        CFile* ptr = &obj;
        int numberOfDuplicates = m_DirectoryMap.count(ptr);

        if (numberOfDuplicates > 1)
            return numberOfDuplicates;
        else if (numberOfDuplicates == 1)
            return 1;
        else
            return 0;
    }
     void removeDuplicates( const string& strToRemove ) {
        CFile obj (strToRemove, 0);
        CFile* ptr = &obj;
        pair <multimap<CFile*,Filetype>::iterator, multimap<CFile*,Filetype>::iterator> range;
        range = m_DirectoryMap.equal_range(ptr);
        auto it = range.first;
        ++it;
        while (it != range.second)
            it = m_DirectoryMap.erase(it);
    }
 /*   CFile findMaxSize() const {
        multimap<CFile*, Filetype>::const_iterator result;
         result = std::max_element(m_DirectoryMap.begin(), m_DirectoryMap.end(), Greater);
         CFile obj(result.first->getFileName(), result.first->getFileSize());
         return obj;
    }
    CFile findMinSize() const {
        multimap<CFile*, Filetype>::const_iterator result;
        result = std::min_element(m_DirectoryMap.begin(), m_DirectoryMap.end(), Greater);
        CFile obj(result.first->getFileName(), result.first->getFileSize());
        return obj;
    } */
    static std::string filetypeToString(Filetype type) {
        switch (type) {
            case Filetype::Archive:
                return "archive";
                break;
            case Filetype::Hidden:
                return "hidden";
                break;
            case Filetype::ReadOnly:
                return "read-only";
                break;
            case Filetype::System:
                return "system";
                break;
            case Filetype::FileNotSupported:
                return "not-supported";
                break;
        }
    }
};

int main () {
    /* - Catching if the file exists, prompt the user untill a correct name is given
       - Making an object of type CDirectory, reading data from a file and inserting
       pairs of <CFile, Filetype> in a multimap.
       - Counting the number of duplicated filenames and removing them (leaving only 1).
       - Finding the min and max files by size and printing their objects. */
    string filename = "";
    int numberOfDuplicates = 0;
    cout << "Please enter input file name: \n";
    string iname = "";
    bool done = false;
    CDirectory obj();
    while (!done && cin >> iname) {
        ifstream ist{iname};
        try {
            CDirectory obj(iname);
            done = true;

            cout << "The original multimap (ordered by filename) contains the following data: \n\n";
            system("pause");
            cout << obj;

            cout << "\n\nCheck if the file has any duplicates. Enter a filename:\n\n";
            do  {
                cin >> filename;
                numberOfDuplicates = obj.countDuplicates(filename);
                if ( numberOfDuplicates > 1) {
                    cout << "The file has " << numberOfDuplicates << " duplicates.";
                    cout << "Removing duplicates of " << filename << ". \n\n";
                }
                else if (numberOfDuplicates == 1)
                    cout << "The file " << filename << " does not have any duplicates.\n\n";
                else if (numberOfDuplicates == 0)
                    cout << "The file " << filename << " is not in the multimap. Please enter a new filename:\n\n";
            } while (!(numberOfDuplicates > 0));

            system("pause");
            obj.removeDuplicates(filename);
            cout << "The updated multimap (ordered by filename) contains the following data: \n\n";
            cout << obj;

        }  catch (std::exception &ex) {
            std::cout << ex.what() << "!\n" << "Please try again.\n";
            }
    }
    getch();
    return 0;
}

【问题讨论】:

    标签: c++ pointers multimap


    【解决方案1】:

    while (fp >> fileName >> fileSize >> fType) {
        CFile obj (fileName, fileSize);
        CFile* ptr = &obj;
        if (fType == "Archive")
            filetype = Filetype::Archive;
        else if (fType == "Hidden")
            filetype = Filetype::Hidden;
        else if (fType == "ReadOnly")
            filetype = Filetype::ReadOnly;
        else if (fType == "System")
            filetype = Filetype::System;
        else
            filetype = Filetype::FileNotSupported;
        m_DirectoryMap.insert(pair<CFile*, Filetype>(ptr, Filetype(filetype)));
    }
    

    你创建了一个循环局部变量obj

    CFile obj (fileName, fileSize);
    

    然后你用

    存储一个指向该对象的指针
    CFile* ptr = &obj;
    //...
    m_DirectoryMap.insert(pair<CFile*, Filetype>(ptr, Filetype(filetype)));
    

    然后你重新开始循环。不幸的是,当您到达循环结束时,所有循环对象都被销毁,然后在循环从头开始时重新创建它们。这意味着地图现在有一个指向已销毁对象的指针。该指针不再有效,使用它是未定义的行为。

    一个快速的解决方法是创建一个指针而不是像这样的自动对象

    CFile* obj = new CFile(fileName, fileSize);
    

    然后将该指针存储在地图中。这确实意味着您需要在完成映射后清理分配的内存。您需要遍历地图并在每个键上调用 delete

    然后我们在使用时遇到另一个问题

    multimap <CFile*, Filetype> m_DirectoryMap;
    

    映射将按指针所持有的地址而不是指向CFile 的地址对键进行排序。要使地图按实际的CFile 排序,您需要编写一个自定义比较器,该比较器将采用两个CFile* 并返回指向CFiles 的比较。

    【讨论】:

    • 在没有适当比较器的情况下,很少使用指针作为映射键是个好主意。
    • @SergeyA 已添加到有关该问题的答案中。
    • @NathanOliver,哦.. 对。我尝试了创建指针并存储它们的方法,并将结构作为自定义比较器 - 所以这对我来说似乎很有效。我还有 2 个问题 - 在我的程序中究竟应该在哪里迭代地图并删除所有指针,在 main 的末尾?在计算最小值和最大值的函数中,我收到一个错误'std::multimap&lt;CFile*, CDirectory::Filetype&gt;::const_iterator' has no member named 'first' - 你知道这是为什么吗?感谢您迄今为止的帮助!
    • 指针的删除应该在CDirectory的析构函数中完成。由于result 是一个迭代器,你需要使用result-&gt;first-&gt;getFileName() 作为一个迭代器,它就像一个指针。
    • @NathanOliver,是的,刚刚调试了它,我还需要更改我的 Greater() 自定义比较器,以返回 return (a.first-&gt;getFileSize() &lt; b.first-&gt;getFileSize()); 现在它似乎工作得很好。 :)) 非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2018-02-20
    • 2015-07-18
    • 2014-07-06
    • 2011-09-09
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 2017-04-26
    相关资源
    最近更新 更多