免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3853 | 回复: 4
打印 上一主题 下一主题

stl中find用法一问 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-27 10:09 |只看该作者 |倒序浏览
比如一个列表中是一个类, 我想实现在该列表中, 是否有某个对象中成员是否含某个值: 但以下代码在用find_if的时候编译不过
高手过来说说,该怎么弄, 看stl中的find_if实现, 就是:

template <class InputIterator, class Predicate>
InputIterator find_if(InputIterator first, InputIterator last,
                      Predicate pred) {
  while (first != last && !pred(*first)) ++first;
  return first;
}

#include <string>
#include <vector>
using namespace std;
class CTest
{
private:
&nbsp;&nbsp;&nbsp;&nbsp;int ii;
&nbsp;&nbsp;&nbsp;&nbsp;string str;
public:
&nbsp;&nbsp;&nbsp;&nbsp;CTest() {};
&nbsp;&nbsp;&nbsp;&nbsp;CTest(int i):ii(i){ };
&nbsp;&nbsp;&nbsp;&nbsp;CTest(string &s) : str(s)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;CTest(int i, string &s) : ii(i), str(s) {}
&nbsp;&nbsp;&nbsp;&nbsp;int get_ii(){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return ii;
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;string &get_str()
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return str;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
};

typedef int (*pf)() ;
template<class T, class itor>
class cmp
{
public:
&nbsp;&nbsp;&nbsp;&nbsp;cmp(T &v, pf f) : m_value(v), m_f(f) {}
&nbsp;&nbsp;&nbsp;&nbsp;bool operator ()(itor it)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (m_value == it->m_f());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
private:
&nbsp;&nbsp;&nbsp;&nbsp;pf m_f;
&nbsp;&nbsp;&nbsp;&nbsp;T m_value;
};


int main(int argc, char *argv[])
{
&nbsp;&nbsp;&nbsp;&nbsp;vector<CTest> vTest;
&nbsp;&nbsp;&nbsp;&nbsp;for (int i=0; i<10; ++i)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char sz[100];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sprintf(sz, "%d", i+100);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string s(sz);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CTest c(i, s);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vTest.push_back(c);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;vector<CTest>::iterator it, end=vTest.end();
&nbsp;&nbsp;&nbsp;&nbsp;for (it=vTest.begin(); it!=end; ++it)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("ii=%d\tstr=%s\n", it->get_ii(), it->get_str().c_str());
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;it = find_if(vTest.begin(), end, cmp(8, CTest::get_ii));
&nbsp;&nbsp;&nbsp;&nbsp;if (it==end)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("not found\n");
&nbsp;&nbsp;&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("found\n");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("ii=%d\tstr=%s\n", it->get_ii(), it->get_str().c_str());
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;return 0;
}


[ 本帖最后由 freebarque 于 2009-5-27 10:14 编辑 ]

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
2 [报告]
发表于 2009-05-27 15:20 |只看该作者
#include <string>

class CTest
{
public:
        CTest( int i=0 ) : i_(i)
        {
        }
        CTest( const char* s ) : i_(0), str_(s)
        {
        }
        CTest( int i, const char* s ) : i_(i), str_(s)
        {
        }
        int get_i() const
        {
                return i_;
        }
        const std::string& get_str() const
        {
                return str_;
        }
private:
        int i_;
        std::string str_;
};

struct cmp
{
        cmp( int (CTest::*pmf)() const, int val ) : pmf_(pmf), val_(val)
        {
        }
        bool operator()( const CTest& t ) const
        {
                return (t.*pmf_)() == val_;
        }
private:
        int (CTest::*pmf_)() const;
        int val_;
};

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main(int argc, char *argv[])
{
        vector<CTest> vTest;
        for( int i=0; i<10; ++i )
        {
                char sz[100];
                sprintf(sz, "%d", i+100);
                vTest.push_back( CTest(i,sz) );
        }

        for( vector<CTest>::iterator it=vTest.begin(); it!=vTest.end(); ++it )
        {
                printf( "i=%d\tstr=%s\n", it->get_i(), it->get_str().c_str() );
        }

        cout << "---------------" << endl;

        vector<CTest>::iterator it = find_if( vTest.begin(), vTest.end(), cmp(&CTest::get_i, );
        if( it == vTest.end() )
                printf( "%s", "not found\n" );
        else
                printf( "i=%d\tstr=%s\n", it->get_i(), it->get_str().c_str() );

        return 0;
}

[ 本帖最后由 bruceteen 于 2009-5-27 15:21 编辑 ]

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
3 [报告]
发表于 2009-05-27 15:29 |只看该作者

如果希望cmp是个模板类,且完全隐式推导的话:

#include <string>

class CTest
{
public:
        CTest( int i=0 ) : i_(i)
        {
        }
        CTest( const char* s ) : i_(0), str_(s)
        {
        }
        CTest( int i, const char* s ) : i_(i), str_(s)
        {
        }
        int get_i() const
        {
                return i_;
        }
        const std::string& get_str() const
        {
                return str_;
        }
private:
        int i_;
        std::string str_;
};

template<class T,typename V>
struct cmp
{
        cmp( V (T::*pmf)() const, V val ) : pmf_(pmf), val_(val)
        {
        }
        bool operator()( const T& t ) const
        {
                return (t.*pmf_)() == val_;
        }
private:
        V (CTest::*pmf_)() const;
        V val_;
};
template<class T,typename V>
cmp<T,V> makecmp( V (T::*pmf)() const, V val )
{
        return cmp<T,V>( pmf, val );
}

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main(int argc, char *argv[])
{
        vector<CTest> vTest;
        for( int i=0; i<10; ++i )
        {
                char sz[100];
                sprintf(sz, "%d", i+100);
                vTest.push_back( CTest(i,sz) );
        }

        for( vector<CTest>::iterator it=vTest.begin(); it!=vTest.end(); ++it )
        {
                printf( "i=%d\tstr=%s\n", it->get_i(), it->get_str().c_str() );
        }

        cout << "---------------" << endl;

        vector<CTest>::iterator it = find_if( vTest.begin(), vTest.end(), makecmp(&CTest::get_i, );
        if( it == vTest.end() )
                printf( "%s", "not found\n" );
        else
                printf( "i=%d\tstr=%s\n", it->get_i(), it->get_str().c_str() );

        return 0;
}

论坛徽章:
0
4 [报告]
发表于 2009-05-27 20:56 |只看该作者
楼上的高手, 非常感谢, 我搞定了一个成员函数:
        
  1. vector<CTest>::iterator it = find_if( vTest.begin(), vTest.end(), makecmp(&CTest::get_i, 8) );
复制代码

编译允许都OK

但没法搞定另一个另成员函数, 能不能麻烦你再帮忙看看:
  1.         string str("108");
  2.         vector<CTest>::iterator it = find_if( vTest.begin(), vTest.end(), makecmp(&CTest::get_str, str) );
复制代码

改成这样, 编译的时候报错:
cmp.cc:71: error: no matching function for call to `makecmp(const std::string&(CTest::*)() const, std::string&'


万分感谢。

论坛徽章:
0
5 [报告]
发表于 2009-05-27 22:01 |只看该作者
en, 搞定了

仔细比对了几个模版, 把前后返回,传入参数弄成一致, 跑对了。

再次感谢上面指点的高手。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP