2008-11-03 14 views
15

मैं सिर्फ सी में वापस हो रही है ++ सी # की एक बहुत कुछ कर के कुछ साल बाद, और हाल ही में उद्देश्य सीइटरेटर एडाप्टर सिर्फ मानचित्र में मानों को पुन: स्थापित करने के लिए?

एक बात मैं पहले किया है std :: नक्शे के लिए अपने खुद के इटरेटर एडाप्टर रोल करने के लिए है जो कुंजी-मूल्य जोड़ी के बजाए केवल मूल्य भाग को खराब कर देगा। यह करने के लिए काफी आम और प्राकृतिक बात है। सी # इस सुविधा को अपनी शब्दकोश श्रेणी की कुंजी और मूल्य गुणों के साथ प्रदान करता है। उद्देश्य-सी के एनएस डिक्शनरी, इसी तरह, सभी केके और सभी वैल्यू हैं।

चूंकि मैं "दूर" रहा हूं, बूस्ट ने रेंज और फॉरएच पुस्तकालयों का अधिग्रहण किया है, जिसे मैं अब बड़े पैमाने पर उपयोग कर रहा हूं। मुझे आश्चर्य हुआ कि दोनों के बीच ऐसा करने की कुछ सुविधा थी, लेकिन मुझे कुछ भी नहीं मिला है।

मैं बूस्ट के इटरेटर एडाप्टर का उपयोग करके कुछ खटखटाए जाने की सोच रहा हूं, लेकिन इससे पहले कि मैं उस मार्ग से नीचे जाऊं, मैंने सोचा कि मैं यहां पूछूंगा कि अगर कोई बूस्ट में ऐसी सुविधा के बारे में जानता है, या कहीं और तैयार है?

+0

आप इस सवाल का जवाब जांच करना चाह सकते: http://stackoverflow.com/questions/2311752/boost-bind-to-access-stdmap- तत्व-में-stdfor-प्रत्येक/2312015 # 2312015 – Manuel

उत्तर

14

मुझे नहीं लगता कि बॉक्स से कुछ भी बाहर है। आप boost :: make_transform का उपयोग कर सकते हैं।

template<typename T1, typename T2> T2& take_second(const std::pair<T1, T2> &a_pair) 
{ 
    return a_pair.second; 
} 

void run_map_value() 
{ 
    map<int,string> a_map; 
    a_map[0] = "zero"; 
    a_map[1] = "one"; 
    a_map[2] = "two"; 
    copy(boost::make_transform_iterator(a_map.begin(), take_second<int, string>), 
    boost::make_transform_iterator(a_map.end(), take_second<int, string>), 
    ostream_iterator<string>(cout, "\n") 
    ); 
} 
+2

धन्यवाद डेविड। यह पहले जैसा मैंने किया है उससे काफी समान है। यह अभी भी मेरी पसंद के लिए बहुत सारे बॉयलरप्लेट की आवश्यकता है। यद्यपि आप एक पूरी तरह से वैध जवाब साबित करने के लिए वोट दे रहे हैं। अभी भी और अधिक उम्मीद कर रहे हैं ... – philsquared

+0

ध्यान दें कि फ़ंक्शन टेम्पलेट में 'boost :: make_transform_iterator (मानचित्र :: const_iterator, take_second < Key, Value>) को लपेटना छोटा है। यह उबलता – MSalters

7

सतत डेविड का जवाब है, वहाँ एक और संभावना को बढ़ावा देने :: transform_iterator से एक व्युत्पन्न वर्ग बनाने के द्वारा boile डाल करने के लिए है। मैं अपनी परियोजनाओं में इस समाधान का उपयोग कर रहा:

namespace detail 
{ 

template<bool IsConst, bool IsVolatile, typename T> 
struct add_cv_if_c 
{ 
    typedef T type; 
}; 
template<typename T> 
struct add_cv_if_c<true, false, T> 
{ 
    typedef const T type; 
}; 
template<typename T> 
struct add_cv_if_c<false, true, T> 
{ 
    typedef volatile T type; 
}; 
template<typename T> 
struct add_cv_if_c<true, true, T> 
{ 
    typedef const volatile T type; 
}; 

template<typename TestConst, typename TestVolatile, typename T> 
struct add_cv_if: public add_cv_if_c<TestConst::value, TestVolatile::value, T> 
{}; 

} // namespace detail 


