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

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -