When we pass an array to a function, a pointer is actually passed.
When a vector is passed to a function, a copy of the vector is created. For example, we can see below program, changes made inside the function are not reflected outside because function has a copy.
The above style of passing might also take a lot of time in cases of large vectors. So it is a good idea to pass by reference.
10 20 30
If we do not want a function to modify a vector, we can pass it as a const reference.
This article is contributed by Kartik. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Passing a vector to constructor in C++
- Passing by pointer Vs Passing by Reference in C++
- vector insert() function in C++ STL
- vector data() function in C++ STL
- vector capacity() function in C++ STL
- vector emplace() function in C++ STL
- vector max_size() function in C++ STL
- vector shrink_to_fit() function in C++ STL
- vector rbegin() and rend() function in C++ STL
- How to flatten a Vector of Vectors or 2D Vector in C++
- vector::crend() & vector::crbegin() with example
- vector :: cbegin() and vector :: cend() in C++ STL
- vector::front() and vector::back() in C++ STL
- vector::push_back() and vector::pop_back() in C++ STL
- vector::empty() and vector::size() in C++ STL