[英]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.
如果我做类似B2-= B1的操作,我希望它从B2中删除B1中的所有内容(如果适用)。 I compare if the file objects are the same by checking their ino_t inode and their dev_t devicenumber.
我通过检查它们的ino_t inode和dev_t设备编号来比较文件对象是否相同。
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. 我的问题是,我无法正确地从左侧成功删除文件(在rhs对象可能包含比lhs对象更多的文件的情况下),或者在从向量中删除重复项的情况下。
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 当
SomeClass3-=SomeClass2
应该看起来像
-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 . 您将需要
#include <algorithm>
用于set_difference和#include <iterator>
用于back_inserter 。
Edit: 编辑:
Actually isSameFile
doesn't do quite what set_difference needs it to do. 实际上,
isSameFile
不能完全满足set_difference的需要。 You'll need to use a comparison function to sort the arrays and use that same function for the set_difference. 您将需要使用比较函数对数组进行排序,并将相同的函数用于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. 在下面的评论中,如果只有
getDeviceNumber
可以区分文件,则cmp
可以做到这一点,但是我也看到您也拥有getInode
。 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. 并使用它进行排序并传递给set_difference。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.