容器 对象指针 泛型算法
											容器中存放的是一些列对象的指针,要对这写对象进行查找,能否直接使用泛型算法find,排序能否直接使用泛型算法sort										
					
	以下是引用zhongjiezhe在2011-8-1 21:51:55的发言:
容器中存放的是一些列对象的指针,要对这写对象进行查找,能否直接使用泛型算法find,排序能否直接使用泛型算法sort
1. 使用find_if容器中存放的是一些列对象的指针,要对这写对象进行查找,能否直接使用泛型算法find,排序能否直接使用泛型算法sort
 程序代码:
程序代码:#include <algorithm>
#include <iostream>
int main()
{
    int* buf[] = { new int(0), new int(1), new int(2) };
    struct foo {
        foo( int v ) : val(v)
        {
        }
        bool operator()( int* pv ) const
        {
            return *pv==val;
        }
        int val;
    };
    size_t cnt = sizeof(buf)/sizeof(buf[0]);
    int** p = std::find_if( buf+0, buf+cnt, foo(1) );
    if( p != buf+cnt )
    {
        std::cout << **p << std::endl;
    }
    return 0;
}2. 可以直接用sort
#include <algorithm>
#include <iostream>
int main()
{
int* buf[] = { new int(2), new int(1), new int(0) };
struct foo {
bool operator()( int* a, int* b ) const
{
return *a<*b;
}
};
size_t cnt = sizeof(buf)/sizeof(buf[0]);
std::sort( buf+0, buf+cnt, foo() );
for( size_t i=0; i<cnt; ++i )
std::cout << *buf[i] << ' ';
std::cout << std::endl;
return 0;
}



 
											





 
	    

 
	






