简体   繁体   English

将向量元素复制到 std::array

[英]Copying the vector elements into std::array

I have a vector of elements of type uint8 which I am copying into one std::array.我有一个 uint8 类型的元素向量,我将其复制到一个 std::array 中。 After some operations again I need to copy the elements from std::array to a vector.Please find the code below再次进行一些操作后,我需要将元素从 std::array 复制到向量。请找到下面的代码

/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <iostream>
#include <vector>
#include <array>
#include <cstdint>
#include <limits>
#include <algorithm>
std::array<std::uint32_t, 5> elem_arr ;
void copy_from_vector(std::vector<std::uint32_t> elem_vec)
{
    std::copy(elem_vec.begin(),elem_vec.end(),elem_arr.begin());
}
std::vector<std::uint32_t> copy_from_array()
{
    std::vector<std::uint32_t> ele_vec_copy {elem_arr.begin(),elem_arr.end()};
    return ele_vec_copy;
}
int main()
{

    std::vector<std::uint32_t> ele_vec {1,2,3};

    copy_from_vector(ele_vec);

    auto ele_vec_copy = copy_from_array();

    for(auto ele : ele_vec_copy)
        std::cout << ele << std::endl;

    return 0;
}
output:
======
1
2
3
0
0

Now the problem is my array size is 5 but vector having only 3 elements.现在的问题是我的数组大小是 5 但向量只有 3 个元素。 when I copy the elements from the array to a new vector I am getting extra 2 zeros.当我将数组中的元素复制到新向量时,我得到了额外的 2 个零。 How can I get the output as below我怎样才能得到如下输出

output:
======
1
2
3

You could copy by size of vector.您可以按矢量的大小进行复制。

#include <iostream>
#include <vector>
#include <array>
#include <cstdint>
#include <limits>
#include <algorithm>

int main()
{
    std::array<std::uint32_t, 5> elem_arr ;
    std::vector<std::uint32_t> ele_vec {1,2,3};
    std::copy(ele_vec.begin(),ele_vec.end(),elem_arr.begin());
    std::vector<std::uint32_t> ele_vec_copy { elem_arr.begin() , elem_arr.begin() + ele_vec.size() };

    for(auto ele : ele_vec_copy)
        std::cout << ele << std::endl;

    return 0;
}

run online在线运行

After you edited the question编辑问题后

It is not possible to know how many elements used/set in std::array but this feature could be mimic by choosing a unique value which represents the element is null or not set like nullptr .不可能知道在std::array使用/设置了多少元素,但可以通过选择一个唯一值来模拟此功能,该值表示元素为null或未像nullptr那样设置 But it comes with a constraint, you must not use the unique value at somewhere else in the array.但它有一个约束,你不能在数组的其他地方使用唯一值。

So the possible implementation is :所以可能的实现是:

#include <iostream>
#include <vector>
#include <array>
#include <cstdint>
#include <limits>
#include <algorithm>
#include <limits>

std::array<std::uint32_t, 5> elem_arr;

void copy_from_vector(std::vector<std::uint32_t> elem_vec)
{
    std::copy(elem_vec.begin(),elem_vec.end(),elem_arr.begin());
}

std::vector<std::uint32_t> copy_from_array()
{
    std::vector<std::uint32_t> ele_vec_copy;
    std::copy_if( elem_arr.begin() , elem_arr.end() , 
                  std::back_inserter( ele_vec_copy ) , 
                  []( std::uint32_t val ) { 
                      return val != std::numeric_limits<std::uint32_t>::max(); 

                  } );
    return ele_vec_copy;
}

int main()
{

    elem_arr.fill( std::numeric_limits<std::uint32_t>::max() );
    std::vector<std::uint32_t> ele_vec {1,2,3};

    copy_from_vector(ele_vec);

    auto ele_vec_copy = copy_from_array();

    for(auto ele : ele_vec_copy)
        std::cout << ele << std::endl;

    return 0;
}

run online在线运行

Or store the elements as std::optional and don't copy null elements.或者将元素存储为std::optional并且不要复制null元素。 Possible implementation is :可能的实现是:

#include <iostream>
#include <vector>
#include <array>
#include <cstdint>
#include <limits>
#include <algorithm>
#include <limits>
#include <optional>

std::array<std::optional<std::uint32_t>, 5> elem_arr;

void copy_from_vector(std::vector<std::uint32_t> elem_vec)
{
    std::transform( elem_vec.begin() , elem_vec.end() , 
                    elem_arr.begin() ,
                    []( auto val ) { return std::optional<std::uint32_t>{ val }; }
                   );
}

std::vector<std::uint32_t> copy_from_array()
{
    std::vector<std::uint32_t> ele_vec_copy;
    std::vector<std::optional<std::uint32_t>> non_null_elements;
    std::copy_if( elem_arr.begin() , elem_arr.end() , 
                  std::back_inserter( non_null_elements ) , 
                  []( auto val ) { 
                      return val;
                  } );
    std::transform( non_null_elements.begin(), non_null_elements.end() , 
                    std::back_inserter( ele_vec_copy ) ,
                    []( auto val ) { return *val; } 
                  );
    return ele_vec_copy;
}

int main()
{
    std::vector<std::uint32_t> ele_vec {1,2,3};

    copy_from_vector(ele_vec);

    auto ele_vec_copy = copy_from_array();

    for(auto ele : ele_vec_copy)
        std::cout << ele << std::endl;

    return 0;
}

run online在线运行

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

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