使用 stl 排列 std::vector 元素的最短解决方案



>假设您有某种类型的Tstd::vector<T>std::vector<int>此向量的索引选择。现在我正在寻找一个函数permute(const std::vector<T>& vector, const std::vector<int>& indices),它返回相对于给定索引的排列向量。

通过编写如下所示的简短函数可以轻松解决问题:

template<typename T>
std::vector<T> permute(const std::vector<T>& matrix, const std::vector<int>& indices) {
    std::vector<T> ret;
    for (auto p : indices) {
        ret.push_back(matrix[p]);
    }
    return ret;
}
int main(int, char**) {
    std::vector<int> perm{ 1,2,0 };
    std::vector<std::vector<double>> matrix = { {1.,2.,3.},{4.,5.,6.},{7.,8.,9.} };
    auto matrixPerm=permute(matrix, perm);
    std::cout << matrixPerm[0][0] << " == " << matrix[1][0] << std::endl;
    std::cout << matrixPerm[1][0] << " == " << matrix[2][0] << std::endl;
    std::cout << matrixPerm[2][0] << " == " << matrix[0][0] << std::endl;
}

我现在想知道这个程序最优雅的版本是什么,如果我们可以使用 STL 甚至 Boost 库。例如,在STL中,我们有shuffle(),但我们不能说以什么方式洗牌。

现在有人,如何缩短功能?

使用std::transform()的解决方案

#include <vector>
#include <iostream>
#include <iterator>
#include <algorithm>
int main(int, char**) {
    std::vector<int> perm{ 1,2,0 };
    std::vector<std::vector<double>> matrix = { {1.,2.,3.},{4.,5.,6.},{7.,8.,9.} };
    std::vector<std::vector<double>> output;
    std::transform(perm.begin(), perm.end(), std::back_inserter(output), [&](int i) { return matrix[i]; });
    std::cout << output[0][0] << " == " << matrix[1][0] << std::endl;
    std::cout << output[1][0] << " == " << matrix[2][0] << std::endl;
    std::cout << output[2][0] << " == " << matrix[0][0] << std::endl;
}

您可以将索引转换为迭代器,然后使用 Boost.Range 创建间接范围。

#include <iostream>
#include <iterator>
#include <algorithm>
#include <boost/range/adaptor/indirected.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/range/algorithm/copy.hpp>
int main(int, char**) {
    using namespace boost::adaptors;

    std::vector<int> perm{ 1,2,0 };
    std::vector<std::vector<double>> matrix = { {1.,2.,3.},{4.,5.,6.},{7.,8.,9.} };
    std::vector<std::vector<double>> output;
    auto permutation = perm | transformed( [&matrix](int x) { return matrix.begin() + x; }) | indirected;
    boost::copy(
        permutation,
        std::back_inserter(output));
    std::cout << output[0][0] << " == " << matrix[1][0] << std::endl;
    std::cout << output[1][0] << " == " << matrix[2][0] << std::endl;
    std::cout << output[2][0] << " == " << matrix[0][0] << std::endl;
}

如果您不需要真实向量,您可以跳过复制元素并只处理范围。

范围适配器使用 Boost.Iterator 库中的排列迭代器。您也可以直接使用它,但必须手动定义开始和结束:

auto begin = make_permutation_iterator( matrix.begin(), perm.begin() );
auto end = make_permutation_iterator( matrix.end(), perm.end() );
std::copy(begin, end, std::back_inserter(output) );

相关内容

最新更新