In the code below, the size of the function (foo) argument (std::vector
) can be anything to make the function a generic one. However, sometimes the size container is known so std::array can be used. The problem is to convert the std::array
to std::vector
. What is the best way to solve this problem? Is it better to just use std::vector
always in this case?
#include <iostream>
#include <array>
#include <vector>
using namespace std;
// generic function: size of the container can be anything
void foo (vector<int>& vec)
{
// do something
}
int main()
{
array<int,3> arr; // size is known. why use std::vector?
foo (arr); // cannot convert std::array to std::vector
return 0;
}