排序/矢量 [英] sort / vector
问题描述
嗨伙计们,
我在VC6下编译时遇到问题!
也许有人有一些时间来看看这个:
模板< class T> class Compare
{
public:
Compare(){};
virtual~Compare(){} ;
bool operator()(const T& T1,const T& T2)const
{return T1.GetNumber()< T2.GetNumber();}
};
class Element
{
public:
元素(Model * model,unsigned int number);
virtual~Element();
unsigned int GetNumber() const {return _number;}
}
std :: vector< Element *> * elements = new std :: vector< Element *>( 0);
elements-> push_back(new Element()); // n次
std :: vector< Element *> :: const_iterator iterFirst = elements-> begin();
std :: vector< Element * > :: const_iterator iterLast = elements-> end();
std :: sort(iterFirst,iterLast,Compare< Element>());
我的编译器告诉我(对不起我的编译器说德语,但它是
可读):
错误C2664:''()'':Konvertierung des参数1 von''类
fesolv ::'*''在'const class fesolv :: Element&''nicht moeglich
Ursache:Konvertierung von' ''fesolv :: Element *''在''const
class fesolv :: Element''nicht moeglich
我不知道在哪里开始。
排序也适用于矢量< T *> *或仅适用于矢量< T *> ????
谢谢,
eiji
< blockquote>
eiji写道:
嗨伙计们,
我在VC6下编译这个有问题!
也许有人有一些时间来看看这个:
模板< class T> class比较
{boo operator()(const T& T1,const T& T2)const
std :: vector< Element *> * elements = new std :: vector< Element * GT;(0);
std :: sort(iterFirst,iterLast,Compare< Element>());
请注意,你的比较函数需要引用,但你的vector
包含指针。 T表示T。为了你的比较想要元素*
然后当然该运算符的主体需要
更改....因此一个单独的ComparePtr函子可能是有序的。
在向量中保持指针*通常是一个不好的举动。
当然总是有任何规则的例外,但是当你要成为
时,在向量中保持指针试着找到一种不同的方式。
的一种方法是这样做而没有与它一起使用的所有皮塔饼是创建一个可以包含在向量中的智能
指针(有一些规则
关于矢量内容)。我甚至不确定指针符合这些要求。
2005年11月27日12:39:10 -0800,eiji <萨***** @ gmx.de>写道:
嗨大家好,
我在VC6下编译时有问题!
也许有人有一些时间来看看这个:
template< class T> class Compare
{
公开:
比较(){};
虚拟〜比较(){};
如果你不从比较< T>继承任何
其他类型,析构函数不必是虚拟的。
bool运算符()(const T& T1,const T& T2)const
{return T1.GetNumber()< T2.GetNumber();}
};
这对指针不起作用;要么你需要把它改成
容纳T *,否则你需要第二种类型来处理它们,
例如:
bool operator()(const T * T1,const T * T2)const
{return T1-> GetNumber()< T2-> GetNumber();}
class元素
公开:
元素(Model * model,unsigned int number);
virtual ~Element();
unsigned int GetNumber()const {return _number;}
}
一些问题/评论:
(1)_number在哪里声明?
(2)你不应该使用以下划线开头的名字来代替
任何东西(见17.4节) .3.1.2的C ++原因)。
(3)如果你永远不想构造一个没有参数的元素,你需要声明一个默认构造函数,b $ b但要私有化,不要实施它。这将阻止编译器为您提供一个您不想要的默认
构造函数。
std :: vector< Element *> * elements = new std: :矢量<元素* GT;(0);
默认构造函数可以正常工作,无需传递参数
零:
std: :vector< Element *> * elements = new std :: vector< Element *> ;;
elements-> push_back(new Element()); // n次
嗯......如果你想隐藏默认的
构造函数,我觉得我们有一个问题...但如果你提供初始化器/参数,没关系。
std :: vector< Element *> :: const_iterator iterFirst = elements-> begin();
std :: vector<元素*> :: const_iterator iterLast = elements-> end();
std :: sort(iterFirst,iterLast,Compare< Element>());
如果您(a)传递一个对象并且(b)更改比较<> :: operator()的
实现和签名,这应该有效接受T *
而不是T&:
比较<元素> cmp_obj;
std :: sort(iterFirst,iterLast,cmp_obj);
它也可以与Compare< Element>()一起使用,但是你正在创建a
临时。我不知道为什么,但是当
将临时对象传递给std :: sort()时,我似乎总是遇到麻烦......
我的编译器告诉我(对不起,我的编译器说德语,但它可读):
错误C2664:''()'':Konvertierung des参数1 von''类
fesolv :: Element *''在''const class fesolv :: Element&''nicht moeglich
Ursache:Konvertierung von''class fesolv :: Element *''in''const
class fesolv :: Element''nicht moeglich
我不知道从哪里开始。
排序也适用于矢量< T *> *或仅适用于矢量< T *> ????
谢谢,
eiji
FYI - übrigensgibtes auch eine deutscheNGfürC++ - Fragen,
de.comp.lang.iso-c ++ ... aber hier bist Du mit DeinenFragennatürlich
genauso willkommen,wiedrüben! :)
-
Bob Hairgrove
没有********** @ Home.com
>有关于矢量内容的某些规则
很高兴知道吗?知道在哪里可以轻松找到它们吗?
Hi folks,
I have a problem compiling this under VC6!
Maybe someone has some minutes to look at this:
template<class T> class Compare
{
public:
Compare(){};
virtual ~Compare(){};
bool operator()(const T& T1, const T& T2) const
{return T1.GetNumber() < T2.GetNumber();}
};
class Element
{
public:
Element(Model* model, unsigned int number);
virtual ~Element();
unsigned int GetNumber() const { return _number;}
}
std::vector<Element*>* elements = new std::vector<Element*>(0);
elements->push_back(new Element()); // n-times
std::vector<Element*>::const_iterator iterFirst = elements->begin() ;
std::vector<Element*>::const_iterator iterLast = elements->end() ;
std::sort(iterFirst, iterLast, Compare<Element>());
My Compiler tells me(sorry my compiler speaks german, but it is
readable):
error C2664: ''()'' : Konvertierung des Parameters 1 von ''class
fesolv::Element *'' in ''const class fesolv::Element &'' nicht moeglich
Ursache: Konvertierung von ''class fesolv::Element *'' in ''const
class fesolv::Element'' nicht moeglich
I don''t know where to start.
Do sort also works for vector<T*>* or only for vector<T*> ????
Thanks,
eiji
eiji wrote:Hi folks,
I have a problem compiling this under VC6!
Maybe someone has some minutes to look at this:
template<class T> class Compare
{
bool operator()(const T& T1, const T& T2) const std::vector<Element*>* elements = new std::vector<Element*>(0); std::sort(iterFirst, iterLast, Compare<Element>());
Notice that your compare function expects references but your vector
contains pointers. The "T" for your compare would want to be Element*
and then of course the body of that operator would need
changing....therefore a seperate ComparePtr functor may be in order.
Keeping pointers in a vector is *generally* a bad move. There are of
course always exceptions to any rule but when you are going to be
keeping pointers in a vector try to find a different way. One way to
do so without all the pita that goes with it is to create a smart
pointer that can be contained in a vector (there are certain rules
about vector contents). I''m not even sure pointers meet those reqs.
On 27 Nov 2005 12:39:10 -0800, "eiji" <Sa*****@gmx.de> wrote:
Hi folks,
I have a problem compiling this under VC6!
Maybe someone has some minutes to look at this:
template<class T> class Compare
{
public:
Compare(){};
virtual ~Compare(){};
The destructor doesn''t have to be virtual if you do not inherit any
other types from Compare<T>.
bool operator()(const T& T1, const T& T2) const
{return T1.GetNumber() < T2.GetNumber();}
};
This won''t work for pointers; either you''ll need to change this to
accommodate T*, or you will need a second type to deal with them,
e.g.:
bool operator()(const T* T1, const T* T2) const
{return T1->GetNumber() < T2->GetNumber();}
class Element
{
public:
Element(Model* model, unsigned int number);
virtual ~Element();
unsigned int GetNumber() const { return _number;}
}
Some questions/remarks:
(1) Where is _number declared?
(2) You should never use names beginning with an underscore for
anything (see section 17.4.3.1.2 of the C++ for the reason).
(3) If you never want to construct an Element with no arguments, you
should declare a default constructor, but make it private and do not
implement it. This will prevent the compiler from giving you a default
constructor that you don''t want.
std::vector<Element*>* elements = new std::vector<Element*>(0);
The default constructor will work fine, no need to pass an argument of
zero:
std::vector<Element*>* elements = new std::vector<Element*>;
elements->push_back(new Element()); // n-times
Hmmm ... I see we have a problem here if you want to hide the default
constructor ... but if you provide initializers/arguments, it''s OK.
std::vector<Element*>::const_iterator iterFirst = elements->begin() ;
std::vector<Element*>::const_iterator iterLast = elements->end() ;
std::sort(iterFirst, iterLast, Compare<Element>());
This should work if you (a) pass an object and (b) change the
implementation and signature of Compare<>::operator() to accept T*
instead of T&:
Compare<Element> cmp_obj;
std::sort(iterFirst, iterLast, cmp_obj);
It might also work with Compare<Element>(), but you are creating a
temporary. I don''t know why, but I always seem to have trouble when
passing a temporary object to std::sort()...
My Compiler tells me(sorry my compiler speaks german, but it is
readable):
error C2664: ''()'' : Konvertierung des Parameters 1 von ''class
fesolv::Element *'' in ''const class fesolv::Element &'' nicht moeglich
Ursache: Konvertierung von ''class fesolv::Element *'' in ''const
class fesolv::Element'' nicht moeglich
I don''t know where to start.
Do sort also works for vector<T*>* or only for vector<T*> ????
Thanks,
eiji
FYI -- übrigens gibt es auch eine deutsche NG für C++-Fragen,
de.comp.lang.iso-c++ ... aber hier bist Du mit Deinen Fragen natürlich
genauso willkommen, wie drüben! :)
--
Bob Hairgrove
No**********@Home.com
>there are certain rules about vector contents
Nice to know? Any idea where to find them easily?
这篇关于排序/矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!