1

私のコンテナは、std::vector のようにランダムにアクセスできます。コンテナーの「インデックス付きサブセット イテレーター」が必要です。この名前をつけました。アイデアは次のとおりです。

私のコンテナのサブセットは、一連のインデックス、たとえば [0, 4, 5, 7] (私のコンテナのサイズは 7 より大きい) によって与えられます。このサブセットに対してイテレータが必要です。

以下は疑似コードです。

std::vector<std::string> v = boost::assign::list_of("Aa")("Bb")("Cc")("Dd")("Ee");
std::vector<int> subsetIndex = boost::assign::list_of(0)(2)(3);
IndexedSubsetIterator subsetIterator = IndexedSubsetIterator(v.begin(), subsetIndex);  // or templated version
std::vector<std::string> subset;
boost::push_back(subset ubsetIterator);

STLまたはブーストでこれを行う簡単な方法があるかどうか疑問に思っていますか? サンプルコードをよろしくお願いします。

どうもありがとう。

4

1 に答える 1

2

これが、Boost のpermutation_iteratorの目的です。ソースへの反復子と、インデックス コンテナーへの反復子から 1 つを作成します。実行可能な例を次に示します。

#include <vector>
#include <iostream>
#include <algorithm>
#include <boost/iterator/permutation_iterator.hpp>
#include <boost/assign/list_of.hpp>

int main()
{
    std::vector<std::string> v =
        boost::assign::list_of("Aa")("Bb")("Cc")("Dd")("Ee");
    std::vector<int> subsetIndex =
        boost::assign::list_of(0)(2)(3);

    auto it_begin =
        boost::make_permutation_iterator(v.begin(), subsetIndex.begin());
    auto it_end =
        boost::make_permutation_iterator(v.end(), subsetIndex.end());

    std::vector<std::string> subset;
    std::copy(it_begin, it_end, std::back_inserter(subset));

    for (const auto& s : subset) std::cout << s << '\n';
}

出力:

Aa
Cc
Dd
于 2013-11-13T15:33:42.463 に答える