简体   繁体   中英

Trouble trying to override -= operator

So I have two objects, which contain a vector of file objects called files (which may either contain a file or a directory file as well as all files it points to). If I do something like B2 -= B1, I want it to remove everything everything in B1 from B2 if applicable. I compare if the file objects are the same by checking their ino_t inode and their dev_t devicenumber.

Currently this is my code

Someclass& Someclass::operator-=(const Someclass &rhs){
    vector<AnotherClass> retfiles;
        for (auto i = 0; i < static_cast<int>(files.size()); i++){
            for (auto j = 0; j < static_cast<int (rhs.files.size()); j++){

            if (i < j){
                if (!isSameFile(files.at(i), rhs.files.at(i))){
                    retfiles.push_back(files.at(i));
                    }
            }
        }

    files = retfiles; 

    return *this;
}

bool Someclass::isSameFile(const AnotherClass &lhs, const AnotherClass &rhs) { 
    return (lhs.getInode() == rhs.getInode()) && (lhs.getDeviceNumber() == rhs.getDeviceNumber());
}

My issue is that I'm not able to successfully remove the files from the lefthand side correctly (in the case that the rhs object may contain more files than the lhs object) or in the case of removing duplicates from my vector.

SomeClass 1:
-rw-r--r-- pub/tree/alpha/iota/omega
-r--r--r-- pub/tree/alpha/iota/kappa
drwxr-xr-x pub/tree/alpha/iota
SomeClass 2:
-rw-r--r-- pub/tree2/tau/sigma
drwxr-xr-x pub/tree2/tau
-rw-r--r-- /etc/group
SomeClass 3:


When SomeClass3-=SomeClass2 should look like

-rw-r--r-- pub/tree/alpha/iota/omega
-r--r--r-- pub/tree/alpha/iota/kappa
drwxr-xr-x pub/tree/alpha/iota

Finding the elements from an array that don't exist in another array is called the set difference and there is a function in the standard library that can do that.

vector<AnotherClass> retfiles;
std::set_difference(files.begin(), files.end(),
                    rhs.files.begin(), rhs.files.end(),
                    std::back_inserter(retfiles), isSameFile);
// retfiles contains elements from files that didn't exists in rhs.files

You'll need #include <algorithm> for set_difference and #include <iterator> for back_inserter .

Edit:

Actually isSameFile doesn't do quite what set_difference needs it to do. You'll need to use a comparison function to sort the arrays and use that same function for the set_difference.

In your comment below, cmp would do that fine if getDeviceNumber is the only thing that distinguished the files, but I see you have getInode as well. So you might want to make a comparison function like this:

bool cmp(const Fing &lhs, const Fing &rhs) { // typo?
    if (lhs.getDeviceNumber() != rhs.getDeviceNumber())
        return lhs.getDeviceNumber() < rhs.getDeviceNumber();
    else
        return lhs.getInode() < rhs.getInode();
}

and use that to sort with and pass to set_difference.

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