質問

C#を何年か実行し、最近Objective Cを実行した後、C ++に戻りました。

前にやったことの1つは、キーと値のペアではなく、値の部分だけを参照するstd :: mapのイテレータアダプタをロールすることです。これは非常に一般的で自然なことです。 C#は、この機能にそのディクショナリクラスのキーと値のプロパティを提供します。同様に、Objective-CのNSDictionaryにはallKeysとallValuesがあります。

「離れて」から、BoostはRangeライブラリとForEachライブラリを取得しました。これらは現在広く使用しています。 2つの間に同じことをする機能があるかどうか疑問に思いましたが、何も見つかりませんでした。

Boostのイテレータアダプターを使用して何かをノックアップすることを考えていますが、そのルートに進む前に、Boostのそのような施設を知っているか、または他の準備ができている場所をここで尋ねると思いましたか?

役に立ちましたか?

解決

すぐに使えるものはないと思います。 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")
    );
}

他のヒント

他の誰かが私と同じようにこれを見つけた場合に備えて、前の答えを置き換えます。 boost 1.43の時点で、一般的に使用される範囲アダプターがいくつか提供されています。この場合、boost :: adaptors :: 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

まさにこの目的のためにブースト範囲アダプターがあります。 http://www.boostをご覧ください。 .org / doc / libs / 1_53_0 / libs / range / doc / html / range / reference / adaptors / reference / map_values.html

(この例はそこから作成されました)

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;
}

Davidの答えを続けると、boost :: transform_iteratorから派生クラスを作成することで、別の可能性があります。私は自分のプロジェクトでこのソリューションを使用しています:

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")
  );
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top