I have a vector of elements of type uint8 which I am copying into one std::array. After some operations again I need to copy the elements from std::array to a vector.Please find the code below
/******************************************************************************
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. when I copy the elements from the array to a new vector I am getting extra 2 zeros. How can I get the output as below
output:
======
1
2
3