STL查找算法 [英] STL find algorithm

查看:42
本文介绍了STL查找算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我对STL查找算法有疑问:

我的代码:


int main(无效)

{

vector< ClassA * myVector;


ClassA * ptrElement1 = ...;


myVector.push_front(ptrElement1);

...


ClassA * ptrElement2 = *(myVector.begin());


vector< ClassA *> :: iterator it =

find(myVector.begin(),myVector.end(),ptrElement2);


... < br $>
}


''find''算法中用于查找''ptrElement2'的内容是什么?

自' 'ptrElement2''只是一个指针(所以它是一个存储

地址的变量),我假设列表中的每个元素(也是指针)都是比较b / b
使用''ptrElement2''在他们所代表的

地址上使用运算符==,即检查是否

(地址存储在任何元素中)列表)==

(地址存储在''ptrElement2'')


我的假设是正确的吗?


但是,这可能很少需要。通常人们想要比较

对象(而不是他们的地址)。要实现这一点,必须在''find''算法中取消引用ptrElement2并另外确保

ClassA提供了正确的运算符==。对吗?


问候,

克里斯

解决方案

Christian Chrismann写道:


我的假设是否正确?





通常人们想要比较

对象(和不是他们的地址)。要实现这一点,必须在''find''算法中取消引用ptrElement2并另外确保

ClassA提供了正确的运算符==。对?



是的,这样做的一种方法是使用find_if并提供一个函数作为
第三个参数定义为:


bool comp(ClassA const * lhs,ClassA const * rhs){return * lhs == * rhs; }


另一个是让你的ClassA(便宜)可复制:


class ClassA

{

public:

// ctor,函数,但没有数据成员,都存在于impl中


operator ==(ClassA const& ;其他);

私人:

boost :: shared_ptr< Impl impl;

};


这样,直接使用ClassAs向量便宜且可行

因此您可以直接搜索它。你作为类型使用的类和

经常值得建立这样的方式。


然后有一些更奇怪的选择,所有这些都需要提升:


- boost :: lambda


find_if(myVector.begin(),myVector.end(),* _1 == someClassA)< br $> b $ b - boost :: indirect_iterator

boost :: indirect_iterator< ClassA const *>

indirect_first(myVector.begin()),indirect_last(myVector.end());

find(indirect_first,indirect_last,someClassA);


- boost :: pointer_container


boost :: ptr_vector< ClassA myPVector;

find(myPVector.begin(),myPVector.end(),someClassA);

我会先使用前两个解决方案中的一个。 br />

Jens




" Christian Chrismann" < pl ***** @ yahoo.deskrev i meddelandet

news:44 ********************** @ newsspool1。 arcor-online.net ...





我对STL查找算法有疑问:

我的代码:


int main(无效)

{

vector< ClassA * myVector;



将指针存储在向量中是非常不寻常的。它导致所有

种类的有趣问题。标准容器的设计是基于价值的


>

ClassA * ptrElement1 = .. 。;


myVector.push_front(ptrElement1);

...


ClassA * ptrElement2 = *( myVector.begin());


vector< ClassA *> :: iterator it =

find(myVector.begin(),myVector.end(),ptrElement2);


... < br $>
}


''find''算法中用于查找''ptrElement2'的内容是什么?

自' 'ptrElement2''只是一个指针(所以它是一个存储

地址的变量),我假设列表中的每个元素(也是指针)都是比较b / b
使用''ptrElement2''在他们所代表的

地址上使用运算符==,即检查是否

(地址存储在任何元素中)列表)==

(地址存储在''ptrElement2'')


我的假设是否正确?



对。您将指针存储在向量中,因此find()会查找

特定指针。


>

但是,这可能很少需要。通常人们想要比较

对象(而不是他们的地址)。要做到这一点,必须在''find''算法中取消引用ptrElement2并另外

确保

,ClassA提供了一个合适的运算符==。对?



是的。还有另一种变体std :: find,find_if,而不是

值取一个函数或像object这样的函数。这样你可以得到任何你喜欢的比较




当然,如果将ClassA对象存储在向量中,这是更简单的方法,
而不使用指针。

Bo Persson


Bo Persson写道:


>

" Christian Chrismann" < pl ***** @ yahoo.deskrev i meddelandet

news:44 ********************** @ newsspool1。 arcor-online.net ...


>

