c++ - Passing a vector as argument to function -


i have function this:

void foo(std::vector<bool> visited, int actual element); 

i use function bfs in graph, goes in infinite loop. suspect creates copy of visited vector. how make change vector, declared , initialized somewhere in main? , right whole "makes copy" theory?

how use pointer object, think <vector> object?

use referenced type

void foo(std::vector<bool> &visited, int actual element); 

otherwise function deals copy of original vector.

here dempnstrative program

#include <iostream> #include <vector>  void f( std::vector<int> &v ) {     v.assign( { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ); }  int main()  {     std::vector<int> v;      f( v );      ( int x : v ) std::cout << x << ' ';     std::cout << std::endl; }     

the program output is

0 1 2 3 4 5 6 7 8 9  

Comments

Popular posts from this blog

symfony - TEST environment only: The database schema is not in sync with the current mapping file -

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -