How to pass a vector as a parameter of a function in C++?

Asked

Viewed 216 times

0

I am declaring this vector and I want to pass it as a function parameter, as I do?

vector<string> matriz_ambiente;

    arquivo.open("teste.txt");

    if (arquivo.is_open()){
        while(getline(arquivo, linha)){
            matriz_ambiente.push_back(linha);
            cout << linha << endl;
        }

Function:

void geracao(string matriz_ambiente){

1 answer

1

The usual way to pass "large" variables in C++ is by reference:

// declaracao do vetor de strings
vector<string> matriz_ambiente;
...
...
// declaracao da funcao
void geracao(vector<string>& parm_matriz_ambiente)
{
  ...
  ...
}
...
// chamada da função
geracao(matriz_ambiente);
....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.