简体   繁体   English

转换std :: array <std::array<T,N> &gt;进入std :: vector <T> 在现代C ++中

[英]Convert std::array<std::array<T,N>> into std::vector<T> in modern C++

I have a buch of triangles represented by a vector of 3d vectors ( std::vector<Vector3> ) and indices, represented in the following way 我有一个由3d向量( std::vector<Vector3> )和索引的向量表示的三角形,用以下方式表示

using int3 = std::array<int, 3>;
std::vector<int3> indices;

I need to convert the indices into a std::vector<size_t> format to interact with a library. 我需要将indices转换为std::vector<size_t>格式以与库进行交互。

I know how to do this using raw loops, but I would like to here about how to do it using algorithms. 我知道如何使用原始循环执行此操作,但是我想在这里了解如何使用算法执行此操作。 I tried something like 我尝试了类似的东西

std::vector<size_t> indices_converted;
std::transform(std::begin(indices), std::end(indices), std::back_inserter(indices_converted), [](const auto &v) {
});

but is not enough, since I can't insert three values at once into the indices_converted vector. 但这还不够,因为我无法一次将三个值插入indices_converted向量。

So, what are your ideas? 那么,您有什么想法?

One-liner using latest version of range-v3 library: 一线使用最新版本的range-v3库:

#include <array>
#include <vector>

#include <range/v3/view/join.hpp>
#include <range/v3/range/conversion.hpp>

// ...

std::vector<std::array<int, 3>> v = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
const auto result = ranges::views::join(v) | ranges::to<std::vector>();

On godbolt 在螺栓上

You can use either the range-based for loop or the standard algorithm std::for_each . 您可以使用基于范围的for循环或标准算法std::for_each

For example 例如

#include <iostream>
#include <array>
#include <vector>
#include <iterator>

int main() 
{
    using int3 = std::array<int, 3>;
    std::vector<int3> indices = 
    {
        { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }
    };

    std::vector<size_t> indices_converted;
    indices_converted.reserve( 3 * indices.size() );

    for ( const auto &item : indices )
    {
        indices_converted.insert( std::end( indices_converted ), std::begin( item ), std::end( item ) );
    }

    for ( const auto &item : indices_converted ) std::cout << item << ' ';
    std::cout << '\n';

    return 0;
}

or 要么

#include <iostream>
#include <array>
#include <vector>
#include <iterator>
#include <algorithm>

int main() 
{
    using int3 = std::array<int, 3>;
    std::vector<int3> indices = 
    {
        { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }
    };

    std::vector<size_t> indices_converted;
    indices_converted.reserve( 3 * indices.size() );

    std::for_each( std::begin( indices ), std::end( indices ),
                   [&indices_converted]( const auto &item )
                   {
                    indices_converted.insert( std::end( indices_converted ), std::begin( item ), std::end( item ) );        
                   } );

    for ( const auto &item : indices_converted ) std::cout << item << ' ';
    std::cout << '\n';

    return 0;
}

In the both cases the output is 在这两种情况下,输出为

1 2 3 4 5 6 7 8 9 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM