Pass queue by reference to function

0

I want to pass by reference a Queue of type string that I created to save names, to display them by screen these must be passed by reference since I do not want to have everything in the main and this object in the future it must be manipulated in other functions.

queue<string>datos;
datos.push("Albert");
datos.push("Maria");
datos.push("Juan");

Something like;

void listar( queue datos) {
//Codigo...
}
    
asked by Albert Hidalgo 05.10.2018 в 01:55
source

1 answer

1

References serve to define "aliases" for the same object in this way we interact and manipulate an object a in the necessary functions and all the actions will affect the same object a . For this the reference operator (&) is used.

void listar( queue<string>& queue_data) {

    while(!queue_data.empty()) {
        cout<<" -> " <<queue_data.front() <<endl;
        queue_data.pop();
    }
}

The exit would be;

Juan
Maria
Albert
    
answered by 05.10.2018 / 01:55
source