#include <vector>
#include <iostream>
using std::vector;
void foo(vector<int> & a, vector<int> & b) {
vector<int> vectors[] = {a, b};
std::cout << "vectors[0].size() = " << vectors[0].size() << std::endl;
vectors[0][0] = 42;
std::cout << "vectors[0][0] = " << vectors[0][0] << std::endl;
}
int main() {
vector<int> one = {1, 2, 3, 4};
vector<int> two = {11, 12, 13, 14};
std::cout << "one[0] = " << one[0] << std::endl;
foo(one, two);
std::cout << "one[0] = " << one[0] << std::endl;
}
This code creates an array of vectors which are copy constructed from the vectors which are referenced by the references passed as function arguments.
Therefore, modifications of the vectors do not escape the function scope ... and you have the drawback of a copy.
Since you're using vector<int> &
(a non-const reference) I assume you want these vectors to be "output" parameters. An array of references is not possible, but you can use std::reference_wrapper
to circumvent this restriction:
#include <functional>
void bar(vector<int> & a, vector<int> & b) {
using vref = std::reference_wrapper<vector<int>>;
vref vectors[] = {a, b};
std::cout << "vectors[0].size() = " << vectors[0].get().size() << std::endl;
vectors[0].get()[0] = 42;
std::cout << "vectors[0][0] = " << vectors[0].get()[0] << std::endl;
}
// Now use bar instead of foo with the above main function!