简体   繁体   中英

Accessing elements in a multimap c++

I have a class CFile:

class CFile{
private:
    string filename;
    unsigned int filesize;
public:
    CFile(){
        filename="";
        filesize=0;
    }
    CFile(string name,unsigned int size){
        filename=name;
        filesize=size;
    }
    string getFileName(){
        return filename;
    }
    int getSize(){
        return filesize;
    }
    void ChangeSize(unsigned int size1){
        filesize=size1;
    }
    bool operator ==(CFile a){
        return(a.getFileName().compare(getFileName()))&&(a.getSize()==getSize())?true:false;
    }
    void operator =(CFile a){
        filename=a.getFileName();
        filesize=a.getSize();
    }
    istream &operator>>(istream &in){
        in>>filename>>filesize;
        return in;
    }
    ostream &operator<<(ostream &out){
        return out<<filename<<filesize;
    }
    bool nameCompare(CFile a){
        return (lexicographical_compare(filename.begin(),filename.end(),a.filename.begin(),a.filename.end()))?1:0;
    }
    static bool greather(const CFile& obj1,const CFile& obj2){
        if (obj1.filesize>obj2.filesize) return true;
        else return false;
    }
};

and the i have a class CDirectory:

    class CDirectory{
private:
    string dirname;
    enum FileType {Archive,Hidden,Readonly,System};
    multimap<CFile,FileType> dirmap;

The first class CFile is for the size and name of a file.The second class CDirectory is for the directory name and type of file.Also in the second class there is a multimap with key value the class CFile and mapped value the type of the file FileType.How can I access the elements of the multimap?I don't know how this works when the key value of the multimap is a class.

As @NathanOliver pointed out, multimap might not be the best structure to model your data. But if you are determined to go this way, use equal_range member function to find all values with a given key. See the example for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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