我对STL查找算法有疑问:
int main(void)
{
vector< ClassA * myVector;



将指针存储在向量中是非常不寻常的。它导致所有

种类的有趣问题。标准容器设计为
,以价值为基础。



为什么会不寻常?存储指向由

所拥有的对象的指针在向量中的某些其他实体是相当常见的并且没有什么

错误。


Hi,

I''ve a question on the STL find algorithm:
My code:

int main( void )
{
vector< ClassA* myVector;

ClassA *ptrElement1 = ...;

myVector.push_front( ptrElement1 );
...

ClassA *ptrElement2 = *(myVector.begin());

vector< ClassA* >::iterator it =
find( myVector.begin(), myVector.end(), ptrElement2 );

...
}

What is used here in the ''find'' algorithm for finding ''ptrElement2''?
Since ''ptrElement2'' is just a pointer (so it''s a variable storing an
address), I assume that each element (also pointers) in the list is
compared with ''ptrElement2'' by using the operator== on the
addresses they represent, i.e. it is checked if

(address stored in any element in the list) ==
(address stored in ''ptrElement2'')

Is my assumption correct?

However, this is probably rarely wanted. Usually one want to compare
the objects (and not their addresses). To achieve this, one must
dereference ptrElement2 in the ''find'' algorithm and additionally make sure
that ClassA provides a proper operator==. Right?

Regards,
Chris

解决方案

Christian Chrismann wrote:

Is my assumption correct?

Yes

Usually one want to compare
the objects (and not their addresses). To achieve this, one must
dereference ptrElement2 in the ''find'' algorithm and additionally make sure
that ClassA provides a proper operator==. Right?

Yes, one way of doing this is to use find_if and provide a function as
the third argument defined as:

bool comp(ClassA const* lhs, ClassA const* rhs) { return *lhs == *rhs; }

Another one is to make your ClassA (cheaply) copyable:

class ClassA
{
public:
// ctor, functions, but no data members, which all live in impl

operator ==(ClassA const& other);
private:
boost::shared_ptr< Impl impl;
};

That way, it''s cheap and feasible to use a vector of ClassAs directly
and thus you can search for it directly. Classes you use as types and
deal with very often are worth building that way.

Then there are some more bizarre alternatives, all of them require boost:

- boost::lambda

find_if(myVector.begin(), myVector.end(), *_1 == someClassA)

- boost::indirect_iterator

boost::indirect_iterator< ClassA const* >
indirect_first(myVector.begin()), indirect_last(myVector.end());
find(indirect_first, indirect_last, someClassA);

- boost::pointer_container

boost::ptr_vector< ClassA myPVector;
find(myPVector.begin(), myPVector.end(), someClassA);
I would use one of the first two solutions at first though.

Jens



"Christian Chrismann" <pl*****@yahoo.deskrev i meddelandet
news:44**********************@newsspool1.arcor-online.net...

Hi,

I''ve a question on the STL find algorithm:
My code:

int main( void )
{
vector< ClassA* myVector;

It is very unusual to store pointers in a vector. It leads to all
kinds of "interesting" problems. The standard containers are designed
to be value based.

>
ClassA *ptrElement1 = ...;

myVector.push_front( ptrElement1 );
...

ClassA *ptrElement2 = *(myVector.begin());

vector< ClassA* >::iterator it =
find( myVector.begin(), myVector.end(), ptrElement2 );

...
}

What is used here in the ''find'' algorithm for finding ''ptrElement2''?
Since ''ptrElement2'' is just a pointer (so it''s a variable storing an
address), I assume that each element (also pointers) in the list is
compared with ''ptrElement2'' by using the operator== on the
addresses they represent, i.e. it is checked if

(address stored in any element in the list) ==
(address stored in ''ptrElement2'')

Is my assumption correct?

Right. You store pointers in the vector, so the find() looks for a
specific pointer.

>
However, this is probably rarely wanted. Usually one want to compare
the objects (and not their addresses). To achieve this, one must
dereference ptrElement2 in the ''find'' algorithm and additionally
make sure
that ClassA provides a proper operator==. Right?

Yes. There is another variant of std::find, find_if, that instead of a
value take a function or a function like object. That way you can get
any kind of comparison you like.

The easier way if of course to store the ClassA objects in the vector,
and not use pointers.
Bo Persson


Bo Persson wrote:

>
"Christian Chrismann" <pl*****@yahoo.deskrev i meddelandet
news:44**********************@newsspool1.arcor-online.net...

>Hi,

I''ve a question on the STL find algorithm:
My code:

int main( void )
{
vector< ClassA* myVector;


It is very unusual to store pointers in a vector. It leads to all
kinds of "interesting" problems. The standard containers are designed
to be value based.

Why would it be unusual? Storing pointers to objects that are owned by
some other entity in a vector is fairly common and there is nothing
wrong with it.


这篇关于STL查找算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