/** An unary function that accesses the member of class T specified in the MemberPtr template parameter. 

    The cv-qualification of T is preserved for MemberType 
*/ 
template<typename T, typename MemberType, MemberType T::*MemberPtr> 
struct access_member_f 
{ 
    // preserve cv-qualification of T for T::second_type 
    typedef typename detail::add_cv_if< 
     std::tr1::is_const<T>, 
     std::tr1::is_volatile<T>, 
     MemberType 
    >::type& result_type; 

    result_type operator()(T& t) const 
    { 
     return t.*MemberPtr; 
    } 
}; 

/** @short An iterator adaptor accessing the member called 'second' of the class the 
    iterator is pointing to. 
*/ 
template<typename Iterator> 
class accessing_second_iterator: public 
    boost::transform_iterator< 
     access_member_f< 
      // note: we use the Iterator's reference because this type 
      // is the cv-qualified iterated type (as opposed to value_type). 
      // We want to preserve the cv-qualification because the iterator 
      // might be a const_iterator e.g. iterating a const 
      // std::pair<> but std::pair<>::second_type isn't automatically 
      // const just because the pair is const - access_member_f is 
      // preserving the cv-qualification, otherwise compiler errors will 
      // be the result 
      typename std::tr1::remove_reference< 
       typename std::iterator_traits<Iterator>::reference 
      >::type, 
      typename std::iterator_traits<Iterator>::value_type::second_type, 
      &std::iterator_traits<Iterator>::value_type::second 
     >, 
     Iterator 
    > 
{ 
    typedef boost::transform_iterator< 
     access_member_f< 
      typename std::tr1::remove_reference< 
       typename std::iterator_traits<Iterator>::reference 
      >::type, 
      typename std::iterator_traits<Iterator>::value_type::second_type, 
      &std::iterator_traits<Iterator>::value_type::second 
     >, 
     Iterator 
    > baseclass; 

public: 
    accessing_second_iterator(): 
     baseclass() 
    {} 

    // note: allow implicit conversion from Iterator 
    accessing_second_iterator(Iterator it): 
     baseclass(it) 
    {} 
}; 

यह भी क्लीनर कोड सुराग:

void run_map_value() 
{ 
    typedef map<int, string> a_map_t; 
    a_map_t a_map; 
    a_map[0] = "zero"; 
    a_map[1] = "one"; 
    a_map[2] = "two"; 

    typedef accessing_second_iterator<a_map_t::const_iterator> ia_t; 
    // note: specify the iterator adaptor type explicitly as template type, enabling 
    // implicit conversion from begin()/end() 
    copy<ia_t>(a_map.begin(), a_map.end(), 
    ostream_iterator<string>(cout, "\n") 
); 
} 
24

पिछले जवाब की जगह, बाकी मामले किसी में इस पाता है जैसे मैंने किया था। 1.43 को बढ़ावा देने के रूप में, कुछ सामान्य रूप से उपयोग किए जाने वाले रेंज एडाप्टर प्रदान किए जाते हैं। इस मामले में, आप boost :: एडाप्टर :: map_values ​​चाहते हैं। प्रासंगिक उदाहरण: http://www.boost.org/doc/libs/1_46_0/libs/range/doc/html/range/reference/adaptors/reference/map_values.html#range.reference.adaptors.reference.map_values.map_values_example

+0

कूल रखता है, उस मैट के लिए धन्यवाद - मैंने इसे बढ़ावा में नहीं देखा था (नहीं कि मैंने 2008 से चेक किया था!) ​​ – philsquared

+3

+1 उपयोगी, fyi आप इसे '(ऑटो वैल्यू: mapVar | boost: : एडाप्टर :: map_values) '(सी ++ 11) जो कमाल है – radman

7

बिल्कुल इस उद्देश्य के लिए एक बूस्ट रेंज एडेप्टर है। देखें http://www.boost.org/doc/libs/1_53_0/libs/range/doc/html/range/reference/adaptors/reference/map_values.html

(इस उदाहरण वहाँ से cribbed)

int main(int argc, const char* argv[]) 
{ 
    using namespace boost::assign; 
    using namespace boost::adaptors; 

    std::map<int,int> input; 
    for (int i = 0; i < 10; ++i) 
    input.insert(std::make_pair(i, i * 10)); 

    boost::copy(
     input | map_values, 
     std::ostream_iterator<int>(std::cout, ",")); 

    return 0; 
}