LinuxÒÁµéÔ°ÂÛ̳'s Archiver

DarkSpy ·¢±íÓÚ 2003-1-18 12:00

I S O C++ / S T L ÎÊÌâ Faq

´ËÌùÄÚÈݲ»ÊDZà³Ì¾«»ª°æÄÚµÄÄÚÈÝ£¬ÊÇÎһشðÒ»¸öC++ÓʼþÁбíÄÚµÄÎÊÌ⣬ϣÍû¶ÔÆäËûÀàËÆÎÊÌâÓÐÆô·¢¡£
ÓÐÆäËû¸üн«²»¶¨ÆÚ±à¼­¡£

ÎÊÌâÒ»£º
Q:
[code]
#pragma warning (disable: 4786)
#include < map>
#include < algorithm>

typedef std::map< int,void*>  stuff_map;

struct _need_delete {
        bool operator() (const stuff_map::value_type &v) const {
                return true;
        }
};

void test()
{
        stuff_map *sm=new stuff_map;
        std::remove_if(sm-> begin(),sm-> end(),_need_delete());
}

±àÒë³ö´í˵ error C2582: 'std::pair< int const ,void *> ' : 'operator =' function is unavailable
:(

Ϊʲô pair µÄ operator= ÕÒ²»µ½°¡. int ºÍ void* ¶¼ÊÇÔ­ÉúÀàÐͰ¡
[/code]

A:
[code]
¿´ map ÄڵĴúÂë
typedef pair<const _Key, _Tp> value_type;
ÔÙ¿´ remove_if µÄ´úÂë:
return __first == __last ? __first : remove_copy_if(++__i, __last, __first, __pred);
È»ºóÔÙ¿´ remove_copy_if µÄ´úÂë:
if (!__pred(*__first)) {
   *__result = *__first;
   ++__result;
}
Õâ¸öʱºò * __result  = const int , ÎÞ·¨¸³Öµ.
[/code]


ÎÊÌâ¶þ£º
Q:
[code]
namespace vfx_intimate{
    struct VSelectThen
    {       template<typename Then, typename Else>
            struct Result
            {       typedef Then RET;
            };
    }; // end SelectThen
    struct VSelectElse
    {       template<typename Then, typename Else>
            struct Result
            {       typedef Else RET;
            };
    }; // end SelectElse

    template<bool Condition>
    struct VSelector
    {       typedef VSelectThen RET;
    }; // end Selector

    template<>
    struct VSelector<false>
    {       typedef VSelectElse RET;
    }; // end Selector<false>

        const int DEFAULT = -32767;

        const int NilValue = -32768;

        struct NilCase
        {   enum {tag = NilValue};
                typedef NilCase RET;
        }; // NilCase
}

template<bool Condition, typename Then, typename Else>
struct VIF
{
        typedef typename vfx_intimate::VSelector<Condition>::RET select;
        typedef typename select::Result<Then,Else>::RET RET;
}; // IF

template <int Tag,typename Statement,typename Next = vfx_intimate::NilCase>
struct VCASE
{   enum {tag = Tag};
    typedef Statement statement;
    typedef Next next;
}; // CASE

template <int Tag,typename aCase>
struct VSWITCH
{
        typedef typename aCase::next nextCase;
        enum {
                tag = aCase::tag,                        // VC++ 5.0 doesn't operate directly on aCase::value in IF<>
                nextTag = nextCase::tag,        // Thus we need a little cheat
                found = (tag == Tag || tag == vfx_intimate::DEFAULT)
        };
        typedef typename VIF<(nextTag == vfx_intimate::NilValue),
                                vfx_intimate::NilCase,
                                VSWITCH<Tag,nextCase> >
                        ::RET nextSwitch;
        typedef typename VIF<(found != 0),
                                aCase::statement,
                                nextSwitch::RET>     //±àÒëͨ²»¹ý£¡£¡£¡£¡
                        ::RET RET;
}; // SWITCH
[/code]

A: C++±àÒëÆ÷±àÒëÏÂÁдúÂ룺
[code]
typedef typename VIF<(found != 0), aCase::statement, nextSwitch::RET>::RET RET;
[/code]
µÄʱºò£¬ÈÏΪ aCase::statement ºÍ nextSwitch::RET Ϊһ¸ö static »òÕß namespace »òÕß class Äڵ͍Òå¡£±ê׼д·¨£¬ÔÚ aCase::statement ºÍ nextSwitch::RET ֮ǰÔö¼Ó typename¡£

Ò³: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.