In the code below, the size of the function argument (foo) ( std::vector ) can be anything to make the function generic. However, sometimes the size of the container is known, so you can use std :: array. The problem is converting 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; }
c ++ vector c ++ 11 stdvector stdarray
Shibli
source share