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
Post a Comment