基于对象属性将指针向量排序到对象 [英] Sort a Vector of Pointers to an object, based on objects properties
问题描述
我正在尝试为
课程创建一个模拟目录结构应用程序。
我使用Vector来存储指向Directory对象的指针( as
当前对象的子目录)。
在我的标题中看起来像这样(私有部分)
vector< Directory *目录;
在构造函数的cpp文件中我有这个:
Directories.clear();
要向Vector添加目录,我有一个函数要求输入目录的名称
并检查它的有效性等,然后将其传递给
使用此函数创建和存储新的子目录:
bool目录::创建(字符串DN)//传入目录名
{
目录* ND =新目录(此,DN);
Directories.push_back(ND);
NumSubDirs ++;
sort(Directories.begin(),Directories.end());
返回true;
}
如你所见,我正在调用sort函数。正如您可能从
中猜到的那样,我发布排序的事实不起作用。我99%肯定它是
因为我需要提出正确的方法来重载<
运算符和运算符。
当调用sort时,值会改变位置,它们根据要求不按目录名称排序
。
I具有以下重载运算符<和操作员功能。 (我
不包括标题)哪些不能解决问题。
bool Directory :: operator<(const Directory& D)
{
返回D.Name<姓名;
}
bool目录::运营商<(const目录* D)
{
返回D->名称< this->姓名;
}
/ *
运营商>
* /
bool目录::运算符>(const目录& D)
{
返回名称D.Name;
}
bool目录::运算符>(const目录* D)
{
返回D->名称名称;
}
我确定这是问题所在,我只是不知道如何告诉它
正确的事情。 。
谢谢。
I am trying to make a simulated directory structure application for a
course.
I am using a Vector to store pointers to my Directory objects (as
subdirectories of the current object).
In my header it looks like this (private section)
vector<Directory*Directories;
In my cpp file in the constructor I have this:
Directories.clear();
To add directories to the Vector I have a function that asks for name
of the directory and checks it for validity etc. then passes it off to
create and store the new subdirectory using this function:
bool Directory::Create(string DN) //pass in directory name
{
Directory *ND = new Directory(this, DN);
Directories.push_back(ND);
NumSubDirs++;
sort(Directories.begin(), Directories.end());
return true;
}
As you can see I am calling the sort function. As you may guess from
the fact that I am posting the sort does not work. I am 99% sure it''s
because I need to come up with the correct method of overloading the <
operator and the operator.
When sort is called the values change position, they just aren''t sorted
by directory name as requested.
I have the following overloaded operator< and operatorfunctions. (I
am not including the headers) which do NOT do the trick.
bool Directory::operator<(const Directory & D)
{
return D.Name < Name;
}
bool Directory::operator<(const Directory *D)
{
return D->Name < this->Name;
}
/*
operator>
*/
bool Directory::operator>(const Directory & D)
{
return Name D.Name;
}
bool Directory::operator>(const Directory * D)
{
return D->Name Name;
}
I am sure that''s the problem, I just don''t know how to tell it the
correct thing to do...
Thanks.
推荐答案
sa *** @ murdocks.on.ca 写道:
我正在尝试制作模拟目录结构申请一个
课程。
我使用Vector来存储指向我的Directory对象的指针(作为
子目录的当前对象)。
在我的标题中它看起来像这样(私人部分)
vector<目录*目录;
在我的构造函数中的cpp文件中我有这个:
Directories.clear();
要向Vector添加目录,我有一个函数,要求输入目录名称
并检查其有效性等,然后将其传递给
create和使用此函数存储新的子目录:
bool目录::创建(字符串DN)//传入目录名
{
目录* ND =新目录(this,DN);
Directories.push_back(ND);
NumSubDirs ++;
sort(Directories.begin(),Directories.end());
返回true;
}
如您所见,我正在调用sort函数。正如您可能从
中猜到的那样,我发布排序的事实不起作用。我99%肯定它是
因为我需要提出正确的方法来重载<
运算符和运算符。
当调用sort时,值会改变位置,它们根本不是
按目录名称排序。
我具有以下重载运算符<和操作员功能。 (我
不包括标题)哪些不能解决问题。
bool Directory :: operator<(const Directory& D)
{
返回D.Name<姓名;
}
bool目录::运营商<(const目录* D)
{
返回D->名称< this->姓名;
}
/ *
运营商>
* /
bool目录::运算符>(const目录& D)
{
返回名称D.Name;
}
bool目录::运算符>(const目录* D)
{
返回D->名称名称;
}
我确定这是问题,我只是不知道如何告诉它
正确的事情要做...
I am trying to make a simulated directory structure application for a
course.
I am using a Vector to store pointers to my Directory objects (as
subdirectories of the current object).
In my header it looks like this (private section)
vector<Directory*Directories;
In my cpp file in the constructor I have this:
Directories.clear();
To add directories to the Vector I have a function that asks for name
of the directory and checks it for validity etc. then passes it off to
create and store the new subdirectory using this function:
bool Directory::Create(string DN) //pass in directory name
{
Directory *ND = new Directory(this, DN);
Directories.push_back(ND);
NumSubDirs++;
sort(Directories.begin(), Directories.end());
return true;
}
As you can see I am calling the sort function. As you may guess from
the fact that I am posting the sort does not work. I am 99% sure it''s
because I need to come up with the correct method of overloading the <
operator and the operator.
When sort is called the values change position, they just aren''t
sorted by directory name as requested.
I have the following overloaded operator< and operatorfunctions. (I
am not including the headers) which do NOT do the trick.
bool Directory::operator<(const Directory & D)
{
return D.Name < Name;
}
bool Directory::operator<(const Directory *D)
{
return D->Name < this->Name;
}
/*
operator>
*/
bool Directory::operator>(const Directory & D)
{
return Name D.Name;
}
bool Directory::operator>(const Directory * D)
{
return D->Name Name;
}
I am sure that''s the problem, I just don''t know how to tell it the
correct thing to do...
常规''sort''不会比较指针后面的对象。
它只是比较指针。你需要的是一个自定义比较器
,它会在比较之前取消引用指针。然后
将该比较器的一个实例传递给''sort'',带有3个参数。
RTFM关于带有3个参数的''sort''。
顺便说一句,您正在阅读哪本关于标准库的书?是否有关于自定义比较器的讨论
?
V
-
请删除资金' 'A'在通过电子邮件回复时
我没有回复最热门的回复,请不要问
The regular ''sort'' does not compare the objects behind the pointers.
It just compares the pointers. What you need is a custom comparator
which will dereference the pointers before comparing them. Then
pass an instance of that comparator to ''sort'' with 3 arguments.
RTFM about the ''sort'' with 3 arguments.
BTW, what book on Standard library are you reading? Does it talk
about custom comparators?
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
sa***@murdocks.on.ca 写道:
我正在尝试为
课程制作模拟目录结构应用程序。
我使用Vector存储指向我的目录的指针对象(作为当前对象的
子目录)。
在我的标题中它看起来像这样(私有部分)
vector<目录*目录;
在我的构造函数中的cpp文件中我有:
Directories.clear();
要向Vector添加目录,我有一个函数要求输入目录的名称
并检查它的有效性等,然后将其传递给
使用此函数创建和存储新的子目录:
bool Directory :: Create(string DN)//传入目录名
I am trying to make a simulated directory structure application for a
course.
I am using a Vector to store pointers to my Directory objects (as
subdirectories of the current object).
In my header it looks like this (private section)
vector<Directory*Directories;
In my cpp file in the constructor I have this:
Directories.clear();
To add directories to the Vector I have a function that asks for name
of the directory and checks it for validity etc. then passes it off to
create and store the new subdirectory using this function:
bool Directory::Create(string DN) //pass in directory name
不要通过副本传递std :: string。使用const引用。
否则你将不必要地调用std :: string copy ctor。
bool Directory :: Create(const std: :string& DN)
虽然这确实应该在Directory构造函数中。
Do not pass a std::string by copy. Use a const reference.
Otherwise you''ll be invoking the std::string copy ctor needlessly.
bool Directory::Create( const std::string& DN )
Although that really should be in a Directory constructor.
{
目录* ND =新目录(this,DN);
Directories.push_back(ND);
NumSubDirs ++;
sort(Directories.begin(),Directories.end());
返回true;
}
如您所见,我正在调用sort函数。正如您可能从
中猜到的那样,我发布排序的事实不起作用。我99%肯定它是
因为我需要提出正确的方法来重载<
运算符和运算符。
当调用sort时,值会改变位置,它们根据要求不按目录名称排序
。
I具有以下重载运算符<和操作员功能。 (我
不包括标题)哪些不能解决问题。
bool Directory :: operator<(const Directory& D)
{
返回D.Name<姓名;
}
bool目录::运营商<(const目录* D)
{
返回D->名称< this->姓名;
}
/ *
运营商>
* /
bool目录::运算符>(const目录& D)
{
返回名称D.Name;
}
bool目录::运算符>(const目录* D)
{
返回D->名称名称;
}
我确定这是问题,我只是不知道如何告诉它
正确的事情要做...
谢谢。
{
Directory *ND = new Directory(this, DN);
Directories.push_back(ND);
NumSubDirs++;
sort(Directories.begin(), Directories.end());
return true;
}
As you can see I am calling the sort function. As you may guess from
the fact that I am posting the sort does not work. I am 99% sure it''s
because I need to come up with the correct method of overloading the <
operator and the operator.
When sort is called the values change position, they just aren''t sorted
by directory name as requested.
I have the following overloaded operator< and operatorfunctions. (I
am not including the headers) which do NOT do the trick.
bool Directory::operator<(const Directory & D)
{
return D.Name < Name;
}
bool Directory::operator<(const Directory *D)
{
return D->Name < this->Name;
}
/*
operator>
*/
bool Directory::operator>(const Directory & D)
{
return Name D.Name;
}
bool Directory::operator>(const Directory * D)
{
return D->Name Name;
}
I am sure that''s the problem, I just don''t know how to tell it the
correct thing to do...
Thanks.
//使用仿函数建立比较测试 - 根据需要调整:
#include< algorithm>
struct DirectorySort
{
bool operator()(const目录* const& r_left,const目录*
const& r_right)
{
返回r_left-> getName()< r_right-> getName();
}
};
int main()
{
std :: vector<目录* dirs;
dirs.push_back(新目录(" dir_8"));
dirs.push_back(新目录(" dir_1"));
dirs.push_back(新目录(" dir_5"));
std :: sort(dirs.begin(),dirs.end(),DirectorySort ());
// deallocate dirs ...
}
// Use a functor to establish comparison test - adjust as needed:
#include <algorithm>
struct DirectorySort
{
bool operator()(const Directory* const& r_left, const Directory*
const& r_right)
{
return r_left->getName() < r_right->getName();
}
};
int main()
{
std::vector< Directory* dirs;
dirs.push_back( new Directory("dir_8") );
dirs.push_back( new Directory("dir_1") );
dirs.push_back( new Directory("dir_5") );
std::sort( dirs.begin(), dirs.end(), DirectorySort() );
// deallocate dirs ...
}
Salt_Peter写道:
Salt_Peter wrote:
sa *** @ murdocks。 on.ca 写道:
不要通过副本传递std :: string。使用const引用。
否则你将不必要地调用std :: string copy ctor。
sa***@murdocks.on.ca wrote:
Do not pass a std::string by copy. Use a const reference.
Otherwise you''ll be invoking the std::string copy ctor needlessly.
谢谢。我应该回过头来看看我的代码,我相信还有其他
的地方我也做了。
Thanks. I should go back through my code, I am sure there are other
places I did it too.
>
bool Directory :: Create(const std :: string& DN)
虽然这确实应该在Directory构造函数中。
>
bool Directory::Create( const std::string& DN )
Although that really should be in a Directory constructor.
>
//使用仿函数建立比较测试 - 根据需要调整:
#include< algorithm>
struct DirectorySort
{
bool operator()(const目录) * const& r_left,const目录*
const& r_right)
{
返回r_left-> getName()< r_right-> getName();
}
};
>
// Use a functor to establish comparison test - adjust as needed:
#include <algorithm>
struct DirectorySort
{
bool operator()(const Directory* const& r_left, const Directory*
const& r_right)
{
return r_left->getName() < r_right->getName();
}
};
我将此代码添加到私有部分的标题中:
struct DirectorySort
{
bool operator()(const目录* const& r_left,const
目录* const& r_right)
{
return r_left-> getName()< r_right-> getName();
}
};
我得到以下编译时错误:
在成员函数`bool Directory :: DirectorySort :: operator()(const
Directory * const&,const Directory * const&)'':
将`const Directory''作为`std :: string
的'this'参数'
Directory :: GetName()''丢弃限定符
将`const Directory''作为'this''参数传递给`std :: string
Directory :: GetName()''丢弃限定符
>
我之前在网上发现了与此类似的代码,但却无法确定如何使其工作。
任何想法?
I added this code to my header in the Private section:
struct DirectorySort
{
bool operator()(const Directory* const& r_left, const
Directory* const& r_right)
{
return r_left->getName() < r_right->getName();
}
};
I get the following compile time errors:
In member function `bool Directory::DirectorySort::operator()(const
Directory* const&, const Directory* const&)'':
passing `const Directory'' as `this'' argument of `std::string
Directory::GetName()'' discards qualifiers
passing `const Directory'' as `this'' argument of `std::string
Directory::GetName()'' discards qualifiers
I found code similar to this on the net earlier but could never figure
out how to make it work.
Any Ideas?
这篇关于基于对象属性将指针向量排序到对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!